def search(self, artist, album, track):
     """get musicbrainz id by query of artist, album and/or track"""
     artist = artist.lower()
     params = {'s': artist, 'a': album}
     data = self.get_data("searchalbum.php", params)
     if not album and track:
         params = {'t': track, 's': artist}
         data = self.get_data("searchtrack.php", params)
         if data and data.get("track") and len(data.get("track")) > 0:
             adbdetails = data["track"][0]
             # safety check - only allow exact artist match
             foundartist = adbdetails.get("strArtist", "").lower()
             if foundartist and get_compare_string(
                     foundartist) == get_compare_string(artist):
                 album = adbdetails.get("strAlbum", "")
                 artist = adbdetails.get("strArtist", "")
     if data and data.get("album") and len(data.get("album")) > 0:
         adbdetails = data["album"][0]
         # safety check - only allow exact artist match
         foundartist = adbdetails.get("strArtist", "").lower()
         if foundartist and get_compare_string(
                 foundartist) == get_compare_string(artist):
             album = adbdetails.get("strAlbum", "")
             artist = adbdetails.get("strArtist", "")
     return artist, album
Example #2
0
 def search(self, artist, album, track):
     '''get musicbrainz id by query of artist, album and/or track'''
     artistid = ""
     albumid = ""
     artist = artist.lower()
     params = {'s': artist, 'a': album}
     data = self.get_data("searchalbum.php", params)
     if data and data.get("album") and len(data.get("album")) > 0:
         adbdetails = data["album"][0]
         # safety check - only allow exact artist match
         foundartist = adbdetails.get("strArtist", "").lower()
         if foundartist and get_compare_string(foundartist) == get_compare_string(artist):
             albumid = adbdetails.get("strMusicBrainzID", "")
             artistid = adbdetails.get("strMusicBrainzArtistID", "")
     if (not artistid or not albumid) and artist and track:
         params = {'s': artist, 't': track}
         data = self.get_data("searchtrack.php", params)
         if data and data.get("track") and len(data.get("track")) > 0:
             adbdetails = data["track"][0]
             # safety check - only allow exact artist match
             foundartist = adbdetails.get("strArtist", "").lower()
             if foundartist and get_compare_string(foundartist) == get_compare_string(artist):
                 albumid = adbdetails.get("strMusicBrainzID", "")
                 artistid = adbdetails.get("strMusicBrainzArtistID", "")
     return (artistid, albumid)
Example #3
0
 def search(self, artist, album, track):
     """get musicbrainz id's by query of artist, album and/or track"""
     artistid = ""
     albumid = ""
     artist = artist.lower()
     if artist and album:
         params = {'method': 'album.getInfo', 'artist': artist, 'album': album}
         data = self.get_data(params)
         if data and data.get("album"):
             lfmdetails = data["album"]
             if lfmdetails.get("mbid"):
                 albumid = lfmdetails.get("mbid")
             if lfmdetails.get("tracks") and lfmdetails["tracks"].get("track"):
                 for track in lfmdetails.get("tracks")["track"]:
                     found_artist = get_compare_string(track["artist"]["name"])
                     if found_artist == get_compare_string(artist) and track["artist"].get("mbid"):
                         artistid = track["artist"]["mbid"]
                         break
     if not (artistid or albumid) and artist and track:
         params = {'method': 'track.getInfo', 'artist': artist, 'track': track}
         data = self.get_data(params)
         if data and data.get("track"):
             lfmdetails = data["track"]
             if lfmdetails.get('album position="1"'):
                 albumid = lfmdetails['album position="1"'].get("mbid")
             if lfmdetails.get("artist") and lfmdetails["artist"].get("name"):
                 found_artist = get_compare_string(lfmdetails["artist"]["name"])
                 if found_artist == get_compare_string(artist) and lfmdetails["artist"].get("mbid"):
                     artistid = lfmdetails["artist"]["mbid"]
     return artistid, albumid
