maloja/maloja/thirdparty/musicbrainz.py

65 lines
1.6 KiB
Python
Raw Normal View History

Refactoring (#83) * Merge isinstance calls * Inline variable that is immediately returned * Replace set() with comprehension * Replace assignment with augmented assignment * Remove unnecessary else after guard condition * Convert for loop into list comprehension * Replace unused for index with underscore * Merge nested if conditions * Convert for loop into list comprehension * Convert for loop into set comprehension * Remove unnecessary else after guard condition * Replace if statements with if expressions * Simplify sequence comparison * Replace multiple comparisons with in operator * Merge isinstance calls * Merge nested if conditions * Add guard clause * Merge duplicate blocks in conditional * Replace unneeded comprehension with generator * Inline variable that is immediately returned * Remove unused imports * Replace unneeded comprehension with generator * Remove unused imports * Remove unused import * Inline variable that is immediately returned * Swap if/else branches and remove unnecessary else * Use str.join() instead of for loop * Multiple refactors - Remove redundant pass statement - Hoist repeated code outside conditional statement - Swap if/else to remove empty if body * Inline variable that is immediately returned * Simplify generator expression * Replace if statement with if expression * Multiple refactoring - Replace range(0, x) with range(x) - Swap if/else branches - Remove unnecessary else after guard condition * Use str.join() instead of for loop * Hoist repeated code outside conditional statement * Use str.join() instead of for loop * Inline variables that are immediately returned * Merge dictionary assignment with declaration * Use items() to directly unpack dictionary values * Extract dup code from methods into a new one
2021-10-19 15:58:24 +03:00
from . import MetadataInterface
import urllib.parse, urllib.request
import json
import time
import threading
2021-12-26 01:43:34 +03:00
from ..__pkginfo__ import USER_AGENT
class MusicBrainz(MetadataInterface):
name = "MusicBrainz"
identifier = "musicbrainz"
# musicbrainz is rate-limited
lock = threading.Lock()
2021-12-26 01:43:34 +03:00
useragent = USER_AGENT
settings = {
}
metadata = {
"response_type":"json",
"response_parse_tree_track": ["images",0,"thumbnails","500"],
"required_settings": [],
}
def get_image_artist(self,artist):
return None
# not supported
def get_image_track(self,track):
self.lock.acquire()
try:
artists, title = track
artiststring = ", ".join(artists) #Join artists collection into string
titlestring = title
querystr = urllib.parse.urlencode({
"fmt":"json",
"query":"{title} {artist}".format(artist=artiststring,title=titlestring)
})
req = urllib.request.Request(**{
"url":"https://musicbrainz.org/ws/2/release?" + querystr,
"method":"GET",
"headers":{
"User-Agent":self.useragent
}
})
response = urllib.request.urlopen(req)
responsedata = response.read()
data = json.loads(responsedata)
mbid = data["releases"][0]["id"]
response = urllib.request.urlopen(
"https://coverartarchive.org/release/{mbid}?fmt=json".format(mbid=mbid)
)
responsedata = response.read()
data = json.loads(responsedata)
imgurl = self.metadata_parse_response_track(data)
if imgurl is not None: imgurl = self.postprocess_url(imgurl)
return imgurl
2022-04-24 21:47:17 +03:00
except Exception:
return None
finally:
time.sleep(2)
self.lock.release()