def artist_bio(req):
    """Fetches the artist biography summary text from last.fm

    Expects JSON paramater 'artist'"""

    artist = req.get('result').get('parameters').get('artist')
    bio = cached_result('get_artist_info', [artist], {})
    speech = 'Unable to locate biography for {artist}'.format(artist=artist)

    if bio is not None:
        speech = bio

    return jsonify(
        {'speech': speech,
         'displayText': speech,
         'source': 'last-assist'})
def artist_bio(req):
    """Fetches the artist biography summary text from last.fm

    Expects JSON paramater 'artist'"""

    artist = req.get('result').get('parameters').get('artist')
    logger.info('received {0} request for artist="{1}"'.format('artist_bio', artist))
    bio = cached_result('get_artist_info', [artist], {})
    speech = 'Unable to locate biography for {artist}'.format(artist=artist)

    if bio and len(bio) > 0:
        speech = bio

    return jsonify(
        {'speech': speech,
         'displayText': speech,
         'source': 'last-assist'})
def artist_top_tracks(req):
    """Fetches top tracks for artist from last.fm

    Expects JSON paramater 'artist'"""

    artist = req.get('result').get('parameters').get('artist')
    top_tracks = cached_result('get_artist_top_tracks', [artist], {})
    speech = 'Unable to locate top tracks for {artist}'.format(artist=artist)

    if top_tracks is not None:
        speech = 'The most popular songs for {artist} are'.format(artist=artist)
        for track in top_tracks:
            speech += '. ' + track

    return jsonify(
        {'speech': speech,
         'displayText': speech,
         'source': 'last-assist'})
def artist_similar(req):
    """Fetches artists that are similar to the given artist

    Expects JSON paramater 'artist'"""

    artist = req.get('result').get('parameters').get('artist')
    sim_artists = cached_result('get_similar_artists', [artist], {})
    speech = 'Unable to locate similar artists for {artist}'.format(artist=artist)

    if sim_artists and len(sim_artists) > 0:
        speech = 'The following artists are similar to {artist}'.format(
            artist=artist)
        for art in sim_artists:
            speech += u'. {artist}'.format(artist=art)

    return jsonify(
        {'speech': speech,
         'displayText': speech,
         'source': 'last-assist'})
def track_similar(req):
    """Given a song title and artist, this will retrieve like or similar songs.

    Expects JSON paramater 'artist' and 'track'"""

    artist = req.get('result').get('parameters').get('artist')
    track = req.get('result').get('parameters').get('track')
    sim_tracks = cached_result('get_similar_tracks', [artist, track], {})
    speech = 'Unable to locate similar tracks for song {track} by {artist}'.format(
        track=track, artist=artist)

    if sim_tracks and len(sim_tracks) > 0:
        speech = 'The following songs are similar to {track} by {artist}'.format(
            track=track, artist=artist)
        for track in sim_tracks:
            speech += '. ' + track[0] + ' by ' + track[1]

    return jsonify(
        {'speech': speech,
         'displayText': speech,
         'source': 'last-assist'}
    )