Skip to content
This repository has been archived by the owner on Jan 7, 2020. It is now read-only.
Shaywei edited this page May 16, 2015 · 1 revision

import sys import requests import spotipy

sp = spotipy.Spotify()

MY_API_KEY = "BEQMHFJOGRUW4ZWJ6" echo_nest_api = "http://developer.echonest.com/api/v4"

def search_artist(city=None, country=None, sort="hotttnesss-desc", result_num=1): # see http://developer.echonest.com/docs/v4/artist.html#search url = "{}/artist/search".format(echo_nest_api) params = {"api_key": MY_API_KEY, "sort": sort, "results": result_num, 'bucket': 'id:spotify'}

if city or country:
    val = ""
    if city:
        val="city:" + str(city)
    if country:
        val=val + "country:" + str(country)
    params["artist_location"] = val

rep = requests.get(url, params=params)
artists = rep.json().get('response').get('artists')
return {artist['id']: artist['name'] for artist in artists}

def search_song(artist_id, result_num="1", sort="song_hotttnesss-desc"): url = "{}/song/search".format(echo_nest_api) params = {"api_key": MY_API_KEY, "artist_id": artist_id, "sort": sort, "results": result_num, 'bucket': ['tracks', 'id:spotify']} rep = requests.get(url, params=params) result = rep.json().get('response').get('songs') return result

def get_top_songs_from_artists(artists, songs_per_artist): all_songs = [] for artist_id in artists.keys(): artist_songs = search_song(artist_id, result_num=songs_per_artist) for song in artist_songs: if len(song['tracks']) > 0: all_songs.append(song) return all_songs

def get_cover_art(song): response = sp.track(song['tracks'][0]['foreign_id']) return response['album']['images'][0]['url']

def dump(song): print "

" print " " % (get_cover_art(song), ) print "

", song['title'], '

' print "

", song['artist_name'], '

' print "

" print "
"

def generate_playlist(city, country, songs_per_artist, number_of_artists): top_artists = search_artist(city="Kiruna", country="Sweden", result_num=5) songs = get_top_songs_from_artists(artists=top_artists, songs_per_artist=3)

name = 'weezer'
if len(sys.argv) > 1:
    name = ' '.join(sys.argv[1:])

print "<html><body>"
for song in songs:
    dump(song)
print "</body></html>"

generate_playlist(city="Kiruna", country="Sweden", songs_per_artist=3, number_of_artists=5)

Clone this wiki locally