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

Implemented artist metadata interface

This commit is contained in:
Krateng 2020-07-25 19:34:41 +02:00
parent b6a66ff2ed
commit fe106a3227
5 changed files with 45 additions and 9 deletions

View File

@ -9,11 +9,18 @@ HOST = "::" # You most likely want either :: for IPv6 or 0.0.0.0 for IPv4 here
[Third Party Services]
# whether to use metadata providers
METADATA_LASTFM = true
METADATA_SPOTIFY = true
# whether to proxy scrobble to other services
SCROBBLE_LASTFM = false
LASTFM_API_KEY = "ASK" # 'ASK' signifies that the user has not yet indicated to not use any key at all.
LASTFM_API_SECRET = "ASK"
FANARTTV_API_KEY = "ASK"
SPOTIFY_API_ID = "ASK"
SPOTIFY_API_SECRET = "ASK"
CACHE_EXPIRE_NEGATIVE = 30 # after how many days negative results should be tried again
CACHE_EXPIRE_POSITIVE = 300 # after how many days positive results should be refreshed
THUMBOR_SERVER = None

View File

@ -31,6 +31,10 @@ def get_image_track_all(track):
for service in services["metadata"]:
res = service.get_image_track(track)
if res is not None: return res
def get_image_artist_all(artist):
for service in services["metadata"]:
res = service.get_image_artist(artist)
if res is not None: return res
@ -135,13 +139,33 @@ class MetadataInterface(GenericInterface,abstract=True):
responsedata = response.read()
if self.metadata["response_type"] == "json":
data = json.loads(responsedata)
return self.metadata_parse_response(data)
return self.metadata_parse_response_track(data)
def get_image_artist(self,artist):
artiststring = urllib.parse.quote(artist)
response = urllib.request.urlopen(
self.metadata["artisturl"].format(artist=artiststring,**self.settings)
)
responsedata = response.read()
if self.metadata["response_type"] == "json":
data = json.loads(responsedata)
return self.metadata_parse_response_artist(data)
# default function to parse response by descending down nodes
# override if more complicated
def metadata_parse_response(self,data):
def metadata_parse_response_artist(self,data):
res = data
for node in self.metadata["response_parse_tree"]:
for node in self.metadata["response_parse_tree_artist"]:
try:
res = res[node]
except:
return None
return res
def metadata_parse_response_track(self,data):
res = data
for node in self.metadata["response_parse_tree_track"]:
try:
res = res[node]
except:

View File

@ -20,12 +20,14 @@ class LastFM(MetadataInterface, ProxyScrobbleInterface):
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"],
"response_parse_tree_track": ["track","album","image",3,"#text"],
"required_settings": ["apikey"],
"activated_setting": "METADATA_LASTFM"
}
def get_image_artist(self,artist):
return None
# lastfm doesn't provide artist images
def proxyscrobble_parse_response(self,data):
return data.attrib.get("status") == "ok" and data.find("scrobbles").attrib.get("ignored") == "0"

View File

@ -13,8 +13,10 @@ class Spotify(MetadataInterface):
metadata = {
"trackurl": "https://api.spotify.com/v1/search?q={artist}%20{title}&type=track&access_token={token}",
"artisturl": "https://api.spotify.com/v1/search?q={artist}&type=artist&access_token={token}",
"response_type":"json",
"response_parse_tree": ["tracks","items",0,"album","images",0,"url"],
"response_parse_tree_track": ["tracks","items",0,"album","images",0,"url"],
"response_parse_tree_artist": ["artists","items",0,"images",0,"url"],
"required_settings": ["apiid","secret"],
"activated_setting": "METADATA_SPOTIFY"
}

View File

@ -14,8 +14,8 @@ from doreah import caching
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 .external import api_request_track, api_request_artist
from .thirdparty import get_image_track_all, get_image_artist_all
from .__pkginfo__ import version
from . import globalconf
from .globalconf import datadir
@ -348,7 +348,8 @@ def getArtistImage(artist,fast=False):
if fast: return "/image?artist=" + urllib.parse.quote(artist)
# non-fast lookup (esentially only the resolver lookup)
result = api_request_artist(artist=artist)
# result = api_request_artist(artist=artist)
result = get_image_artist_all(artist)
# cache results (even negative ones)
#cachedArtists[artist] = result