Skip to content

Commit

Permalink
bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
blastbeng committed Oct 5, 2024
1 parent 6be05be commit 03b562a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
72 changes: 60 additions & 12 deletions spotisub/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,14 @@ def create_playlist(playlist_info):
def insert_playlist_type(conn, playlist_info):
"""insert playlist into database"""
playlist_info_db = None
if "uuid" in playlist_info and playlist_info["uuid"] is not None:
playlist_info_db = select_playlist_info_by_uuid_with_conn(
conn, playlist_info["uuid"])
elif "name" in playlist_info and playlist_info["name"] is not None:
playlist_info_db = select_playlist_info_by_uuid_with_conn(
conn, playlist_info["uuid"])
if playlist_info_db is None and "name" in playlist_info and playlist_info["name"] is not None:
playlist_info_db = select_playlist_info_by_name_with_conn(
conn, playlist_info["name"])
elif playlist_info_db is None and "import_arg" in playlist_info and playlist_info["import_arg"] is not None:
playlist_info_db = select_playlist_info_by_arg_with_conn(
conn, playlist_info["import_arg"])
subsonic_playlist_id_info = playlist_info[
"subsonic_playlist_id"] if "subsonic_playlist_id" in playlist_info else None
if playlist_info_db is None:
Expand Down Expand Up @@ -437,7 +439,7 @@ def select_playlist_info_by_name_with_conn(conn, name):


def select_playlist_info_by_arg(arg):
"""select spotify artists by uuid"""
"""select spotify artists by arg"""
with dbms.db_engine.connect() as conn:
value = None
stmt = select(
Expand All @@ -461,6 +463,29 @@ def select_playlist_info_by_arg(arg):
return value


def select_playlist_info_by_arg_with_conn(conn, arg):
"""select spotify artists by arg"""
value = None
stmt = select(
dbms.playlist_info.c.uuid,
dbms.playlist_info.c.subsonic_playlist_id,
dbms.playlist_info.c.subsonic_playlist_name,
dbms.playlist_info.c.spotify_playlist_uri,
dbms.playlist_info.c.ignored,
dbms.playlist_info.c.type,
dbms.playlist_info.c.import_arg).where(
dbms.playlist_info.c.import_arg == arg)
stmt.compile()
cursor = conn.execute(stmt)
records = cursor.fetchall()

for row in records:
value = row
cursor.close()

return value


def select_playlist_info_by_type(type):
"""select spotify artists by uuid"""
records = []
Expand Down Expand Up @@ -517,7 +542,7 @@ def select_playlist_info_by_uuid_with_conn(conn, uuid):
dbms.playlist_info.c.subsonic_playlist_name,
dbms.playlist_info.c.spotify_playlist_uri,
dbms.playlist_info.c.ignored,
dbms.playlist_info.c.type,
dbms.playlist_info .c.type,
dbms.playlist_info.c.import_arg).where(
dbms.playlist_info.c.uuid == uuid)
stmt.compile()
Expand All @@ -533,8 +558,8 @@ def select_playlist_info_by_uuid_with_conn(conn, uuid):
def delete_playlist_relation_by_id(playlist_id: str):
"""delete playlist from database"""
with dbms.db_engine.connect() as conn:
pl_info = select_playlist_info_by_subsonic_id(
playlist_id, conn_ext=conn)
pl_info = select_playlist_info_by_subsonic_id_with_conn(
conn, playlist_id)
if pl_info is not None:
stmt1 = delete(dbms.subsonic_spotify_relation).where(
dbms.subsonic_spotify_relation.c.playlist_info_uuid == pl_info)
Expand Down Expand Up @@ -612,11 +637,35 @@ def insert_playlist_relation(
conn.execute(stmt)
return select_playlist_relation_by_uuid(old_relation.uuid)

def select_playlist_info_by_subsonic_id_with_conn(conn, subsonic_playlist_uuid):
"""select spotify artists by uuid"""
value = None
stmt = select(
dbms.playlist_info.c.uuid,
dbms.playlist_info.c.subsonic_playlist_id,
dbms.playlist_info.c.subsonic_playlist_name,
dbms.playlist_info.c.spotify_playlist_uri,
dbms.playlist_info.c.ignored,
dbms.playlist_info.c.type).where(
dbms.playlist_info.c.subsonic_playlist_id == subsonic_playlist_uuid)

stmt = stmt.group_by(
dbms.playlist_info.c.uuid)

stmt.compile()
cursor = conn.execute(stmt)
records = cursor.fetchall()

def select_playlist_info_by_subsonic_id(subsonic_playlist_uuid, conn_ext=None):
for row in records:
value = row
cursor.close()

return value

def select_playlist_info_by_subsonic_id(subsonic_playlist_uuid):
"""select spotify artists by uuid"""
value = None
with conn_ext if conn_ext is not None else dbms.db_engine.connect() as conn:
with dbms.db_engine.connect() as conn:
stmt = select(
dbms.playlist_info.c.uuid,
dbms.playlist_info.c.subsonic_playlist_id,
Expand All @@ -636,8 +685,7 @@ def select_playlist_info_by_subsonic_id(subsonic_playlist_uuid, conn_ext=None):
for row in records:
value = row
cursor.close()
if conn_ext is None:
conn.close()
conn.close()

return value

Expand Down
2 changes: 1 addition & 1 deletion spotisub/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def artist_top_tracks_run(uuid):
logging.info('(%s) Searching top tracks for: %s',
str(threading.current_thread().ident), playlist_info_db.import_arg)
sp = spotipy_helper.get_spotipy_client()
artist_top = sp.artist_top_tracks(artists_uri[artist_name])
artist_top = sp.artist_top_tracks(artist["uri"])
subsonic_helper.write_playlist(sp, playlist_info, artist_top)
else:
logging.warning('(%s) Artist: %s Not found!', str(
Expand Down

0 comments on commit 03b562a

Please sign in to comment.