Example #4
0
 def search(self, artist, album, track):
     '''get musicbrainz id's by query of artist, album and/or track'''
     artistid = ""
     albumid = ""
     artist = artist.lower()
     if artist and album:
         params = {'method': 'album.getInfo', 'artist': artist, 'album': album}
         data = self.get_data(params)
         if data and data.get("album"):
             lfmdetails = data["album"]
             if lfmdetails.get("mbid"):
                 albumid = lfmdetails.get("mbid")
             if lfmdetails.get("tracks") and lfmdetails["tracks"].get("track"):
                 for track in lfmdetails.get("tracks")["track"]:
                     found_artist = get_compare_string(track["artist"]["name"])
                     if found_artist == get_compare_string(artist) and track["artist"].get("mbid"):
                         artistid = track["artist"]["mbid"]
                         break
     if not (artistid or albumid) and artist and track:
         params = {'method': 'track.getInfo', 'artist': artist, 'track': track}
         data = self.get_data(params)
         if data and data.get("track"):
             lfmdetails = data["track"]
             if lfmdetails.get('album position="1"'):
                 albumid = lfmdetails['album position="1"'].get("mbid")
             if lfmdetails.get("artist") and lfmdetails["artist"].get("name"):
                 found_artist = get_compare_string(lfmdetails["artist"]["name"])
                 if found_artist == get_compare_string(artist) and lfmdetails["artist"].get("mbid"):
                     artistid = lfmdetails["artist"]["mbid"]
     return (artistid, albumid)
Example #5
0
 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
Example #6
0
 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 select_best_match(results,
                          prefyear="",
                          preftype="",
                          preftitle="",
                          manual_select=False):
        '''helper to select best match or let the user manually select the best result from the search'''
        details = {}
        # score results if one or more preferences are given
        if results and (prefyear or preftype or preftitle):
            newdata = []
            preftitle = preftitle.lower()
            for item in results:
                item["score"] = 0
                itemtitle = item["title"] if item.get(
                    "title") else item["name"]
                itemtitle = itemtitle.lower()
                itemorgtitle = item["original_title"] if item.get(
                    "original_title") else item["original_name"]
                itemorgtitle = itemorgtitle.lower()

                # high score if year matches
                if prefyear:
                    if item.get("first_air_date"
                                ) and prefyear in item["first_air_date"]:
                        item["score"] += 800  # matches preferred year
                    if item.get("release_date"
                                ) and prefyear in item["release_date"]:
                        item["score"] += 800  # matches preferred year

                # find exact match on title
                if preftitle and preftitle == itemtitle:
                    item["score"] += 1000  # exact match!
                if preftitle and preftitle == itemorgtitle:
                    item["score"] += 1000  # exact match!

                # match title by replacing some characters
                if preftitle and get_compare_string(
                        preftitle) == get_compare_string(itemtitle):
                    item["score"] += 750
                if preftitle and get_compare_string(
                        preftitle) == get_compare_string(itemorgtitle):
                    item["score"] += 750

                # add SequenceMatcher score to the results
                if preftitle:
                    stringmatchscore = SM(
                        None, preftitle, itemtitle).ratio() + SM(
                            None, preftitle, itemorgtitle).ratio()
                    if stringmatchscore > 1.6:
                        item["score"] += stringmatchscore * 250

                # higher score if result ALSO matches our preferred type or native language
                # (only when we already have a score)
                if item["score"]:
                    if preftype and (item["media_type"] in preftype) or (
                            preftype in item["media_type"]):
                        item["score"] += 250  # matches preferred type
                    if item["original_language"] == KODI_LANGUAGE:
                        item["score"] += 500  # native language!
                    if KODI_LANGUAGE.upper() in item.get("origin_country", []):
                        item["score"] += 500  # native language!
                    if KODI_LANGUAGE in item.get("languages", []):
                        item["score"] += 500  # native language!

                if item["score"] > 500 or manual_select:
                    newdata.append(item)
            results = sorted(newdata, key=itemgetter("score"), reverse=True)

        if results and manual_select:
            # show selectdialog to manually select the item
            results_list = []
            for item in results:
                title = item["name"] if "name" in item else item["title"]
                if item.get("premiered"):
                    year = item["premiered"].split("-")[0]
                else:
                    year = item.get("first_air_date", "").split("-")[0]
                if item["poster_path"]:
                    thumb = "http://image.tmdb.org/t/p/original%s" % item[
                        "poster_path"]
                else:
                    thumb = ""
                label = "%s (%s) - %s" % (title, year, item["media_type"])
                listitem = xbmcgui.ListItem(label=label,
                                            iconImage=thumb,
                                            label2=item["overview"])
                results_list.append(listitem)
            if manual_select and results_list:
                dialog = DialogSelect("DialogSelect.xml",
                                      "",
                                      listing=results_list,
                                      window_title="%s - TMDB" %
                                      xbmc.getLocalizedString(283))
                dialog.doModal()
                selected_item = dialog.result
                del dialog
                if selected_item != -1:
                    details = results[selected_item]
                else:
                    results = []

        if not details and results:
            # just grab the first item as best match
            details = results[0]
        return details
