2021-10-24 11:15:47 +00:00
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
from config import plugins
|
2022-11-23 12:11:36 +01:00
|
|
|
|
2021-10-24 11:15:47 +00:00
|
|
|
from .funkwhale_startup import PLUGIN
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MalojaException(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@plugins.register_hook(plugins.LISTENING_CREATED, PLUGIN)
|
|
|
|
|
def submit_listen(listening, conf, **kwargs):
|
|
|
|
|
server_url = conf["server_url"]
|
|
|
|
|
api_key = conf["api_key"]
|
|
|
|
|
if not server_url or not api_key:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
logger = PLUGIN["logger"]
|
|
|
|
|
logger.info("Submitting listening to Majola at %s", server_url)
|
|
|
|
|
payload = get_payload(listening, api_key)
|
|
|
|
|
logger.debug("Majola payload: %r", payload)
|
|
|
|
|
url = server_url.rstrip("/") + "/apis/mlj_1/newscrobble"
|
|
|
|
|
session = plugins.get_session()
|
2023-01-20 20:35:18 +01:00
|
|
|
response = session.post(url, json=payload)
|
2021-10-24 11:15:47 +00:00
|
|
|
response.raise_for_status()
|
|
|
|
|
details = json.loads(response.text)
|
|
|
|
|
if details["status"] == "success":
|
|
|
|
|
logger.info("Majola listening submitted successfully")
|
|
|
|
|
else:
|
|
|
|
|
raise MalojaException(response.text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_payload(listening, api_key):
|
|
|
|
|
track = listening.track
|
|
|
|
|
payload = {
|
|
|
|
|
"key": api_key,
|
2023-01-20 20:35:18 +01:00
|
|
|
"artists": [track.artist.name],
|
2021-10-24 11:15:47 +00:00
|
|
|
"title": track.title,
|
|
|
|
|
"time": int(listening.creation_date.timestamp()),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if track.album:
|
2023-01-20 20:35:18 +01:00
|
|
|
if track.album.title:
|
|
|
|
|
payload["album"] = track.album.title
|
2021-10-24 11:15:47 +00:00
|
|
|
|
|
|
|
|
return payload
|