Ejemplo n.º 1
0
def get_extra_artist_info(id):
    req_vars = { "apikey": apikey(),
                 "sig": sign(),
                 "nameid": id,
                 "format": "json",
                 "country": "US",
                 "language": "en",
                 "include": "musicbio,images"}
Ejemplo n.º 2
0
def lfm_match_album(tracks):
    """
    Match a set of tracks from LFM listening history by album, with moods/tones

    The intuitive thing to do here would be to perform a track level
    match and then request track level data on moods and tones. I'm not 
    planning to do this, looking at album-level instead for two reasons. 

    First, the Rovi API doesn't provide track-level mood/tone data. 
    Second, the Rovi API reasonable requires two API calls to get this
    on a track basis, and I can do it in a single go with the match/album API. 

    This function currently only considers the best match and does not perform
    any sanity-checking or worry about accuracy in any way. 

    It queries Rovi's Match/Album API. 
    """

    # Fixme: I should consider a shortcut for this which avoids multiple calls
    # to the same (suspected) album 

    rovi_ids = [ ] 
    for track in tracks:
        # Fixme: handle missing elements in the LFM data? 
        req_vars = {'name': track['album'],
                    'performername': track['artist'],
                    'include': 'moods,themes',
                    'size': 1,
                    'format': 'json',
                    'apikey': apikey(),
                    'sig': sign()}
 
        r = requests.get('http://api.rovicorp.com/recognition/v2.1/music/match/album',
                         params=req_vars)
        
        album_match = r.json
        
        album_data = album_match.get('matchResponse', False)
        if album_data:
            album_data = album_data.get('results', False)
        if album_data:
            # This should be a single entry list
            album_data = album_data[0]
            album_id = album_data.get('id')
            album_info = album_data.get('album')
            rovi_moods = album_info.get('moods')
            rovi_themes = album_info.get('themes')

            rovi_ids.append({'album_id': album_id,
                             'track': track['track'],
                             'artist': track['artist'],
                             'album': track['album'],
                             'moods': rovi_moods,
                             'themes': rovi_themes})

        # Continue looping through the tracks in list above.

    return rovi_ids
Ejemplo n.º 3
0
def get_genres():
    req_vars = { "apikey": apikey(),
                 "sig": sign(),
                 "include": "subgenres",
                 "format": "json",
                 "country": "US",
                 "language": "en" }
 
    base_url = "http://api.rovicorp.com/data/v1.1/descriptor/musicgenres" 
    r = requests.get(base_url, params=req_vars)
    data = r.json
    data = data.get('genres')
    
    f = open("genre_cache.json", 'w')
    json.dump(data, f, indent=4)
    f.close()
Ejemplo n.º 4
0
def get_rovi_response(path, method, param_list):
    param_list.append(('apikey', apikey()))
    param_list.append(('sig', sign()))
    param_list.append(('format', 'json'))

    params = urllib.urlencode(param_list)
    
    url = 'http://%s/%s/%s' % (str(hostname()) , str(path), str(method))
    
    url = url + '?' + params
    
    if debug:
        print url
        pass
    
    response_dict = {}
    
    if 0:
        pass
    else:
        
        count = 0
        while count < 5:
            f = urllib.urlopen(url)
            http_data = f.read()
            if http_data == "":
                if debug:
                    print "Failed on URL, retrying: " + url
                time.sleep(60)
                count += 1
                continue
            response_dict = json.loads(http_data)
            return response_dict
             
            
    return response_dict
