Created
November 4, 2019 22:07
-
-
Save surskitt/097ae956fd40864bec20dfdeccd4bdbd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import spotipy.util | |
import iterfzf | |
def get_api_dict(user, client_id, client_secret): | |
""" Create the api dict for use by spotify auth """ | |
return {'username': user, | |
'client_id': client_id, | |
'client_secret': client_secret, | |
'redirect_uri': 'http://localhost', | |
'scope': 'playlist-read-private\ | |
playlist-modify-private\ | |
playlist-modify-public\ | |
user-library-read\ | |
user-library-modify'} | |
def get_spotify_client(user, client_id, client_secret): | |
""" Retrieve a token using api details and return client object """ | |
api = get_api_dict(user, client_id, client_secret) | |
# retrieve token, asking user to auth in browser if necessary | |
token = spotipy.util.prompt_for_user_token(**api) | |
if not token: | |
print('Error: token not received', file=sys.stderr) | |
sys.exit(1) | |
return spotipy.Spotify(auth=token) | |
def saved_album_gen(sp): | |
""" Pull all saved albums """ | |
r = sp.current_user_saved_albums(limit=50) | |
while r: | |
for i in r['items']: | |
yield i | |
r = sp.next(r) | |
def album_name(album): | |
""" return text representation of album """ | |
date = album['album']['release_date'] | |
artist = ', '.join(i['name'] for i in album['album']['artists']) | |
name = album['album']['name'] | |
return f'{date}: {artist} - {name}' | |
def main(): | |
# spotify client credentials | |
user = 'sharktamer' | |
client_id = '' | |
client_secret = '' | |
# generate spotify client object | |
sp = get_spotify_client(user, client_id, client_secret) | |
# pull all saved albums from spotify api | |
albums = saved_album_gen(sp) | |
# create dict using the album representation as key | |
albums_dict = {album_name(a): a['album'] for a in albums | |
if a['album']['release_date'] > '2010-01-01'} | |
# select albums using fzf | |
selected_albums = iterfzf.iterfzf(sorted(albums_dict), multi=True, | |
prompt='select albums: ') | |
# exit if no albums were selected | |
if selected_albums is None: | |
sys.exit() | |
# search for playlist | |
playlist_name = 'best of 2010s' | |
playlist_search = [i for i in sp.user_playlists(user)['items'] | |
if i['name'] == playlist_name] | |
# if playlist does not exist, create it | |
if len(playlist_search) == 0: | |
playlist = sp.user_playlist_create(user, 'best of 2010s', public=True) | |
else: | |
playlist = playlist_search[0] | |
# for each selected album, pick tracks and add to playlist | |
for name in selected_albums: | |
album = albums_dict[name] | |
tracks_dict = {t['name']: t['uri'] for t in album['tracks']['items']} | |
selected_tracks = iterfzf.iterfzf(tracks_dict, multi=True, | |
prompt=f'{name}: ') | |
for track_name in selected_tracks: | |
track_uri = tracks_dict[track_name] | |
sp.user_playlist_add_tracks(user, playlist['id'], [track_uri]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment