Skip to content

Commit

Permalink
Add album art fetching from Musicbrainz, fix GH-265
Browse files Browse the repository at this point in the history
  • Loading branch information
krateng committed Nov 1, 2023
1 parent 7910ee3 commit 139de02
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions dev/releases/3.2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ minor_release_name: "Nicole"
3.2.1:
notes:
- "[Feature] Added setting for custom week offset"
- "[Feature] Added Musicbrainz album art fetching"
- "[Bugfix] Fixed album entity rows being marked as track entity rows"
- "[Bugfix] Fixed scrobbling of tracks when all artists have been removed by server parsing"
- "[Bugfix] Fixed Spotify import of multiple files"
Expand Down
2 changes: 1 addition & 1 deletion maloja/pkg_global/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def find_good_folder(datatype,configobject):
"display_art_icons":(tp.Boolean(), "Display Album/Artist Icons", True),
"default_album_artist":(tp.String(), "Default Albumartist", "Various Artists"),
"use_album_artwork_for_tracks":(tp.Boolean(), "Use Album Artwork for tracks", True),
"fancy_placeholder_art":(tp.Boolean(), "Use fancy placeholder artwork",True),
"fancy_placeholder_art":(tp.Boolean(), "Use fancy placeholder artwork",False),
"discourage_cpu_heavy_stats":(tp.Boolean(), "Discourage CPU-heavy stats", False, "Prevent visitors from mindlessly clicking on CPU-heavy options. Does not actually disable them for malicious actors!"),
"use_local_images":(tp.Boolean(), "Use Local Images", True),
#"local_image_rotate":(tp.Integer(), "Local Image Rotate", 3600),
Expand Down
62 changes: 59 additions & 3 deletions maloja/thirdparty/musicbrainz.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ class MusicBrainz(MetadataInterface):
lock = threading.Lock()
useragent = USER_AGENT


thumbnailsize_order = ['500','large','1200','250','small']

settings = {
}

metadata = {
"response_type":"json",
"response_parse_tree_track": ["images",0,"thumbnails","500"],
"required_settings": [],
}

Expand All @@ -27,7 +29,57 @@ def get_image_artist(self,artist):
# not supported

def get_image_album(self,album):
return None
self.lock.acquire()
try:
artists, title = album
searchstr = f'release:"{title}"'
for artist in artists:
searchstr += f' artist:"{artist}"'
querystr = urllib.parse.urlencode({
"fmt":"json",
"query":searchstr
})
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)
entity = data["releases"][0]
coverartendpoint = "release"
while True:
mbid = entity["id"]
try:
response = urllib.request.urlopen(
f"https://coverartarchive.org/{coverartendpoint}/{mbid}?fmt=json"
)
responsedata = response.read()
data = json.loads(responsedata)
thumbnails = data['images'][0]['thumbnails']
for size in self.thumbnailsize_order:
if thumbnails.get(size) is not None:
imgurl = thumbnails.get(size)
continue
except:
imgurl = None
if imgurl is None:
entity = entity["release-group"]
# this will raise an error so we don't stay in the while loop forever
coverartendpoint = "release-group"
continue

imgurl = self.postprocess_url(imgurl)
return imgurl

except Exception:
return None
finally:
time.sleep(2)
self.lock.release()

def get_image_track(self,track):
self.lock.acquire()
Expand Down Expand Up @@ -60,7 +112,11 @@ def get_image_track(self,track):
)
responsedata = response.read()
data = json.loads(responsedata)
imgurl = self.metadata_parse_response_track(data)
thumbnails = data['images'][0]['thumbnails']
for size in self.thumbnailsize_order:
if thumbnails.get(size) is not None:
imgurl = thumbnails.get(size)
continue
except:
imgurl = None
if imgurl is None:
Expand Down

0 comments on commit 139de02

Please sign in to comment.