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

Further adjustments to compliant API

This commit is contained in:
Krateng 2019-05-15 10:11:41 +02:00
parent 9f5a705504
commit baa7552478
2 changed files with 24 additions and 10 deletions

View File

@ -71,13 +71,21 @@ def handle(path,keys,headers,auth):
print("Response: " + str(result)) print("Response: " + str(result))
return result return result
def scrobbletrack(artiststr,titlestr,timestamp):
try:
(artists,title) = cla.fullclean(artiststr,titlestr)
database.createScrobble(artists,title,timestamp)
database.sync()
except:
raise ScrobblingException()
class BadAuthException(Exception): pass class BadAuthException(Exception): pass
class InvalidAuthException(Exception): pass class InvalidAuthException(Exception): pass
class InvalidMethodException(Exception): pass class InvalidMethodException(Exception): pass
class InvalidSessionKey(Exception): pass class InvalidSessionKey(Exception): pass
class MalformedJSONException(Exception): pass class MalformedJSONException(Exception): pass
class ScrobblingException(Exception): pass
class APIHandler: class APIHandler:
# make these classes singletons # make these classes singletons
@ -111,7 +119,8 @@ class GNUFM2(APIHandler):
BadAuthException:(400,{"error":6,"message":"Requires authentication"}), BadAuthException:(400,{"error":6,"message":"Requires authentication"}),
InvalidAuthException:(401,{"error":4,"message":"Invalid credentials"}), InvalidAuthException:(401,{"error":4,"message":"Invalid credentials"}),
InvalidMethodException:(200,{"error":3,"message":"Invalid method"}), InvalidMethodException:(200,{"error":3,"message":"Invalid method"}),
InvalidSessionKey:(403,{"error":9,"message":"Invalid session key"}) InvalidSessionKey:(403,{"error":9,"message":"Invalid session key"}),
ScrobblingException:(500,{"error":8,"message":"Operation failed"})
} }
def get_method(self,pathnodes,keys): def get_method(self,pathnodes,keys):
@ -144,17 +153,19 @@ class GNUFM2(APIHandler):
else: else:
if "track" in keys and "artist" in keys: if "track" in keys and "artist" in keys:
artiststr,titlestr = keys["artist"], keys["track"] artiststr,titlestr = keys["artist"], keys["track"]
(artists,title) = cla.fullclean(artiststr,titlestr) #(artists,title) = cla.fullclean(artiststr,titlestr)
timestamp = int(keys["timestamp"]) timestamp = int(keys["timestamp"])
database.createScrobble(artists,title,timestamp) #database.createScrobble(artists,title,timestamp)
scrobbletrack(artiststr,titlestr,timestamp)
return 200,{"scrobbles":{"@attr":{"ignored":0}}} return 200,{"scrobbles":{"@attr":{"ignored":0}}}
else: else:
for num in range(50): for num in range(50):
if "track[" + str(num) + "]" in keys: if "track[" + str(num) + "]" in keys:
artiststr,titlestr = keys["artist[" + str(num) + "]"], keys["track[" + str(num) + "]"] artiststr,titlestr = keys["artist[" + str(num) + "]"], keys["track[" + str(num) + "]"]
(artists,title) = cla.fullclean(artiststr,titlestr) #(artists,title) = cla.fullclean(artiststr,titlestr)
timestamp = int(keys["timestamp[" + str(num) + "]"]) timestamp = int(keys["timestamp[" + str(num) + "]"])
database.createScrobble(artists,title,timestamp) #database.createScrobble(artists,title,timestamp)
scrobbletrack(artiststr,titlestr,timestamp)
return 200,{"scrobbles":{"@attr":{"ignored":0}}} return 200,{"scrobbles":{"@attr":{"ignored":0}}}
@ -170,7 +181,8 @@ class LBrnz1(APIHandler):
BadAuthException:(401,{"code":401,"error":"You need to provide an Authorization header."}), BadAuthException:(401,{"code":401,"error":"You need to provide an Authorization header."}),
InvalidAuthException:(401,{"code":401,"error":"Bad Auth"}), InvalidAuthException:(401,{"code":401,"error":"Bad Auth"}),
InvalidMethodException:(200,{"code":200,"error":"Invalid Method"}), InvalidMethodException:(200,{"code":200,"error":"Invalid Method"}),
MalformedJSONException:(400,{"code":400,"error":"Invalid JSON document submitted."}) MalformedJSONException:(400,{"code":400,"error":"Invalid JSON document submitted."}),
ScrobblingException:(500,{"code":500,"error":"Unspecified server error."})
} }
def get_method(self,pathnodes,keys): def get_method(self,pathnodes,keys):
@ -192,12 +204,13 @@ class LBrnz1(APIHandler):
for listen in payload: for listen in payload:
metadata = listen["track_metadata"] metadata = listen["track_metadata"]
artiststr, titlestr = metadata["artist_name"], metadata["track_name"] artiststr, titlestr = metadata["artist_name"], metadata["track_name"]
(artists,title) = cla.fullclean(artiststr,titlestr) #(artists,title) = cla.fullclean(artiststr,titlestr)
try: try:
timestamp = int(listen["listened_at"]) timestamp = int(listen["listened_at"])
except: except:
timestamp = int(datetime.datetime.now(tz=datetime.timezone.utc).timestamp()) timestamp = int(datetime.datetime.now(tz=datetime.timezone.utc).timestamp())
database.createScrobble(artists,title,timestamp) #database.createScrobble(artists,title,timestamp)
scrobbletrack(artiststr,titlestr,timestamp)
return 200,{"code":200,"status":"ok"} return 200,{"code":200,"status":"ok"}
else: else:
return 200,{"code":200,"status":"ok"} return 200,{"code":200,"status":"ok"}

View File

@ -649,7 +649,8 @@ def post_scrobble():
#if (time - lastsync) > 3600: #if (time - lastsync) > 3600:
# sync() # sync()
sync() #let's just always sync, not like one filesystem access every three minutes is a problem and it avoids lost tracks when we lose power sync()
#always sync, one filesystem access every three minutes shouldn't matter
return {"status":"success","track":trackdict} return {"status":"success","track":trackdict}