diff --git a/maloja/apis/native_v1.py b/maloja/apis/native_v1.py index 1d7f232..7b56a05 100644 --- a/maloja/apis/native_v1.py +++ b/maloja/apis/native_v1.py @@ -589,3 +589,9 @@ def get_export(**keys): def delete_scrobble(timestamp): """Internal Use Only""" database.remove_scrobble(timestamp) + +@api.post("reparse_scrobble") +@authenticated_function(api=True) +def reparse_scrobble(timestamp): + """Internal Use Only""" + database.reparse_scrobble(timestamp) diff --git a/maloja/database/__init__.py b/maloja/database/__init__.py index d83c8f3..ba3499f 100644 --- a/maloja/database/__init__.py +++ b/maloja/database/__init__.py @@ -102,7 +102,35 @@ def incoming_scrobble(rawscrobble,fix=True,client=None,api=None,dbconn=None): log(f"Incoming scrobble [Client: {client} | API: {api}]: {rawscrobble}") - # raw scrobble to processed info + scrobbledict = rawscrobble_to_scrobbledict(rawscrobble, fix, client); + + sqldb.add_scrobble(scrobbledict,dbconn=dbconn) + proxy_scrobble_all(scrobbledict['track']['artists'],scrobbledict['track']['title'],scrobbledict['time']) + + dbcache.invalidate_caches(scrobbledict['time']) + + #return {"status":"success","scrobble":scrobbledict} + return scrobbledict + +@waitfordb +def reparse_scrobble(timestamp): + log(f"Reparsing Scrobble {timestamp}") + scrobble = sqldb.get_scrobble(timestamp) + + if not scrobble: + return + + scrobbledict = rawscrobble_to_scrobbledict(scrobble['rawscrobble']) + + track_id = sqldb.get_track_id(scrobbledict['track']) + + # check if id changed + if sqldb.get_track_id(scrobble['track']) != track_id: + sqldb.update_scrobble_track_id(timestamp, track_id) + + +def rawscrobble_to_scrobbledict(rawscrobble, fix=True, client=None): + # raw scrobble to processed info scrobbleinfo = {**rawscrobble} if fix: scrobbleinfo['track_artists'],scrobbleinfo['track_title'] = cla.fullclean(scrobbleinfo['track_artists'],scrobbleinfo['track_title']) @@ -129,17 +157,9 @@ def incoming_scrobble(rawscrobble,fix=True,client=None,api=None,dbconn=None): "rawscrobble":rawscrobble } - - sqldb.add_scrobble(scrobbledict,dbconn=dbconn) - proxy_scrobble_all(scrobbledict['track']['artists'],scrobbledict['track']['title'],scrobbledict['time']) - - dbcache.invalidate_caches(scrobbledict['time']) - - #return {"status":"success","scrobble":scrobbledict} return scrobbledict - @waitfordb def remove_scrobble(timestamp): log(f"Deleting Scrobble {timestamp}") diff --git a/maloja/database/sqldb.py b/maloja/database/sqldb.py index 1623d57..8888587 100644 --- a/maloja/database/sqldb.py +++ b/maloja/database/sqldb.py @@ -277,6 +277,17 @@ def delete_scrobble(scrobble_id,dbconn=None): dbconn.execute(op) +@connection_provider +def update_scrobble_track_id(scrobble_id, track_id, dbconn=None): + + with SCROBBLE_LOCK: + + op = DB['scrobbles'].update().where( + DB['scrobbles'].c.timestamp == scrobble_id + ).values(track_id=track_id) + + dbconn.execute(op) + ### these will 'get' the ID of an entity, creating it if necessary @cached_wrapper @@ -691,7 +702,16 @@ def get_artist(id,dbconn=None): return artist_db_to_dict(artistinfo) +@cached_wrapper +@connection_provider +def get_scrobble(timestamp,dbconn=None): + op = DB['scrobbles'].select().where( + DB['scrobbles'].c.timestamp==timestamp + ) + result = dbconn.execute(op).all() + scrobble = result[0] + return scrobbles_db_to_dict([scrobble], True)[0] ##### MAINTENANCE diff --git a/maloja/web/jinja/partials/scrobbles.jinja b/maloja/web/jinja/partials/scrobbles.jinja index 1fb1cbd..9fac4d0 100644 --- a/maloja/web/jinja/partials/scrobbles.jinja +++ b/maloja/web/jinja/partials/scrobbles.jinja @@ -17,6 +17,22 @@ {{ entityrow.row(s.track) }} {% if adminmode %} + + + + + + + +
+ + + +
+
+ + + diff --git a/maloja/web/static/js/edit.js b/maloja/web/static/js/edit.js index 0a415ba..8e0ef16 100644 --- a/maloja/web/static/js/edit.js +++ b/maloja/web/static/js/edit.js @@ -10,3 +10,12 @@ function deleteScrobble(id,element) { neo.xhttpreq("/apis/mlj_1/delete_scrobble",data={'timestamp':id},method="POST",callback=(()=>null),json=true); } + +function toggleReparseConfirm(element) { + element.parentElement.parentElement.classList.toggle('active'); +} + +function reparseScrobble(id) { + neo.xhttpreq("/apis/mlj_1/reparse_scrobble",data={'timestamp':id},method="POST",callback=(()=>null),json=true); + +}