feat: add custom mood profile editor
CI / test (push) Successful in 4m55s

This commit is contained in:
strsv committed 2026-11-06 21:52:24 +03:00
1 parent d860157540
commit b865a4a389
11 files changed
+274 -9

No files matched your search

+81 -1
View File
@@ -610,6 +610,9 @@ def game_filters(qs):
return where, params, order, q
MOOD_NAME_RE = re.compile(r"^[a-z0-9][a-z0-9-]{1,39}$")
class Handler(BaseHTTPRequestHandler):
server_version = "SteamBase/1.0"
@@ -644,6 +647,19 @@ class Handler(BaseHTTPRequestHandler):
return {}
return json.loads(self.rfile.read(length).decode("utf-8"))
def do_DELETE(self):
parsed = urllib.parse.urlparse(self.path)
path = parsed.path
try:
if path.startswith("/api/moods/"):
name = urllib.parse.unquote(path.rsplit("/", 1)[1])
self._delete_mood(name)
else:
self._error(404, "not found")
except Exception as exc:
log_event("http_error", method="DELETE", path=path, error=str(exc))
self._error(500, str(exc))
def do_GET(self):
parsed = urllib.parse.urlparse(self.path)
path = parsed.path
@@ -664,6 +680,8 @@ class Handler(BaseHTTPRequestHandler):
self._recommend(self._body())
elif path == "/api/feedback":
self._feedback(self._body())
elif path == "/api/moods":
self._save_mood(self._body())
elif path == "/api/prefetch":
body = self._body()
ids = self._missing_ids(body.get("limit", 200), bool(body.get("retry_missing")))
@@ -1125,12 +1143,74 @@ class Handler(BaseHTTPRequestHandler):
con = connect()
try:
moods = [{"name": r["name"], "description": r["description"],
"genres": json.loads(r["genres"]), "tags": json.loads(r["tags"])}
"genres": json.loads(r["genres"]), "tags": json.loads(r["tags"]),
"builtin": r["name"] in db.MOOD_PRESETS}
for r in con.execute("SELECT * FROM moods ORDER BY name")]
self._json({"moods": moods})
finally:
con.close()
def _save_mood(self, body):
name = str(body.get("name") or "").strip().lower()
if name in db.MOOD_PRESETS:
self._error(400, "builtin profile")
return
if not MOOD_NAME_RE.match(name):
self._error(400, "invalid name")
return
description = str(body.get("description") or "").strip()[:200]
genres = []
for genre in body.get("genres") or []:
value = str(genre).strip()
if value and value not in genres:
genres.append(value)
tags = {}
for tag, weight in (body.get("tags") or {}).items():
try:
value = float(weight)
except (TypeError, ValueError):
continue
tag = str(tag).strip()
if tag and 0.1 <= value <= 5.0:
tags[tag] = round(value, 2)
tags = dict(list(tags.items())[:30])
if not genres and not tags:
self._error(400, "empty profile")
return
con = connect()
try:
now = datetime.now(timezone.utc).isoformat(timespec="seconds")
con.execute(
"""INSERT INTO moods (name, description, genres, tags, updated_at)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(name) DO UPDATE SET
description = excluded.description,
genres = excluded.genres,
tags = excluded.tags,
updated_at = excluded.updated_at""",
(name, description, json.dumps(genres), json.dumps(tags), now))
con.commit()
self._json({"mood": {"name": name, "description": description,
"genres": genres, "tags": tags}, "builtin": False})
finally:
con.close()
def _delete_mood(self, name):
name = (name or "").strip().lower()
if name in db.MOOD_PRESETS:
self._error(400, "builtin profile")
return
con = connect()
try:
cursor = con.execute("DELETE FROM moods WHERE name = ?", (name,))
con.commit()
if not cursor.rowcount:
self._error(404, "unknown profile")
return
self._json({"deleted": True})
finally:
con.close()
def _recommend(self, body):
con = connect()
try: