Skip to content

Commit

Permalink
fixes for music artwork
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt committed Nov 30, 2016
1 parent 94c0a9f commit 66fde7a
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 83 deletions.
2 changes: 1 addition & 1 deletion addon.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="script.module.skin.helper.artutils"
name="Skin Helper Artwork and metadata Module"
version="1.0.10"
version="1.0.11"
provider-name="marcelveldt">
<requires>
<import addon="xbmc.python" version="2.24.0"/>
Expand Down
143 changes: 101 additions & 42 deletions lib/helpers/mbrainz.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from simplecache import use_cache
import xbmcvfs
import xbmcaddon
import xbmc


class MusicBrainz(object):
Expand All @@ -36,53 +37,28 @@ def __init__(self, simplecache=None):
del addon
self.mbrainz = mbrainz

@use_cache(60)
#@use_cache(60)
def search(self, artist, album, track):
'''get musicbrainz id by query of artist, album and/or track'''
mb_albums = []
albumid = ""
artistid = ""
if artist and album:
mb_albums = self.mbrainz.search_release_groups(query=album,
limit=3, offset=None, strict=False, artist=artist)
elif not mb_albums and artist and track:
mb_albums = self.mbrainz.search_recordings(query=track,
limit=3, offset=None, strict=False, artist=artist)
elif not mb_albums and artist and album:
# use albumname as track
track = album
mb_albums = self.mbrainz.search_recordings(query=track,
limit=3, offset=None, strict=False, artist=artist)
try:

if mb_albums and mb_albums.get("release-group-list"):
mb_albums = mb_albums.get("release-group-list")
elif mb_albums and mb_albums.get("recording-list"):
mb_albums = mb_albums.get("recording-list")
# lookup with artist and album (preferred method)
if artist and album:
artistid, albumid = self.search_release_group_match(artist, album)

# lookup with artist and track (if no album provided)
if not (artistid and albumid) and artist and track:
artistid, albumid = self.search_recording_match(artist, track)

# last resort: lookup with trackname as album
if not (artistid and albumid) and artist and track:
artistid, albumid = self.search_release_group_match(artist, track)

except Exception as exc:
log_msg("Error in musicbrainz.search: %s" % str(exc), xbmc.LOGWARNING)

for mb_album in mb_albums:
if artistid:
break
if mb_album and isinstance(mb_album, dict):
albumid = mb_album.get("id", "")
if mb_album.get("artist-credit"):
for mb_artist in mb_album.get("artist-credit"):
if isinstance(mb_artist, dict) and mb_artist.get("artist", ""):
# safety check - only allow exact artist match
foundartist = mb_artist["artist"].get("name")
foundartist = foundartist.encode("utf-8").decode("utf-8")
if foundartist and get_compare_string(foundartist) == get_compare_string(artist):
artistid = mb_artist.get("artist").get("id")
break
if not artistid and mb_artist["artist"].get("alias-list"):
alias_list = [get_compare_string(item["alias"])
for item in mb_artist["artist"]["alias-list"]]
if get_compare_string(artist) in alias_list:
artistid = mb_artist.get("artist").get("id")
break
else:
log_msg("mb_album not a dict! -- %s" % mb_album)
if not artistid:
albumid = ""
return (artistid, albumid)

def get_artist_id(self, artist, album, track):
Expand Down Expand Up @@ -128,7 +104,7 @@ def get_albuminfo(self, mbalbumid):
if not item in result["genre"] and int(tag["count"]) > 4:
result["genre"].append(item)
except Exception as exc:
log_msg("Error in musicbrainz - get album details: %s" % str(exc))
log_msg("Error in musicbrainz - get album details: %s" % str(exc), xbmc.LOGWARNING)
return result

@staticmethod
Expand All @@ -139,3 +115,86 @@ def get_albumthumb(albumid):
if xbmcvfs.exists(url):
thumb = url
return thumb

def search_release_group_match(self, artist, album):
'''try to get a match on releasegroup for given artist/album combi'''
artistid = ""
albumid = ""
mb_albums = self.mbrainz.search_release_groups(query=album,
limit=3, offset=None, strict=False, artist=artist)
if mb_albums and mb_albums.get("release-group-list"):
for mb_album in mb_albums["release-group-list"]:
if artistid and albumid:
break
if mb_album and isinstance(mb_album, dict):
if mb_album.get("artist-credit"):
artistid = self.match_artistcredit(mb_album["artist-credit"], artist)
if artistid:
albumid = mb_album.get("id", "")
break
return (artistid, albumid)

@staticmethod
def match_artistcredit(artist_credit, artist):
'''find match for artist in artist-credits'''
artistid = ""
for mb_artist in artist_credit:
if artistid:
break
if isinstance(mb_artist, dict) and mb_artist.get("artist", ""):
# safety check - only allow exact artist match
foundartist = mb_artist["artist"].get("name")
foundartist = foundartist.encode("utf-8").decode("utf-8")
if foundartist and get_compare_string(foundartist) == get_compare_string(artist):
artistid = mb_artist.get("artist").get("id")
break
if not artistid and mb_artist["artist"].get("alias-list"):
alias_list = [get_compare_string(item["alias"])
for item in mb_artist["artist"]["alias-list"]]
if get_compare_string(artist) in alias_list:
artistid = mb_artist.get("artist").get("id")
break
for item in artist.split("&"):
item = get_compare_string(item)
if item in alias_list or item in get_compare_string(foundartist):
artistid = mb_artist.get("artist").get("id")
break
return artistid


def search_recording_match(self, artist, track):
'''try to get the releasegroup (album) for the given artist/track combi, various-artists compilations are ignored'''
artistid = ""
albumid = ""
mb_albums = self.mbrainz.search_recordings(query=track,
limit=20, offset=None, strict=False, artist=artist)
if mb_albums and mb_albums.get("recording-list"):
for mb_recording in mb_albums["recording-list"]:
if albumid and artistid:
break
if mb_recording and isinstance(mb_recording, dict):
# look for match on artist
if mb_recording.get("artist-credit"):
artistid = self.match_artistcredit(mb_recording["artist-credit"], artist)
# if we have a match on artist, look for match in release list
if artistid:
if mb_recording.get("release-list"):
for mb_release in mb_recording["release-list"]:
if mb_release.get("artist-credit"):
if mb_release["artist-credit"][0].get("id","") == artistid:
albumid = mb_release["release-group"]["id"]
break
else:
continue
if mb_release.get("artist-credit-phrase","") == 'Various Artists':
continue
# grab release group details to make sure we're not looking at some various artists compilation
mb_album = self.mbrainz.get_release_group_by_id(
mb_release["release-group"]["id"], includes=["artist-credits"])
mb_album = mb_album["release-group"]
if mb_album.get("artist-credit"):
artistid = self.match_artistcredit(mb_album["artist-credit"], artist)
if artistid:
albumid = mb_release["release-group"]["id"]
break
return (artistid, albumid)
Loading

0 comments on commit 66fde7a

Please sign in to comment.