Better exceptions for existing scrobble timestamps

This commit is contained in:
krateng 2024-01-20 20:11:50 +01:00
parent 386f3c4a41
commit ed34992d8b
3 changed files with 39 additions and 15 deletions

View File

@ -89,6 +89,16 @@ errors = {
'desc':"A scrobble is already registered with this timestamp." 'desc':"A scrobble is already registered with this timestamp."
} }
}), }),
database.exceptions.DuplicateScrobble: lambda e: (200,{
"status": "success",
"desc": "The scrobble is present in the database.",
"track": {},
"warnings": [{
'type': 'scrobble_exists',
'value': None,
'desc': 'This scrobble exists in the database (same timestamp and track). The submitted scrobble was not added.'
}]
}),
images.MalformedB64: lambda e: (400,{ images.MalformedB64: lambda e: (400,{
"status":"failure", "status":"failure",
"error":{ "error":{

View File

@ -26,7 +26,7 @@ class DuplicateTimestamp(Exception):
# if it's the same scrobble # if it's the same scrobble
class DuplicateScrobble(EntityExists): class DuplicateScrobble(Exception):
def __init__(self, scrobble): def __init__(self, scrobble):
self.scrobble = scrobble self.scrobble = scrobble

View File

@ -328,34 +328,48 @@ def album_dict_to_db(info,dbconn=None):
@connection_provider @connection_provider
def add_scrobble(scrobbledict,update_album=False,dbconn=None): def add_scrobble(scrobbledict,update_album=False,dbconn=None):
_, e = add_scrobbles([scrobbledict],update_album=update_album,dbconn=dbconn) _, ex, er = add_scrobbles([scrobbledict],update_album=update_album,dbconn=dbconn)
if e > 0: if er > 0:
raise exc.DuplicateTimestamp(existing_scrobble=None,rejected_scrobble=scrobbledict) raise exc.DuplicateTimestamp(existing_scrobble=None,rejected_scrobble=scrobbledict)
# TODO: actually pass existing scrobble # TODO: actually pass existing scrobble
elif ex > 0:
raise exc.DuplicateScrobble(scrobble=scrobbledict)
@connection_provider @connection_provider
def add_scrobbles(scrobbleslist,update_album=False,dbconn=None): def add_scrobbles(scrobbleslist,update_album=False,dbconn=None):
with SCROBBLE_LOCK: with SCROBBLE_LOCK:
ops = [ # ops = [
DB['scrobbles'].insert().values( # DB['scrobbles'].insert().values(
**scrobble_dict_to_db(s,update_album=update_album,dbconn=dbconn) # **scrobble_dict_to_db(s,update_album=update_album,dbconn=dbconn)
) for s in scrobbleslist # ) for s in scrobbleslist
] # ]
success,errors = 0,0 success, exists, errors = 0, 0, 0
for op in ops:
for s in scrobbleslist:
scrobble_entry = scrobble_dict_to_db(s,update_album=update_album,dbconn=dbconn)
try: try:
dbconn.execute(op) dbconn.execute(DB['scrobbles'].insert().values(
**scrobble_entry
))
success += 1 success += 1
except sql.exc.IntegrityError as e: except sql.exc.IntegrityError:
errors += 1 # get existing scrobble
result = dbconn.execute(DB['scrobbles'].select().where(
DB['scrobbles'].c.timestamp == scrobble_entry['timestamp']
)).first()
print("Existing",result)
if result.track_id == scrobble_entry['track_id']:
exists += 1
else:
errors += 1
# TODO check if actual duplicate
if errors > 0: log(f"{errors} Scrobbles have not been written to database!",color='red') if errors > 0: log(f"{errors} Scrobbles have not been written to database!",color='red')
return success,errors if exists > 0: log(f"{exists} Scrobbles have not been written to database (duplicate)", color='orange')
return success, exists, errors
@connection_provider @connection_provider
def delete_scrobble(scrobble_id,dbconn=None): def delete_scrobble(scrobble_id,dbconn=None):