1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00

Added album support for scrobbling

This commit is contained in:
krateng 2023-03-28 16:04:50 +02:00
parent 7f62021d57
commit 3a4f145f41
4 changed files with 21 additions and 10 deletions

View File

@ -75,7 +75,7 @@ class Listenbrainz(APIHandler):
self.scrobble({ self.scrobble({
'track_artists':[artiststr], 'track_artists':[artiststr],
'track_title':titlestr, 'track_title':titlestr,
'album_name':albumstr, 'album_title':albumstr,
'scrobble_time':timestamp, 'scrobble_time':timestamp,
'track_length': additional.get("duration"), 'track_length': additional.get("duration"),
**extrafields **extrafields

View File

@ -470,7 +470,7 @@ def post_scrobble(
rawscrobble = { rawscrobble = {
'track_artists':(artist or []) + artists, 'track_artists':(artist or []) + artists,
'track_title':title, 'track_title':title,
'album_name':album, 'album_title':album,
'album_artists':albumartists, 'album_artists':albumartists,
'scrobble_duration':duration, 'scrobble_duration':duration,
'track_length':length, 'track_length':length,

View File

@ -130,8 +130,10 @@ def rawscrobble_to_scrobbledict(rawscrobble, fix=True, client=None):
scrobbleinfo = {**rawscrobble} scrobbleinfo = {**rawscrobble}
if fix: if fix:
scrobbleinfo['track_artists'],scrobbleinfo['track_title'] = cla.fullclean(scrobbleinfo['track_artists'],scrobbleinfo['track_title']) 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()) scrobbleinfo['scrobble_time'] = scrobbleinfo.get('scrobble_time') or int(datetime.datetime.now(tz=datetime.timezone.utc).timestamp())
# processed info to internal scrobble dict # processed info to internal scrobble dict
scrobbledict = { scrobbledict = {
"time":scrobbleinfo.get('scrobble_time'), "time":scrobbleinfo.get('scrobble_time'),

View File

@ -335,12 +335,19 @@ def delete_scrobble(scrobble_id,dbconn=None):
@connection_provider @connection_provider
def add_track_to_album(track_id,album_id,replace=False,dbconn=None): 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( op = DB['tracks'].update().where(
DB['tracks'].c.id == track_id, *conditions
(DB['tracks'].c.album_id == None) or replace
# update if we want replacement or if there is no album yet
).values( ).values(
DB['tracks'].c.album_id = album_id album_id=album_id
) )
result = dbconn.execute(op) result = dbconn.execute(op)
@ -380,6 +387,7 @@ def get_track_id(trackdict,create_new=True,update_album=False,dbconn=None):
#print("required artists",artist_ids,"this match",match_artist_ids) #print("required artists",artist_ids,"this match",match_artist_ids)
if set(artist_ids) == set(match_artist_ids): if set(artist_ids) == set(match_artist_ids):
#print("ID for",trackdict['title'],"was",row[0]) #print("ID for",trackdict['title'],"was",row[0])
if 'album' in trackdict:
add_track_to_album(row.id,get_album_id(trackdict['album']),replace=update_album) add_track_to_album(row.id,get_album_id(trackdict['album']),replace=update_album)
return row.id return row.id
@ -399,6 +407,7 @@ def get_track_id(trackdict,create_new=True,update_album=False,dbconn=None):
result = dbconn.execute(op) result = dbconn.execute(op)
#print("Created",trackdict['title'],track_id) #print("Created",trackdict['title'],track_id)
if 'album' in trackdict:
add_track_to_album(track_id,get_album_id(trackdict['album'])) add_track_to_album(track_id,get_album_id(trackdict['album']))
return track_id return track_id
@ -442,7 +451,7 @@ def get_album_id(albumdict,create_new=True,dbconn=None):
op = DB['albums'].select( op = DB['albums'].select(
# DB['albums'].c.id # DB['albums'].c.id
).where( ).where(
DB['albums'].c.title_normalized==ntitle DB['albums'].c.albtitle_normalized==ntitle
) )
result = dbconn.execute(op).all() result = dbconn.execute(op).all()
for row in result: for row in result:
@ -452,7 +461,7 @@ def get_album_id(albumdict,create_new=True,dbconn=None):
op = DB['albumartists'].select( op = DB['albumartists'].select(
# DB['albumartists'].c.artist_id # DB['albumartists'].c.artist_id
).where( ).where(
DB['albumartists'].c.track_id==row.id DB['albumartists'].c.album_id==row.id
) )
result = dbconn.execute(op).all() result = dbconn.execute(op).all()
match_artist_ids = [r.artist_id for r in result] match_artist_ids = [r.artist_id for r in result]