diff --git a/maloja/apis/native_v1.py b/maloja/apis/native_v1.py index aea7710..7a90501 100644 --- a/maloja/apis/native_v1.py +++ b/maloja/apis/native_v1.py @@ -82,6 +82,14 @@ errors = { 'desc':"This entity does not exist in the database." } }), + database.exceptions.DuplicateTimestamp: lambda e: (409,{ + "status":"error", + "error":{ + 'type':'duplicate_timestamp', + 'value':e.rejected_scrobble, + 'desc':"A scrobble is already registered with this timestamp." + } + }), images.MalformedB64: lambda e: (400,{ "status":"failure", "error":{ diff --git a/maloja/database/exceptions.py b/maloja/database/exceptions.py index 534dc1e..47abbd4 100644 --- a/maloja/database/exceptions.py +++ b/maloja/database/exceptions.py @@ -14,6 +14,13 @@ class ArtistExists(EntityExists): class AlbumExists(EntityExists): pass + +class DuplicateTimestamp(Exception): + def __init__(self,existing_scrobble,rejected_scrobble): + self.existing_scrobble = existing_scrobble + self.rejected_scrobble = rejected_scrobble + + class DatabaseNotBuilt(HTTPError): def __init__(self): super().__init__( diff --git a/maloja/database/sqldb.py b/maloja/database/sqldb.py index 5dce8a0..2641c66 100644 --- a/maloja/database/sqldb.py +++ b/maloja/database/sqldb.py @@ -328,7 +328,10 @@ def album_dict_to_db(info,dbconn=None): @connection_provider def add_scrobble(scrobbledict,update_album=False,dbconn=None): - add_scrobbles([scrobbledict],update_album=update_album,dbconn=dbconn) + _, e = add_scrobbles([scrobbledict],update_album=update_album,dbconn=dbconn) + if e > 0: + raise exc.DuplicateTimestamp(existing_scrobble=None,rejected_scrobble=scrobbledict) + # TODO: actually pass existing scrobble @connection_provider def add_scrobbles(scrobbleslist,update_album=False,dbconn=None):