def remote_coverart_list(artist_menu): log("Finding remote Cover ARTs") coverart_url = [] try: art = retrieve_fanarttv_json(artist_menu["musicbrainz_artistid"]) if not len(art) < 2: album_artwork = art[5]["artwork"] if album_artwork: for artwork in album_artwork: if artwork[ArtType.COVER]: album = { "artistl_id": artist_menu["local_id"], "artistd_id": artist_menu["musicbrainz_artistid"], "local_name": artist_menu["name"], "artist": artist_menu["name"], "musicbrainz_albumid": artwork["musicbrainz_albumid"], "size": 1000, "picture": artwork[ArtType.COVER], "thumb_art": artwork[ArtType.COVER] } coverart_url.append(album) # log( "cdart_url: %s " % cdart_url ) except Exception as e: log(e.message, xbmc.LOGERROR) print_exc() return coverart_url
def remote_cdart_list(artist_menu): log("Finding remote cdARTs") cdart_url = [] try: art = retrieve_fanarttv_json(artist_menu["musicbrainz_artistid"]) if not len(art) < 2: album_artwork = art[5]["artwork"] if album_artwork: for artwork in album_artwork: for cdart in artwork[ArtType.CDART]: album = { "artistl_id": artist_menu["local_id"], "artistd_id": artist_menu["musicbrainz_artistid"], "musicbrainz_albumid": artwork["musicbrainz_albumid"], "disc": cdart["disc"], "size": cdart["size"], "picture": cdart[ArtType.CDART], "thumb_art": cdart[ArtType.CDART] } try: album["local_name"] = album[ "artist"] = artist_menu["name"] except KeyError: album["local_name"] = album[ "artist"] = artist_menu["artist"] cdart_url.append(album) # log( "cdart_url: %s " % cdart_url) except Exception as e: log(e.message, xbmc.LOGERROR) print_exc() return cdart_url
def _report_hook(count, blocksize, totalsize): try: percent = int(float(count * blocksize * 100) / totalsize) if totalsize > 0 else 100 if percent < 1: percent = 1 if percent > 100: percent = 100 except Exception as ex: log(ex.message) percent = 1 if type_ in (ArtType.FANART, ArtType.CLEARLOGO, ArtType.THUMB, ArtType.BANNER): dialog_msg("update", percent=percent, line1="%s%s" % (__lng__(32038), cu.get_unicode(album["artist"])), background=background) else: dialog_msg("update", percent=percent, line1="%s%s" % (__lng__(32038), cu.get_unicode(album["artist"])), line2="%s%s" % (__lng__(32039), cu.get_unicode(album["title"])), background=background)
def first_check(all_artists, album_artists, background=False, update_db=False): log("Checking for artist match with fanart.tv - First Check", xbmc.LOGNOTICE) heading = __lng__(32187) album_artists_matched = [] all_artists_matched = [] d = datetime.utcnow() present_datecode = calendar.timegm(d.utctimetuple()) count = 0 dialog_msg("create", heading="", background=background) for artist in album_artists: log("Checking artist MBID: %s" % artist["musicbrainz_artistid"]) match = artist if artist["musicbrainz_artistid"] and (artist["has_art"] == "False" or update_db): match["has_art"] = check_art(artist["musicbrainz_artistid"]) elif not artist["musicbrainz_artistid"]: match["has_art"] = "False" else: match["has_art"] = artist["has_art"] album_artists_matched.append(match) dialog_msg("update", percent=percent_of(float(count), len(album_artists)), line1=heading, line2="", line3=__lng__(32049) % artist["name"], background=background) count += 1 log("Storing Album Artists List") store_lalist(album_artists_matched) if __cfg__.enable_all_artists() and all_artists: count = 0 for artist in all_artists: log("Checking artist MBID: %s" % artist["musicbrainz_artistid"]) match = artist if artist["musicbrainz_artistid"] and (artist["has_art"] == "False" or update_db): match["has_art"] = check_art(artist["musicbrainz_artistid"]) elif not artist["musicbrainz_artistid"]: match["has_art"] = "False" else: match["has_art"] = artist["has_art"] all_artists_matched.append(match) dialog_msg("update", percent=percent_of(float(count), len(all_artists)), line1=heading, line2="", line3=__lng__(32049) % artist["name"], background=background) count += 1 store_local_artist_table(all_artists_matched, background=background) store_fanarttv_datecode(present_datecode) dialog_msg("close", background=background) log("Finished First Check") return
def remote_fanart_list(artist_menu): log("Finding remote fanart") backgrounds = "" try: art = retrieve_fanarttv_json(artist_menu["musicbrainz_artistid"]) if not len(art) < 3: backgrounds = art[0]["backgrounds"] except Exception as e: log(e.message, xbmc.LOGERROR) print_exc() return backgrounds
def remote_banner_list(artist_menu): log("Finding remote music banners") banner = "" try: art = retrieve_fanarttv_json(artist_menu["musicbrainz_artistid"]) if not len(art) < 3: banner = art[4]["banner"] except Exception as e: log(e.message, xbmc.LOGERROR) print_exc() return banner
def remote_hdlogo_list(artist_menu): log("Finding remote hdlogo") hdlogo = "" try: art = retrieve_fanarttv_json(artist_menu["musicbrainz_artistid"]) if not len(art) < 3: hdlogo = art[3]["hdlogo"] except Exception as e: log(e.message, xbmc.LOGERROR) print_exc() return hdlogo
def retrieve_artist_details(artist_id): cdam_utils.log("jsonrpc_calls.py - Retrieving Artist Details") json_query = '{"jsonrpc": "2.0", "method": "AudioLibrary.GetArtistDetails", ' \ '"params": {"properties": ["musicbrainzartistid"], "artistid": %d}, "id": 1}' % artist_id json_artist_details = retrieve_json_dict(json_query, items='artistdetails', force_log=False) if json_artist_details: return json_artist_details else: return empty
def remote_artistthumb_list(artist_menu): log("Finding remote artistthumb") artistthumb = "" # If there is something in artist_menu["distant_id"] build cdart_url try: art = retrieve_fanarttv_json(artist_menu["musicbrainz_artistid"]) if not len(art) < 3: artistthumb = art[2]["artistthumb"] except Exception as e: log(e.message, xbmc.LOGERROR) print_exc() return artistthumb
def check_size(path, _type, _size_w, _size_h): # size check is disabled because currently fanart.tv always returns size=1000 # ref: https://forum.fanart.tv/viewtopic.php?f=4&t=403 file_name = get_filename(_type, path, "auto") source = os.path.join(path, file_name) if xbmcvfs.exists(source): log("size check n.a. in new fanart.tv API, returning False for %s" % source) return False else: log("size check n.a. in new fanart.tv API, returning True for %s" % source) return True
def retrieve_album_list(): cdam_utils.log("jsonrpc_calls.py - Retrieving Album List") json_query = '{"jsonrpc": "2.0", "method": "AudioLibrary.GetAlbums", ' \ '"params": { "limits": { "start": 0 }, ' \ '"properties": ["title", "artist", "musicbrainzalbumid", "musicbrainzalbumartistid"], ' \ '"sort": {"order":"ascending"}}, "id": 1}' json_albums = retrieve_json_dict(json_query, items='albums', force_log=False) if json_albums: return json_albums, len(json_albums) else: return empty, 0
def get_recognized(all_artists, album_artists, background=False): log("Checking for artist match with fanart.tv - Get Recognized artists", xbmc.LOGNOTICE) album_artists_matched = [] all_artists_matched = [] count = 0 dialog_msg("create", heading="", background=background) present_datecode = calendar.timegm(datetime.utcnow().utctimetuple()) new_artwork, data = check_fanart_new_artwork(present_datecode) if new_artwork: for artist in album_artists: log("Checking artist MBID: %s" % artist["musicbrainz_artistid"]) match = artist if match["musicbrainz_artistid"]: match["has_art"] = update_art(match["musicbrainz_artistid"], data, artist["has_art"]) album_artists_matched.append(match) dialog_msg("update", percent=percent_of(float(count), len(album_artists)), line1=__lng__(32185), line2="", line3=__lng__(32049) % artist["name"], background=background) count += 1 if __cfg__.enable_all_artists() and all_artists: count = 0 for artist in all_artists: log("Checking artist MBID: %s" % artist["musicbrainz_artistid"]) match = artist if match["musicbrainz_artistid"]: match["has_art"] = update_art( match["musicbrainz_artistid"], data, artist["has_art"]) all_artists_matched.append(match) dialog_msg("update", percent=percent_of(float(count), len(all_artists)), line1=__lng__(32185), line2="", line3=__lng__(32049) % artist["name"], background=background) count += 1 else: log("No new music artwork on fanart.tv", xbmc.LOGNOTICE) album_artists_matched = album_artists all_artists_matched = all_artists store_lalist(album_artists_matched) store_local_artist_table(all_artists_matched, background=background) store_fanarttv_datecode(present_datecode) dialog_msg("close", background=background) log("Finished Getting Recognized Artists") return all_artists_matched, album_artists_matched
def check_art(mbid): url = music_url_json % (str(mbid), Def.FANARTTV_API_KEY, __cfg__.client_key()) htmlsource = get_html_source(url, "FTV_" + str(mbid), save_file=True, overwrite=True) if htmlsource == "null": log("No artwork found for MBID: %s" % mbid) has_art = "False" else: log("Artwork found for MBID: %s" % mbid) has_art = "True" return has_art
def retrieve_album_details(album_id): cdam_utils.log("jsonrpc_calls.py - Retrieving Album Details") album_details = [] json_query = '{"jsonrpc": "2.0", "method": "AudioLibrary.GetAlbumDetails", ' \ '"params": {"properties": ["artist", "title", "musicbrainzalbumid", "musicbrainzalbumartistid"], ' \ '"albumid": %d}, "id": 1}' % album_id json_album_details = retrieve_json_dict(json_query, items='albumdetails', force_log=False) if json_album_details: album_details.append(json_album_details) return album_details else: return empty
def split_album_info(album_result, index): album = {} try: album["artist"] = album_result[index].releaseGroup.artist.name album["artist_id"] = album_result[index].releaseGroup.artist.id.replace("http://musicbrainz.org/artist/", "") album["id"] = album_result[index].releaseGroup.id.replace("http://musicbrainz.org/release-group/", "") album["title"] = album_result[index].releaseGroup.title except Exception as e: log(e.message) album["artist"] = "" album["artist_id"] = "" album["id"] = "" album["title"] = "" return album
def update_musicbrainz_id(type_, info): log("Updating MusicBrainz ID") artist_id = "" try: if type_ == "artist": # available data info["local_id"], info["name"], info["distant_id"] _, artist_id, _ = get_musicbrainz_artist_id(info["name"]) cdam_db.set_artist_mbid(artist_id, info["name"]) if type_ == "album": album, _ = get_musicbrainz_album(info["title"], info["artist"], 0) cdam_db.set_album_mbid(album["id"], info["title"]) except Exception as e: log(e.message, xbmc.LOGERROR) print_exc() return artist_id
def get_all_local_artists(all_artists=True): cdam_utils.log("jsonrpc_calls.py - Retrieving all local artists") if all_artists: json_query = '{"jsonrpc": "2.0", "method": "AudioLibrary.GetArtists", ' \ '"params": { "albumartistsonly": false }, "id": 1}' else: json_query = '{"jsonrpc": "2.0", "method": "AudioLibrary.GetArtists", ' \ '"params": { "albumartistsonly": true }, "id": 1}' json_artists = retrieve_json_dict(json_query, items='artists', force_log=False) if json_artists: return json_artists else: return empty
def cdart_single_delete(fn): log("Deleting: %s" % fn, xbmc.LOGNOTICE) if xbmcvfs.exists(fn): try: xbmcvfs.delete(fn) return True except Exception as e: log("Error in script occured", xbmc.LOGNOTICE) log(e.message, xbmc.LOGWARNING) log("Deleteing error, check path and file permissions", xbmc.LOGNOTICE) return False else: log("Error: cdART file does not exist.. Please check...", xbmc.LOGNOTICE) return True
def get_fanart_path(database_id): cdam_utils.log( "jsonrpc_calls.py - Retrieving Fanart Path for Artist id: %s" % database_id) if database_id: json_query = '{"jsonrpc": "2.0", "method": "AudioLibrary.GetArtistDetails", ' \ '"params": {"properties": ["fanart"], "artistid": %d}, "id": 1}' % database_id json_fanart = retrieve_json_dict(json_query, items='artistdetails', force_log=False) else: cdam_utils.log("jsonrpc_calls.py - Improper type or database_id") return empty if json_fanart: return json_fanart["fanart"] else: return empty
def retrieve_json_dict(json_query, items='items', force_log=False): """ retrieve_json_dict() This function returns the response from a json rpc query in a dict(hash) form requirements: json_query - a properly formed json rpc request for return items - the specific key being looked for, example for VideoLibrary.GetMovies - 'movies' is the key, 'items' is a common response(can be left blank then) """ xbmc.log("[json_utils.py] - JSONRPC Query -\n%s" % json_query, level=xbmc.LOGDEBUG) response = xbmc.executeJSONRPC(json_query) if force_log: xbmc.log("[json_utils.py] - retrieve_json_dict - JSONRPC -\n%s" % response, level=xbmc.LOGDEBUG) if response.startswith("{"): # response = eval(response, {"true": True, "false": False, "null": None}) response = json.loads(response) try: if 'result' in response: result = response['result'] json_dict = result[items] return json_dict else: xbmc.log( "[json_utils.py] - retrieve_json_dict - No response from XBMC", level=xbmc.LOGNOTICE) xbmc.log(response, level=xbmc.LOGDEBUG) return None except Exception as e: log("Error in script occured", xbmc.LOGNOTICE) log(e.message, xbmc.LOGWARNING) traceback.print_exc() xbmc.log("[json_utils.py] - retrieve_json_dict - JSONRPC -\n%s" % response, level=xbmc.LOGNOTICE) xbmc.log( "[json_utils.py] - retrieve_json_dict - Error trying to get json response", level=xbmc.LOGNOTICE) return empty else: return empty
def cdart_get_backup_filename(artist, album, disc_num=1): cfg = cdam.Settings() fn_format = cfg.folder() backup_folder = cfg.path_backup_path() if not backup_folder: cfg.open() backup_folder = cfg.path_backup_path() if fn_format == 0: destination = os.path.join(backup_folder, cu.change_characters(artist)) fn = os.path.join(destination, cu.change_characters(album)) else: destination = backup_folder fn = os.path.join( destination, cu.change_characters( (artist + " - " + album).lower())) if disc_num > 1: fn += "_disc_" + str(disc_num) fn += ".png" log("backup filename: %s" % fn, xbmc.LOGDEBUG) return fn
def get_musicbrainz_release_group(release_mbid): """ Retrieves the MusicBrainz Release Group MBID from a given release MBID Use: release_groupmbid = get_musicbrainz_release_group( release_mbid ) release_mbid - valid release mbid """ log("Retrieving MusicBrainz Release Group MBID from Album Release MBID") url = release_group_url_release_mbid % (server, quote_plus(release_mbid)) mbid = "" htmlsource = cu.get_html_source(url, release_mbid, save_file=False, overwrite=False) match = re.search('''<release-group-list count="(?:.*?)">(.*?)</release-group-list>''', htmlsource) if match: mbid_match = re.search('''<release-group id="(.*?)"(?:.*?)">''', match.group(1)) if not mbid_match: mbid_match = re.search('''<release-group (?:.*?)id="(.*?)">''', match.group(1)) if mbid_match: mbid = mbid_match.group(1) xbmc.sleep(mb_delay) return mbid
def check_fanart_new_artwork(present_datecode): log("Checking for new Artwork on fanart.tv since last run...", xbmc.LOGNOTICE) previous_datecode = retrieve_fanarttv_datecode() # fix: use global tempxml_folder instead of explicit definition tempxml_folder = __cdam__.path_temp_xml() if xbmcvfs.exists( os.path.join(tempxml_folder, "%s.xml" % previous_datecode)): xbmcvfs.delete( os.path.join(tempxml_folder, "%s.xml" % previous_datecode)) url = new_music % (Def.FANARTTV_API_KEY, __cfg__.client_key(), str(previous_datecode)) htmlsource = get_html_source(url, "FTV-NEW_" + str(present_datecode), save_file=True, overwrite=False) if htmlsource == "null": log("No new Artwork found on fanart.tv", xbmc.LOGNOTICE) return False, htmlsource else: try: log("New Artwork found on fanart.tv", xbmc.LOGNOTICE) data = json.loads(htmlsource) return True, data except Exception as e: htmlsource = "null" xbmc.log(e.message, xbmc.LOGERROR) print_exc() return False, htmlsource
def get_album_path(album_id): cdam_utils.log("jsonrpc_calls.py - Retrieving Album Path") paths = [] albumartistmbids = [] albumreleasembids = [] json_query = '{"jsonrpc": "2.0", "method": "AudioLibrary.GetSongs", ' \ '"params": { "properties": ["file", "musicbrainzalbumartistid", "musicbrainzalbumid"], ' \ '"filter": { "albumid": %d }, "sort": {"method":"path","order":"ascending"} }, "id": 1}' % album_id json_songs_detail = retrieve_json_dict(json_query, items='songs', force_log=False) if json_songs_detail: for song in json_songs_detail: path = os.path.dirname(song['file']) paths.append(path) albumartistmbid = song['musicbrainzalbumartistid'] albumartistmbids.append(albumartistmbid) albumreleasembid = song['musicbrainzalbumid'] albumreleasembids.append(albumreleasembid) return paths, albumartistmbids, albumreleasembids else: return empty
def get_thumbnail_path(database_id, type_): cdam_utils.log( "jsonrpc_calls.py - Retrieving Thumbnail Path for %s id: %s" % (type_, database_id)) if type_ == MediaType.ALBUM and database_id: json_query = '{"jsonrpc": "2.0", "method": "AudioLibrary.GetAlbumDetails", ' \ '"params": {"properties": ["thumbnail"], "albumid": %d}, "id": 1}' % database_id json_thumb = retrieve_json_dict(json_query, items='albumdetails', force_log=False) elif type_ == MediaType.ARTIST and database_id: json_query = '{"jsonrpc": "2.0", "method": "AudioLibrary.GetArtistDetails", ' \ '"params": {"properties": ["thumbnail"], "artistid": %d}, "id": 1}' % database_id json_thumb = retrieve_json_dict(json_query, items='artistdetails', force_log=False) else: cdam_utils.log("jsonrpc_calls.py - Improper type or database_id") return empty if json_thumb: return json_thumb["thumbnail"] else: return empty
def mbid_check(database_mbid, type_): log("Looking up %s MBID. Current MBID: %s" % (type_, database_mbid), xbmc.LOGNOTICE) new_mbid = "" mbid_match = False url = None if type_ == "release-group": url = release_group_id_check % (server, database_mbid) elif type_ == "artist": url = artist_id_check % (server, database_mbid) htmlsource = cu.get_html_source(url, "", save_file=False, overwrite=False) if type_ == "release-group": match = re.search('''<release-group id="(.*?)"(?:.*?)>''', htmlsource) if match: new_mbid = match.group(1) else: match = re.search('''<release-group (?:.*?)id="(.*?)">''', htmlsource) if match: new_mbid = match.group(1) else: match = re.search('''<release-group ext:score=(?:.*?)id="(.*?)">''', htmlsource) if match: new_mbid = match.group(1) mbid_match = bool(new_mbid == database_mbid) elif type_ == "artist": match = re.search('''<artist id="(.*?)"(?:.*?)>''', htmlsource) if match: new_mbid = match.group(1) else: match = re.search('''<artist(?:.*?)id="(.*?)">''', htmlsource) if match: new_mbid = match.group(1) if new_mbid == database_mbid: mbid_match = True else: mbid_match = False else: pass log("Current MBID: %s New MBID: %s" % (database_mbid, new_mbid)) if mbid_match: log("MBID is current. No Need to change") else: log("MBID is not current. Need to change") xbmc.sleep(mb_delay) return mbid_match, new_mbid
def get_musicbrainz_album(album_title, artist, e_count, limit=1, with_singles=False, by_release=False, use_alias=False, use_live=False): """ Retrieves information for Album from MusicBrainz using provided Album title and Artist name. Use: album, albums = get_musicbrainz_album( album_title, artist, e_count, limit, with_singles, by_release ) album_title - the album title(must be unicode) artist - the artist's name(must be unicode) e_count - used internally(should be set to 0) limit - limit the number of responses with_singles - set to True to look up single releases at the same time by_release - use release name for search """ match_within = "~2" album = {} albums = [] album["score"] = "" album["id"] = "" album["title"] = "" album["artist"] = "" album["artist_id"] = "" artist = cu.smart_unicode(cu.get_unicode(artist)) album_title = cu.smart_unicode(cu.get_unicode(album_title)) log("Artist: %s" % artist) log("Album: %s" % album_title) artist = artist.replace('"', '?') artist = artist.replace('&', 'and') album_title = album_title.replace('"', '?') album_title = album_title.replace('&', 'and') if limit == 1: url = None if not use_alias: url = release_group_url_artist % ( server, quote_plus(album_title.encode("utf-8")), match_within, quote_plus(artist.encode("utf-8"))) if not with_singles and not by_release and not use_live: log("Retrieving MusicBrainz Info - Checking by Artist - Not including Singles or Live albums", xbmc.LOGDEBUG) url = url + nolive_nosingles + query_limit % limit elif not with_singles and not by_release and use_live: log("Retrieving MusicBrainz Info - Checking by Artist - Not including Singles") url = url + live_nosingles + query_limit % limit elif not by_release: log("Retrieving MusicBrainz Info - Checking by Artist - Including Singles and Live albums", xbmc.LOGDEBUG) url += query_limit % limit elif not with_singles: log("Retrieving MusicBrainz Info - Checking by Artist - Using Release Name") url = release_group_url_artist % (server, quote_plus(album_title.encode("utf-8")), match_within, quote_plus(artist.encode("utf-8"))) + query_limit % limit elif use_alias: url = release_group_url_alias % ( server, quote_plus(album_title.encode("utf-8")), match_within, quote_plus(artist.encode("utf-8"))) if not with_singles and not by_release and not use_live: log("Retrieving MusicBrainz Info - Checking by Artist - Not including Singles or Live albums", xbmc.LOGDEBUG) url = url + nolive_nosingles + query_limit % limit elif not with_singles and not by_release and use_live: log("Retrieving MusicBrainz Info - Checking by Artist - Not including Singles") url = url + live_nosingles + query_limit % limit elif not by_release: log("Retrieving MusicBrainz Info - Checking by Artist - Including Singles and Live albums", xbmc.LOGDEBUG) url += query_limit % limit elif not with_singles: log("Retrieving MusicBrainz Info - Checking by Artist - Using Release Name") url = release_group_url_alias % (server, quote_plus(album_title.encode("utf-8")), match_within, quote_plus(artist.encode("utf-8"))) + query_limit % limit htmlsource = cu.get_html_source(url, "", save_file=False, overwrite=False) match = re.search('''<release-group-list count="(?:.*?)" offset="(?:.*?)">(.*?)</release-group-list>''', htmlsource) if match: try: mbid = re.search('''<release-group id="(.*?)"(?:.*?)">''', htmlsource) if not mbid: mbid = re.search('''<release-group (?:.*?)id="(.*?)">''', htmlsource) mbtitle = re.search('''<title>(.*?)</title>''', htmlsource) mbartist = re.search('''<name>(.*?)</name>''', htmlsource) mbartistid = re.search('''<artist id="(.*?)">''', htmlsource) album["id"] = mbid.group(1) album["title"] = cu.unescape(cu.smart_unicode(mbtitle.group(1))) album["artist"] = cu.unescape(cu.smart_unicode(mbartist.group(1))) album["artist_id"] = mbartistid.group(1) except Exception as e: log(e.message) if not album["id"]: xbmc.sleep(mb_delay) # sleep for allowing proper use of webserver if not with_singles and not by_release and not use_alias and not use_live: log("No releases found on MusicBrainz, Checking For Live Album") album, albums = get_musicbrainz_album(album_title, artist, 0, limit, False, False, False, True) # try again by using artist alias elif not with_singles and not by_release and not use_alias and use_live: log("No releases found on MusicBrainz, Checking by Artist Alias") album, albums = get_musicbrainz_album(album_title, artist, 0, limit, False, False, True, False) # try again by using artist alias elif use_alias and not with_singles and not by_release and not use_live: log("No releases found on MusicBrainz, Checking by Release Name") album, albums = get_musicbrainz_album(album_title, artist, 0, limit, False, True, False, False) # try again by using release name elif by_release and not with_singles and not use_alias: log("No releases found on MusicBrainz, Checking by Release name and Artist Alias") album, albums = get_musicbrainz_album(album_title, artist, 0, limit, False, True, True, False) # try again by using release name and artist alias elif by_release and not with_singles and use_alias: log("No releases found on MusicBrainz, checking singles") album, albums = get_musicbrainz_album(album_title, artist, 0, limit, True, False, False, False) # try again with singles elif with_singles and not use_alias and not by_release: log("No releases found on MusicBrainz, checking singles and Artist Alias") album, albums = get_musicbrainz_album(album_title, artist, 0, limit, True, False, True, False) # try again with singles and artist alias else: log("No releases found on MusicBrainz.") album["artist"], album["artist_id"], _ = get_musicbrainz_artist_id(artist) else: match_within = "~4" url = release_group_url_artist % ( server, (album_title.encode("utf-8")), match_within, (artist.encode("utf-8"))) + query_limit % limit htmlsource = cu.get_html_source(url, "", save_file=False, overwrite=False) match = re.search('''<release-group-list count="(?:.*?)" offset="(?:.*?)">(.*?)</release-group-list>''', htmlsource) if match: match_release_group = re.findall('''<release-group(.*?)</release-group>''', match.group(1)) if match_release_group: for item in match_release_group: album = {"score": "", "id": "", "title": "", "artist": "", "artist_id": ""} try: mbscore = re.search('''score="(.*?)"''', item) mbid = re.search('''<release-group id="(.*?)"(?:.*?)">''', item) if not mbid: mbid = re.search('''id="(.*?)"(?:.*?)">''', item) if not mbid: mbid = re.search('''<release-group (?:.*?)id="(.*?)">''', htmlsource) mbtitle = re.search('''<title>(.*?)</title>''', item) mbartist = re.search('''<name>(.*?)</name>''', item) mbartistid = re.search('''<artist id="(.*?)">''', item) album["score"] = mbscore.group(1) album["id"] = mbid.group(1) album["title"] = cu.unescape(cu.smart_unicode(mbtitle.group(1))) album["artist"] = cu.unescape(cu.smart_unicode(mbartist.group(1))) album["artist_id"] = mbartistid.group(1) log("Score : %s" % album["score"]) log("Title : %s" % album["title"]) log("Id : %s" % album["id"]) log("Artist : %s" % album["artist"]) log("Artist ID : %s" % album["artist_id"]) albums.append(album) except Exception as e: log(e.message, xbmc.LOGERROR) print_exc() else: pass else: pass xbmc.sleep(mb_delay) # sleep for allowing proper use of webserver return album, albums
def get_musicbrainz_artist_id(artist_search, limit=1, alias=False): name = "" id_ = "" score = "" sortname = "" artist_name = cu.smart_unicode((artist_search.replace('"', '?').replace('&', 'and'))) if not alias: url = artist_url % (server, quote_plus(artist_name.encode("utf-8")), limit) else: url = alias_url % (server, quote_plus(artist_name.encode("utf-8")), limit) htmlsource = cu.get_html_source(url, "", save_file=False, overwrite=False) match = re.search('''<artist(.*?)</artist>''', htmlsource) if match: score_match = re.search('''score="(.*?)"''', htmlsource) name_match = re.search('''<name>(.*?)</name>''', htmlsource) id_match = re.search('''<artist id="(.*?)"(?:.*?)>''', htmlsource) if not id_match: id_match = re.search('''<artist (?:.*?)id="(.*?)">''', htmlsource) sort_name_match = re.search('''<sort-name>(.*?)</sort-name>''', htmlsource) if score_match: score = score_match.group(1) if name_match: name = cu.unescape(cu.smart_unicode(name_match.group(1))) if id_match: id_ = id_match.group(1) if sort_name_match: sortname = cu.unescape(cu.smart_unicode(sort_name_match.group(1))) log("Score : %s" % score) log("Id : %s" % id_) log("Name : %s" % name) log("Sort Name : %s" % sortname) else: if not alias: log("No Artist ID found trying aliases: %s" % artist_search) name, id_, sortname = get_musicbrainz_artist_id(artist_search, limit, True) else: log("No Artist ID found for Artist: %s" % artist_search) xbmc.sleep(mb_delay) return name, id_, sortname
def get_musicbrainz_artists(artist_search, limit=1): log("Artist: %s" % artist_search) artists = [] artist_name = cu.smart_unicode((artist_search.replace('"', '?').replace('&', 'and'))) url = artist_url % (server, quote_plus(artist_name.encode("utf-8")), limit) htmlsource = cu.get_html_source(url, "", save_file=False, overwrite=False) match = re.findall('''<artist(.*?)</artist>''', htmlsource) if match: for item in match: artist = {"score": "", "name": "", "id": "", "sortname": ""} score_match = re.search('''score="(.*?)"''', item) name_match = re.search('''<name>(.*?)</name>''', item) id_match = re.search('''id="(.*?)"(?:.*?)>''', item) if not id_match: id_match = re.search('''id="(.*?)">''', item) sort_name_match = re.search('''<sort-name>(.*?)</sort-name>''', item) if score_match: artist["score"] = score_match.group(1) if name_match: artist["name"] = cu.unescape(cu.smart_unicode(name_match.group(1))) if id_match: artist["id"] = id_match.group(1) if sort_name_match: artist["sortname"] = cu.unescape(cu.smart_unicode(sort_name_match.group(1))) log("Score : %s" % artist["score"]) log("Id : %s" % artist["id"]) log("Name : %s" % artist["name"]) log("Sort Name : %s" % artist["sortname"]) artists.append(artist) else: log("No Artist ID found for Artist: %s" % repr(artist_search)) xbmc.sleep(mb_delay) return artists
def retrieve_fanarttv_json(id_): log("Retrieving artwork for artist id: %s" % id_) # url = music_url_json % (api_key, id, "all") url = music_url_json % (id_, Def.FANARTTV_API_KEY, __cfg__.client_key()) # htmlsource = (get_html_source(url, id, save_file=False, overwrite=False)).encode('utf-8', 'ignore') htmlsource = get_html_source(url, "FTV_" + str(id_), save_file=True, overwrite=False) artist_artwork = [] backgrounds = [] musiclogos = [] artistthumbs = [] hdlogos = [] banners = [] albums = [] fanart = {} clearlogo = {} artistthumb = {} album_art = {} hdlogo = {} banner = {} image_types = [ 'musiclogo', 'artistthumb', 'artistbackground', 'hdmusiclogo', 'musicbanner', 'albums' ] try: data = json.loads(htmlsource) # for key, value in data.iteritems(): for art in image_types: # if value.has_key(art): if art in data: # for item in value[art]: for item in data[art]: if art == "musiclogo": musiclogos.append(item.get('url')) if art == "hdmusiclogo": hdlogos.append(item.get('url')) if art == "artistbackground": backgrounds.append(item.get('url')) if art == "musicbanner": banners.append(item.get('url')) if art == "artistthumb": artistthumbs.append(item.get('url')) if art == "albums" and not albums: # for album_id in data[artist]["albums"]: for album_id, album in data["albums"].iteritems(): album_artwork = { "musicbrainz_albumid": album_id, ArtType.CDART: [], ArtType.COVER: "" } if ArtType.CDART in album: for subitem in album[ArtType.CDART]: cdart = {} if "disc" in subitem: cdart["disc"] = int(subitem["disc"]) else: cdart["disc"] = 1 if "url" in subitem: cdart[ArtType.CDART] = subitem["url"] else: cdart[ArtType.CDART] = "" if "size" in subitem: cdart["size"] = int(subitem["size"]) album_artwork[ArtType.CDART].append(cdart) try: if "albumcover" in album: # if len(album["albumcover"]) < 2: # we should download the first hit here if there are multiple covers album_artwork[ArtType.COVER] = album[ "albumcover"][0]["url"] except Exception as e: log(e.message) album_artwork[ArtType.COVER] = "" albums.append(album_artwork) except Exception as e: log(e.message, xbmc.LOGERROR) print_exc() fanart["backgrounds"] = backgrounds clearlogo["clearlogo"] = musiclogos hdlogo["hdlogo"] = hdlogos banner["banner"] = banners artistthumb["artistthumb"] = artistthumbs album_art["artwork"] = albums artist_artwork.append(fanart) artist_artwork.append(clearlogo) artist_artwork.append(artistthumb) artist_artwork.append(hdlogo) artist_artwork.append(banner) artist_artwork.append(album_art) # print artist_artwork return artist_artwork