Added a Maloja plugin to submit listenings (!1383)
This commit is contained in:
parent
568c59599b
commit
fadbb9a7ab
|
@ -88,6 +88,7 @@ sys.path.append(FUNKWHALE_PLUGINS_PATH)
|
||||||
CORE_PLUGINS = [
|
CORE_PLUGINS = [
|
||||||
"funkwhale_api.contrib.scrobbler",
|
"funkwhale_api.contrib.scrobbler",
|
||||||
"funkwhale_api.contrib.listenbrainz",
|
"funkwhale_api.contrib.listenbrainz",
|
||||||
|
"funkwhale_api.contrib.maloja",
|
||||||
]
|
]
|
||||||
|
|
||||||
LOAD_CORE_PLUGINS = env.bool("FUNKWHALE_LOAD_CORE_PLUGINS", default=True)
|
LOAD_CORE_PLUGINS = env.bool("FUNKWHALE_LOAD_CORE_PLUGINS", default=True)
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
import json
|
||||||
|
|
||||||
|
from config import plugins
|
||||||
|
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()
|
||||||
|
response = session.post(url, payload)
|
||||||
|
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,
|
||||||
|
"artists": track.artist.name,
|
||||||
|
"title": track.title,
|
||||||
|
"time": int(listening.creation_date.timestamp()),
|
||||||
|
}
|
||||||
|
|
||||||
|
if track.album:
|
||||||
|
payload["album"] = track.album.title
|
||||||
|
|
||||||
|
return payload
|
|
@ -0,0 +1,14 @@
|
||||||
|
from config import plugins
|
||||||
|
|
||||||
|
PLUGIN = plugins.get_plugin_config(
|
||||||
|
name="maloja",
|
||||||
|
label="Maloja",
|
||||||
|
description="A plugin that allows you to submit your listens to your Maloja server.",
|
||||||
|
homepage="https://docs.funkwhale.audio/users/builtinplugins.html#maloja-plugin",
|
||||||
|
version="0.1",
|
||||||
|
user=True,
|
||||||
|
conf=[
|
||||||
|
{"name": "server_url", "type": "text", "label": "Maloja server URL"},
|
||||||
|
{"name": "api_key", "type": "text", "label": "Your Maloja API key"},
|
||||||
|
],
|
||||||
|
)
|
|
@ -0,0 +1 @@
|
||||||
|
Added a Maloja plugin to submit listenings
|
Loading…
Reference in New Issue