Skip to content

Instantly share code, notes, and snippets.

@Lorac
Last active March 16, 2017 17:35
Show Gist options
  • Save Lorac/f32bb5460d8cf5bfa116f5e637bf78f4 to your computer and use it in GitHub Desktop.
Save Lorac/f32bb5460d8cf5bfa116f5e637bf78f4 to your computer and use it in GitHub Desktop.
Based of an artist, generate a playlist
#!/usr/bin/python3
import argparse
import itertools
import sys
import time
import threading
import spotipy
import spotipy.util as util
DEFAULT_NUMBER_RELATED_ARTISTS = 3
done = False
def animate():
for c in itertools.cycle(['|', '/', '-', '\\']):
if done:
break
sys.stdout.write('\rLoading ' + c)
sys.stdout.flush()
time.sleep(0.075)
sys.stdout.write('\rDone! ')
def main():
global done
parser = argparse.ArgumentParser(description='Spotify information.')
parser.add_argument('-u', '--username', dest="username", help='Your username')
parser.add_argument('-a', '--artist', dest="artist", help='Spotify artist')
parser.add_argument('-p', '--playlist', dest="playlistName", help='Spotify playlist name')
parser.add_argument('-r', '--numberRelatedArtists', dest="numberRelatedArtists", type=int, default=DEFAULT_NUMBER_RELATED_ARTISTS, help='Number of related artists to fetch')
args = parser.parse_args()
if args.numberRelatedArtists < 1:
parser.error("Need at least 1 related artist")
if args.username is None:
parser.error("Username is required")
token = util.prompt_for_user_token(username=args.username, scope="playlist-modify-public playlist-modify-private")
spotify_client = spotipy.Spotify(auth=token)
current_user = spotify_client.current_user()
artists = spotify_client.search(args.artist, type="artist")['artists']['items']
if (len(artists) == 0):
print("Couldn\'t find any artists")
return 1
choice = 1
if len(artists) > 1:
for index, artist in enumerate(artists):
print("%s)" % str(index + 1), artist['name'])
choice = int(input("Please make your choice: "))
print("Chosen artist : %s" % artists[choice-1]['name'])
loading_animation = threading.Thread(target=animate)
loading_animation.start()
try:
top_artist_uri = artists[choice - 1]['uri']
top_tracks = spotify_client.artist_top_tracks(top_artist_uri)['tracks']
playlist = spotify_client.user_playlist_create(current_user['id'], args.playlistName)
tracks_to_add = [track for track in top_tracks]
related_artist = spotify_client.artist_related_artists(top_artist_uri)['artists']
for artist in related_artist[:args.numberRelatedArtists]:
related_artist_top_tracks = spotify_client.artist_top_tracks(artist['uri'])['tracks']
[tracks_to_add.append(track) for track in related_artist_top_tracks]
spotify_client.user_playlist_add_tracks(current_user['id'], playlist['id'], [track["id"] for track in tracks_to_add])
done = True
print()
for track in tracks_to_add:
print("%s - %s" % (track['artists'][0]['name'], track['name']))
except Exception as e:
done = True
print(e)
if __name__ == "__main__":
main()
@Lorac
Copy link
Author

Lorac commented Mar 14, 2017

pip install spotipy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment