mod mbid with uuid

This commit is contained in:
Francesco De Carlo 2025-08-01 23:24:54 +02:00
parent 9ba0e04501
commit b7fed3c719

View file

@ -8,7 +8,7 @@ def init_db():
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(""" cursor.execute("""
CREATE TABLE IF NOT EXISTS songs ( CREATE TABLE IF NOT EXISTS songs (
mbid TEXT PRIMARY KEY, uuid TEXT PRIMARY KEY,
title TEXT NOT NULL, title TEXT NOT NULL,
artist TEXT NOT NULL, artist TEXT NOT NULL,
tags TEXT NOT NULL, tags TEXT NOT NULL,
@ -22,7 +22,7 @@ def init_db():
@dataclass(frozen=True) @dataclass(frozen=True)
class Song: class Song:
mbid: str uuid: str
title: str title: str
artist: str artist: str
tags: list[str] tags: list[str]
@ -30,17 +30,17 @@ class Song:
youtube_id: str youtube_id: str
def get_song_by_mbid(mbid: str) -> Song | None: def get_song_by_uuid(uuid: str) -> Song | None:
conn = get_connection() conn = get_connection()
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute("SELECT * FROM songs WHERE mbid = ?", (mbid,)) cursor.execute("SELECT * FROM songs WHERE uuid = ?", (uuid,))
row = cursor.fetchone() row = cursor.fetchone()
conn.close() conn.close()
if row is None: if row is None:
return None return None
song = Song(mbid=row["mbid"], title=row["title"], artist=row["artist"], tags=row["tags"].split(","), image_id=row["lastfm_image_id"], youtube_id=row["youtube_id"]) song = Song(uuid=row["uuid"], title=row["title"], artist=row["artist"], tags=row["tags"].split(","), image_id=row["lastfm_image_id"], youtube_id=row["youtube_id"])
return song return song
@ -54,7 +54,7 @@ def get_song_by_title_artist(title: str, artist: str) -> Song | None:
if row is None: if row is None:
return None return None
song = Song(mbid=row["mbid"], title=row["title"], artist=row["artist"], tags=row["tags"].split(","), image_id=row["lastfm_image_id"], youtube_id=row["youtube_id"]) song = Song(uuid=row["uuid"], title=row["title"], artist=row["artist"], tags=row["tags"].split(","), image_id=row["lastfm_image_id"], youtube_id=row["youtube_id"])
return song return song
@ -64,10 +64,10 @@ def add_song(song: Song):
cursor.execute( cursor.execute(
""" """
INSERT OR REPLACE INTO songs (mbid, title, artist, tags, lastfm_image_id, youtube_id) INSERT OR REPLACE INTO songs (uuid, title, artist, tags, lastfm_image_id, youtube_id)
VALUES (?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?)
""", """,
(song.mbid, song.title, song.artist, ",".join(song.tags), song.image_id, song.youtube_id), (song.uuid, song.title, song.artist, ",".join(song.tags), song.image_id, song.youtube_id),
) # Updates song info if it already exists ) # Updates song info if it already exists
conn.commit() conn.commit()
conn.close() conn.close()