Bedre tag sync

This commit is contained in:
2026-04-13 15:33:01 +02:00
parent cb204baa50
commit b066b6d92c
5 changed files with 90 additions and 12 deletions

View File

@@ -54,14 +54,14 @@ class SyncManager:
def _run():
try:
payload = self._build_push_payload()
logger.info(f"Push: {len(payload['songs'])} sange, {len(payload['playlists'])} playlister")
result = self._post("/sync/push", payload)
# Gem server-IDs lokalt
self._save_playlist_ids(result.get("playlist_id_map", {}))
logger.info(f"Sync push: {result}")
logger.info(f"Push OK: {result}")
if on_done:
on_done(result)
except Exception as e:
logger.error(f"Sync push fejl: {e}")
logger.error(f"Sync push fejl: {e}", exc_info=True)
if on_error:
on_error(str(e))
threading.Thread(target=_run, daemon=True).start()
@@ -79,6 +79,51 @@ class SyncManager:
)
except Exception:
pass
# Importer brugerens egne dans-tags
for tag in data.get("song_tags", []):
title = tag.get("song_title", "")
artist = tag.get("song_artist", "")
dance_name = tag.get("dance_name", "")
level_name = tag.get("level_name", "")
if not title or not dance_name:
continue
song = conn.execute(
"SELECT id FROM songs WHERE title=? AND artist=? LIMIT 1",
(title, artist)
).fetchone()
if not song:
continue
level_row = conn.execute(
"SELECT id FROM dance_levels WHERE name=? COLLATE NOCASE LIMIT 1",
(level_name,)
).fetchone() if level_name else None
level_id = level_row["id"] if level_row else None
dance_row = conn.execute(
"SELECT id FROM dances WHERE name=? AND level_id IS ? LIMIT 1",
(dance_name, level_id)
).fetchone()
if not dance_row:
cur = conn.execute(
"INSERT OR IGNORE INTO dances (name, level_id) VALUES (?,?)",
(dance_name, level_id)
)
dance_id = cur.lastrowid
else:
dance_id = dance_row["id"]
existing = conn.execute(
"SELECT id FROM song_dances WHERE song_id=? AND dance_id=?",
(song["id"], dance_id)
).fetchone()
if not existing:
max_order = conn.execute(
"SELECT COALESCE(MAX(dance_order),0) FROM song_dances WHERE song_id=?",
(song["id"],)
).fetchone()[0]
conn.execute(
"INSERT INTO song_dances (song_id, dance_id, dance_order) VALUES (?,?,?)",
(song["id"], dance_id, max_order + 1)
)
conn.commit()
conn.close()
@@ -87,12 +132,15 @@ class SyncManager:
def _run():
try:
result = self._get("/sync/pull")
pl_count = len(result.get("my_playlists", []))
logger.info(f"Pull: {len(result.get('dances', []))} danse, {pl_count} playlister")
for pl in result.get("my_playlists", []):
logger.info(f" Playliste fra server: '{pl['name']}' ({len(pl.get('songs',[]))} sange)")
self._apply_pull(result)
logger.info(f"Sync pull: {len(result.get('dances', []))} danse")
if on_done:
on_done(result)
except Exception as e:
logger.error(f"Sync pull fejl: {e}")
logger.error(f"Sync pull fejl: {e}", exc_info=True)
if on_error:
on_error(str(e))
threading.Thread(target=_run, daemon=True).start()
@@ -101,14 +149,21 @@ class SyncManager:
"""Push og derefter pull i samme tråd."""
def _run():
try:
logger.info("push_and_pull: bygger payload...")
payload = self._build_push_payload()
logger.info(f"push_and_pull: sender {len(payload['songs'])} sange, {len(payload['playlists'])} playlister")
push_result = self._post("/sync/push", payload)
logger.info(f"push_and_pull: push OK — {push_result}")
pull_result = self._get("/sync/pull")
pl_count = len(pull_result.get("my_playlists", []))
logger.info(f"push_and_pull: pull OK — {pl_count} playlister")
for pl in pull_result.get("my_playlists", []):
logger.info(f" Playliste: '{pl['name']}' ({len(pl.get('songs',[]))} sange)")
self._apply_pull(pull_result)
if on_done:
on_done({"push": push_result, "pull": pull_result})
except Exception as e:
logger.error(f"Sync fejl: {e}")
logger.error(f"push_and_pull fejl: {e}", exc_info=True)
if on_error:
on_error(str(e))
threading.Thread(target=_run, daemon=True).start()