Ejemplo n.º 1
0
Archivo: lastfm.py Proyecto: lidel/mmda
def _lastfm_get_tag_artists_optimized(lastfm_tag):
    """
    Get tag artists from last.fm API in optimized way.

    Currently the only way to get mbids of such artists in pylast is item.get_mbid() method,
    which invokes 'artist.getInfo()' API method -- this is an overhead of one additional http request for each artist.
    It is redundant, since mbids are already in obtained XML, but pylast architecture ignores it atm.
    This method is a necessary workaround.

    @param lastfm_tag: a pylast.Tag object

    @return:  a dictionary with artist name and mbid
    """
    params = lastfm_tag._get_params()
    #params['limit'] = pylast._unicode(limit)
    doc = lastfm_tag._request('tag.getTopArtists', True, params)
    names = pylast._extract_all(doc, "name")
    mbids = pylast._extract_all(doc, "mbid")
    #matches = pylast._extract_all(doc, "match")
    tag_artists = []
    for i in xrange(0, len(names)):
        if mbids[i]:
            tag_artists.append({'name':names[i], 'mbid':mbids[i]})
    # TODO: catch and store images?
    return tag_artists
Ejemplo n.º 2
0
Archivo: lastfm.py Proyecto: lidel/mmda
def _lastfm_get_similar_optimized(lastfm_artist,limit=10):
    """
    Get similar artists from last.fm API in optimized way.

    Currently the only way to get mbids of such artists in pylast is item.get_mbid() method,
    which invokes 'artist.getInfo()' API method -- this is an overhead of one additional http request for each similar artist.
    It is redundant, since mbids are already in obtained XML, but pylast architecture ignores it atm.
    This method is a necessary workaround.

    @param lastfm_artist: a pylast.Artist object
    @param limit: number of similar artist to get

    @return:  a dictionary with artist name, score and mbid
    """
    params = lastfm_artist._get_params()
    params['limit'] = pylast._unicode(limit)
    doc = lastfm_artist._request('artist.getSimilar', True, params)
    names = pylast._extract_all(doc, "name")
    mbids = pylast._extract_all(doc, "mbid")
    matches = pylast._extract_all(doc, "match")
    similar_artists = []
    for i in range(0, len(names)):
        if mbids[i]:
            similar_artists.append({'name':names[i], 'score':int(float(matches[i])*100), 'mbid':mbids[i]})
    # TODO: catch and store images?
    return similar_artists