Ejemplo n.º 5
0
def lfm_match_tracks(tracks):
    """ 
    Associate Rovi IDs with a list of tracks from LFM listening history

    Take a track list in the format {artist: 'artist', track: 'track', 
    album: 'album'}

    This will use Rovi's match API to get a track ID back.
    It returns a list [ ] with format # Fixme.

    At the moment, the plan is to only record the single, best match and
    not worry too much about sanity-checking the accuracy of the match.

    This function is quite expensive since it requires a http call for 
    each track in the list. It currently works, more or less, but 
    is not actually used in the web app so hasn't been tested with a lot
    of data. lfm_match_album was used instead. 
    """
    
    rovi_ids = [ ] 
    for track in tracks:
        # Calling Rovi Match API - track match.
        # Fixme: manage missing album, artist case? 
        req_vars = {'name': track['track'],
                    'performername': track['artist'],
                    'albumtitle': track['album'],
                    'include': 'appearances',
                    'size': 1,
                    'format': 'json',
                    'apikey': apikey(),
                    'sig': sign() 
                    }
        r = requests.get('http://api.rovicorp.com/recognition/v2.1/music/match/track', 
                         params=req_vars)
        
        track_match = r.json

        track_data = track_match.get('matchResponse', False)
        if track_data: 
            track_data = track_data.get('results')
        if track_data: 
            # At this point we should have a single-element list. 
            # All I am looking for at this point, blindly, is ID
            # First, the unambiguous track_ID, and then a possibly
            # one-to-many album_ID. 
            # I'm not doing anything to check that I have the 
            # 'right' album_ID
            # Fixme: Ask the Rovi team about appearances API. 
            # Fixme: Put in some sanity checking here. 

            # Fixme: Ask Will about multi-level dict checking. I'm 
            # not happy with the way this code below is written. 
            track_data = track_data[0]
            rovi_track_id = track_data.get('id')
            rovi_song_data = track_data.get('song', False)
            rovi_track_appearances = False
            if rovi_song_data:                
                rovi_track_appearances = rovi_song_data.get('appearances', False)
            appearance_ids = False
            rovi_album_id = None
            if rovi_track_appearances:
                appearance_ids = rovi_track_appearances[0].get('ids', False)
            if appearance_ids:
                rovi_album_id = appearance_ids.get('albumId')

            # Append to the new list
            # This could be done by assigning the object directly.
            rovi_ids.append({'track_ID': rovi_track_id,
                             'track': track['track'],
                             'artist': track['artist'],
                             'album': track['album'],
                             'album_ID': rovi_album_id})            
        
        # Continue looping through tracks in list above
    
    return rovi_ids
Ejemplo n.º 6
0
def song(data):
    my_url = 'http://api.rovicorp.com/data/v1/song/info?track=' + str(data) + "&apikey=" + str(apikey()) + "&sig=" + str(sign())

    f = urllib.urlopen(my_url)

    song = json.loads(f.read())
    top_song = song["song"]["title"]
    top_artist = song["song"]["primaryArtists"][0]["name"]

    song_text = "Top result: " + top_song + " by " + top_artist + "."

    return build_response(song_text, " ")
Ejemplo n.º 7
0
def review(data):
    my_url = 'http://api.rovicorp.com/data/v1/album/primaryreview?album=' + str(data) + '&format=json' + '&apikey=' + str(apikey()) + '&sig=' + str(sign())

    f = urllib.urlopen(my_url)
    review = json.loads(f.read())
    
    review_text = "Sorry, no album review available"
    album_review = review.get('primaryReview')
    if album_review:
        review_text = album_review.get('text')
    review_text = scrub_links(review_text)

    return build_response(review_text, " ")
Ejemplo n.º 8
0
def bio(data):
    my_url = 'http://api.rovicorp.com/data/v1/name/musicbio?name=' + str(data) + '&format=json' + '&apikey=' + str(apikey()) + '&sig=' + str(sign())
    
    f = urllib.urlopen(my_url)
    biography = json.loads(f.read())

    bio_text = "Sorry, no bio available"
    music_bio = biography.get("musicBio")

    if music_bio:
        music_overview = music_bio.get("musicBioOverview")
        if music_overview:
            bio_text = music_overview[0].get("overview")
    bio_text = scrub_links(bio_text)

    return build_response(bio_text, " ")
Ejemplo n.º 9
0
def name(data):
    my_url = 'http://api.rovicorp.com/data/v1/name/discography?name=' + str(data) + '&type=main' + '&format=json' + '&apikey=' + str(apikey()) + '&sig=' + str(sign()) 
    
    f = urllib.urlopen(my_url)
    discography = json.loads(f.read())
    
    album_str = ""
    for albums in discography['discography']:
        album_str = album_str + albums.get('title') + " " + albums.get('year')[:4] + "\r\n"
    
    return build_response(album_str, '\r\n')
Ejemplo n.º 10
0
def album(data):
    my_url = 'http://api.rovicorp.com/data/v1/album/info?album=' + str(data) + '&apikey=' + str(apikey()) + '&sig=' + str(sign())

    f = urllib.urlopen(my_url)
    album = json.loads(f.read())
    top_album = album["album"]["title"]
    top_artist = album["album"]["primaryArtists"][0]["name"]
    release_date = album["album"]["originalReleaseDate"]

    album_text = "Top result: " + top_album + " by " + top_artist + ". Released on " + str(release_date) + "."

    return build_response(album_text, " ")