Adapted listenbrainz API to new architecture

This commit is contained in:
krateng 2022-04-06 22:05:41 +02:00
parent de18ecff26
commit 08bd352641
5 changed files with 20 additions and 12 deletions

View File

@ -20,8 +20,9 @@ def api_key_correct(request,args,kwargs):
elif "apikey" in kwargs:
apikey = kwargs.pop("apikey")
else: return False
if checkAPIkey(apikey):
client = [c for c in apikeystore if apikeystore[c]==apikey][0]
client = apikeystore.check_and_identify_key(apikey)
if client:
return {'client':client}
else:
return False

View File

@ -89,12 +89,10 @@ class APIHandler:
return method(path,keys)
def scrobble(self,artiststr,titlestr,time=None,duration=None,album=None):
logmsg = f"API {self.__apiname__} receiving scrobble: ARTISTS: {artiststr}, TRACK: {titlestr}"
log(logmsg)
def scrobble(self,rawscrobble,client=None):
# fixing etc is handled by the main scrobble function
try:
database.incoming_scrobble(artists=[artiststr],title=titlestr,time=time,duration=duration,album=album)
return database.incoming_scrobble(rawscrobble,api=self.__apiname__,client=client)
except:
raise ScrobblingException()

View File

@ -2,7 +2,7 @@ from ._base import APIHandler
from ._exceptions import *
from .. import database
import datetime
from ._apikeys import checkAPIkey
from ._apikeys import apikeystore
from ..globalconf import malojaconfig
@ -37,7 +37,9 @@ class Listenbrainz(APIHandler):
except:
raise BadAuthException()
if not checkAPIkey(token):
client = apikeystore.check_and_identify_key(token)
if not client:
raise InvalidAuthException()
try:
@ -60,7 +62,11 @@ class Listenbrainz(APIHandler):
except:
raise MalformedJSONException()
self.scrobble(artiststr,titlestr,timestamp)
self.scrobble({
'track_artists':[artiststr],
'track_title':titlestr,
'scrobble_time':timestamp
},client=client)
return 200,{"status":"ok"}

View File

@ -248,12 +248,15 @@ def post_scrobble(artist:Multi=None,auth_result=None,**keys):
'scrobble_time':int(keys.get('time')) if (keys.get('time') is not None) else None
}
# for logging purposes, don't pass values that we didn't actually supply
rawscrobble = {k:rawscrobble[k] for k in rawscrobble if rawscrobble[k]}
return database.incoming_scrobble(
rawscrobble,
client='browser' if auth_result.get('doreah_native_auth_check') else auth_result.get('client'),
api='native/v1',
fix=(keys.get("nofix") is None)
)
# TODO: malojaclient needs to be converted to proper argument in doreah

View File

@ -86,13 +86,13 @@ coa = CollectorAgent()
##
##
def incoming_scrobble(rawscrobble,fix=True,client=None,dbconn=None):
def incoming_scrobble(rawscrobble,fix=True,client=None,api=None,dbconn=None):
if (not "track_artists" in rawscrobble) or (len(rawscrobble['track_artists']) == 0) or (not "track_title" in rawscrobble):
log(f"Incoming scrobble {rawscrobble} [Source: {client}] is not valid")
return {"status":"failure"}
log(f"Incoming scrobble [{client}]: {rawscrobble}")
log(f"Incoming scrobble [Client: {client} | API: {api}]: {rawscrobble}")
# raw scrobble to processed info
scrobbleinfo = {**rawscrobble}