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

Implemented track image fetcher interface

This commit is contained in:
Krateng 2020-07-25 17:54:01 +02:00
parent a097d34f10
commit 4c30ff5fa2
3 changed files with 44 additions and 2 deletions

View File

@ -7,6 +7,7 @@
# pls don't sue me
import xml.etree.ElementTree as ElementTree
import json
import urllib.parse, urllib.request
from doreah.settings import get_settings
from doreah.logging import log
@ -25,6 +26,10 @@ def proxy_scrobble_all(artists,title,timestamp):
for service in services["proxyscrobble"]:
service.scrobble(artists,title,timestamp)
def get_image_track_all(track):
for service in services["metadata"]:
res = service.get_image_track(track)
if res is not None: return res
@ -113,6 +118,30 @@ class MetadataInterface(GenericInterface,abstract=True):
get_settings(self.metadata["activated_setting"])
)
def get_image_track(self,track):
artists, title = track
artiststring = urllib.parse.quote(", ".join(artists))
titlestring = urllib.parse.quote(title)
response = urllib.request.urlopen(
self.metadata["trackurl"].format(artist=artiststring,title=titlestring,**self.settings)
)
responsedata = response.read()
if self.metadata["response_type"] == "json":
data = json.loads(responsedata)
return self.metadata_parse_response(data)
# default function to parse response by descending down nodes
# override if more complicated
def metadata_parse_response(self,data):
res = data
for node in self.metadata["response_parse_tree"]:
try:
res = res[node]
except:
return None
return res
@ -127,5 +156,7 @@ def utf(st):
### actually create everything
__all__ = ["lastfm"] # list them for now, do this dynamically later
__all__ = [
"lastfm"
]
from . import *

View File

@ -17,6 +17,15 @@ class LastFM(MetadataInterface, ProxyScrobbleInterface):
"required_settings": ["apikey","sk","secret"],
"activated_setting": "SCROBBLE_LASTFM"
}
metadata = {
"trackurl": "https://ws.audioscrobbler.com/2.0/?method=track.getinfo&track={title}&artist={artist}&api_key={apikey}&format=json",
"response_type":"json",
"response_parse_tree": ["track","album","image",3,"#text"],
"required_settings": ["apikey"],
"activated_setting": "METADATA_LASTFM"
}
def proxyscrobble_parse_response(self,data):
return data.attrib.get("status") == "ok" and data.find("scrobbles").attrib.get("ignored") == "0"

View File

@ -15,6 +15,7 @@ from doreah.logging import log
from doreah.regular import yearly, daily
from .external import api_request_track, api_request_artist
from .thirdparty import get_image_track_all
from .__pkginfo__ import version
from . import globalconf
from .globalconf import datadir
@ -292,7 +293,8 @@ def getTrackImage(artists,title,fast=False):
if fast: return "/image?title=" + urllib.parse.quote(title) + "&" + "&".join(["artist=" + urllib.parse.quote(a) for a in artists])
# non-fast lookup (esentially only the resolver lookup)
result = api_request_track((artists,title))
# result = api_request_track((artists,title))
result = get_image_track_all((artists,title))
# cache results (even negative ones)
#cachedTracks[(frozenset(artists),title)] = result