From 3a4f145f41cd1c00a694f2d8b83c8e0b33b25d3d Mon Sep 17 00:00:00 2001 From: krateng Date: Tue, 28 Mar 2023 16:04:50 +0200 Subject: [PATCH] Added album support for scrobbling --- maloja/apis/listenbrainz.py | 2 +- maloja/apis/native_v1.py | 2 +- maloja/database/__init__.py | 2 ++ maloja/database/sqldb.py | 25 +++++++++++++++++-------- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/maloja/apis/listenbrainz.py b/maloja/apis/listenbrainz.py index 234048c..cb15b89 100644 --- a/maloja/apis/listenbrainz.py +++ b/maloja/apis/listenbrainz.py @@ -75,7 +75,7 @@ class Listenbrainz(APIHandler): self.scrobble({ 'track_artists':[artiststr], 'track_title':titlestr, - 'album_name':albumstr, + 'album_title':albumstr, 'scrobble_time':timestamp, 'track_length': additional.get("duration"), **extrafields diff --git a/maloja/apis/native_v1.py b/maloja/apis/native_v1.py index 41fa636..4e9c49f 100644 --- a/maloja/apis/native_v1.py +++ b/maloja/apis/native_v1.py @@ -470,7 +470,7 @@ def post_scrobble( rawscrobble = { 'track_artists':(artist or []) + artists, 'track_title':title, - 'album_name':album, + 'album_title':album, 'album_artists':albumartists, 'scrobble_duration':duration, 'track_length':length, diff --git a/maloja/database/__init__.py b/maloja/database/__init__.py index c6178f0..54b76c9 100644 --- a/maloja/database/__init__.py +++ b/maloja/database/__init__.py @@ -130,8 +130,10 @@ def rawscrobble_to_scrobbledict(rawscrobble, fix=True, client=None): scrobbleinfo = {**rawscrobble} if fix: scrobbleinfo['track_artists'],scrobbleinfo['track_title'] = cla.fullclean(scrobbleinfo['track_artists'],scrobbleinfo['track_title']) + scrobbleinfo['album_artists'] = cla.parseArtists(scrobbleinfo['album_artists']) scrobbleinfo['scrobble_time'] = scrobbleinfo.get('scrobble_time') or int(datetime.datetime.now(tz=datetime.timezone.utc).timestamp()) + # processed info to internal scrobble dict scrobbledict = { "time":scrobbleinfo.get('scrobble_time'), diff --git a/maloja/database/sqldb.py b/maloja/database/sqldb.py index 9a5b17f..3ec27f2 100644 --- a/maloja/database/sqldb.py +++ b/maloja/database/sqldb.py @@ -335,12 +335,19 @@ def delete_scrobble(scrobble_id,dbconn=None): @connection_provider def add_track_to_album(track_id,album_id,replace=False,dbconn=None): + conditions = [ + DB['tracks'].c.id == track_id + ] + if not replace: + # if we dont want replacement, just update if there is no album yet + conditions.append( + DB['tracks'].c.album_id == None + ) + op = DB['tracks'].update().where( - DB['tracks'].c.id == track_id, - (DB['tracks'].c.album_id == None) or replace - # update if we want replacement or if there is no album yet + *conditions ).values( - DB['tracks'].c.album_id = album_id + album_id=album_id ) result = dbconn.execute(op) @@ -380,7 +387,8 @@ def get_track_id(trackdict,create_new=True,update_album=False,dbconn=None): #print("required artists",artist_ids,"this match",match_artist_ids) if set(artist_ids) == set(match_artist_ids): #print("ID for",trackdict['title'],"was",row[0]) - add_track_to_album(row.id,get_album_id(trackdict['album']),replace=update_album) + if 'album' in trackdict: + add_track_to_album(row.id,get_album_id(trackdict['album']),replace=update_album) return row.id if not create_new: return None @@ -399,7 +407,8 @@ def get_track_id(trackdict,create_new=True,update_album=False,dbconn=None): result = dbconn.execute(op) #print("Created",trackdict['title'],track_id) - add_track_to_album(track_id,get_album_id(trackdict['album'])) + if 'album' in trackdict: + add_track_to_album(track_id,get_album_id(trackdict['album'])) return track_id @cached_wrapper @@ -442,7 +451,7 @@ def get_album_id(albumdict,create_new=True,dbconn=None): op = DB['albums'].select( # DB['albums'].c.id ).where( - DB['albums'].c.title_normalized==ntitle + DB['albums'].c.albtitle_normalized==ntitle ) result = dbconn.execute(op).all() for row in result: @@ -452,7 +461,7 @@ def get_album_id(albumdict,create_new=True,dbconn=None): op = DB['albumartists'].select( # DB['albumartists'].c.artist_id ).where( - DB['albumartists'].c.track_id==row.id + DB['albumartists'].c.album_id==row.id ) result = dbconn.execute(op).all() match_artist_ids = [r.artist_id for r in result]