Example #8
0
    def select_best_match(results, prefyear="", preftype="", preftitle="", manual_select=False):
        '''helper to select best match or let the user manually select the best result from the search'''
        details = {}
        # score results if one or more preferences are given
        if results and (prefyear or preftype or preftitle):
            newdata = []
            preftitle = preftitle.lower()
            for item in results:
                item["score"] = 0
                itemtitle = item["title"] if item.get("title") else item["name"]
                itemtitle = itemtitle.lower()
                itemorgtitle = item["original_title"] if item.get("original_title") else item["original_name"]
                itemorgtitle = itemorgtitle.lower()

                # high score if year matches
                if prefyear:
                    if item.get("first_air_date") and prefyear in item["first_air_date"]:
                        item["score"] += 800  # matches preferred year
                    if item.get("release_date") and prefyear in item["release_date"]:
                        item["score"] += 800  # matches preferred year

                # find exact match on title
                if preftitle and preftitle == itemtitle:
                    item["score"] += 1000  # exact match!
                if preftitle and preftitle == itemorgtitle:
                    item["score"] += 1000  # exact match!

                # match title by replacing some characters
                if preftitle and get_compare_string(preftitle) == get_compare_string(itemtitle):
                    item["score"] += 750
                if preftitle and get_compare_string(preftitle) == get_compare_string(itemorgtitle):
                    item["score"] += 750

                # add SequenceMatcher score to the results
                if preftitle:
                    stringmatchscore = SM(None, preftitle, itemtitle).ratio(
                    ) + SM(None, preftitle, itemorgtitle).ratio()
                    if stringmatchscore > 1.6:
                        item["score"] += stringmatchscore * 250

                # higher score if result ALSO matches our preferred type or native language
                # (only when we already have a score)
                if item["score"]:
                    if preftype and (item["media_type"] in preftype) or (preftype in item["media_type"]):
                        item["score"] += 250  # matches preferred type
                    if item["original_language"] == KODI_LANGUAGE:
                        item["score"] += 500  # native language!
                    if KODI_LANGUAGE.upper() in item.get("origin_country", []):
                        item["score"] += 500  # native language!
                    if KODI_LANGUAGE in item.get("languages", []):
                        item["score"] += 500  # native language!

                if item["score"] > 500 or manual_select:
                    newdata.append(item)
            results = sorted(newdata, key=itemgetter("score"), reverse=True)

        if results and manual_select:
            # show selectdialog to manually select the item
            results_list = []
            for item in results:
                title = item["name"] if "name" in item else item["title"]
                if item.get("premiered"):
                    year = item["premiered"].split("-")[0]
                else:
                    year = item.get("first_air_date", "").split("-")[0]
                if item["poster_path"]:
                    thumb = "http://image.tmdb.org/t/p/original%s" % item["poster_path"]
                else:
                    thumb = ""
                label = "%s (%s) - %s" % (title, year, item["media_type"])
                listitem = xbmcgui.ListItem(label=label, iconImage=thumb, label2=item["overview"])
                results_list.append(listitem)
            if manual_select and results_list:
                dialog = DialogSelect("DialogSelect.xml", "", listing=results_list, window_title="%s - TMDB"
                                      % xbmc.getLocalizedString(283))
                dialog.doModal()
                selected_item = dialog.result
                del dialog
                if selected_item != -1:
                    details = results[selected_item]
                else:
                    results = []

        if not details and results:
            # just grab the first item as best match
            details = results[0]
        return details