diff --git a/maloja/thirdparty/__init__.py b/maloja/thirdparty/__init__.py index b9c5de6..78b08f0 100644 --- a/maloja/thirdparty/__init__.py +++ b/maloja/thirdparty/__init__.py @@ -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 * diff --git a/maloja/thirdparty/lastfm.py b/maloja/thirdparty/lastfm.py index f49e70a..8b1975f 100644 --- a/maloja/thirdparty/lastfm.py +++ b/maloja/thirdparty/lastfm.py @@ -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" diff --git a/maloja/utilities.py b/maloja/utilities.py index 48f7a82..936fd36 100644 --- a/maloja/utilities.py +++ b/maloja/utilities.py @@ -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