-
Notifications
You must be signed in to change notification settings - Fork 0
/
cool_artists.py
executable file
·64 lines (55 loc) · 3.04 KB
/
cool_artists.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
# source secret.env before running this program!
# command for testing:
# ./cool_artists.py dangbert spotify:playlist:5RkoGPrfNbgjK0qkJizt1O ""
import spotipy
import spotipy.util as util
from tools import cool_artists
import pprint
import json
import sys
import argparse
from dateutil import parser
from dateutil.relativedelta import *
from datetime import datetime
import pytz
# TODO: this fails for the Rap Caviar playlist:
# ./cool_artists.py dangbert spotify:playlist:37i9dQZF1DX0XUsuxWHRQd ""
# TODO: only pulled 2 songs from "cool artists" for input song spotify:track:24ovEYWV3PjlLOSsmNEWQv
# TODO: consider putting this online (with a dumb simple UI)
# user chooses source playlist from dropdown of their playlists or enters the URI
# backend hosted on (Heroku), frontend on engbert.me, and sharing on reddit...
def main():
# parse args
parser = argparse.ArgumentParser(description='Gets the top songs of each unique artist in source playlist and adds them to the dest playlist.')
# TODO: add option to select whether to put songs at top or bottom of playlist...
parser.add_argument('username', type=str, help='(string) username of spotify account owning the playlist.')
parser.add_argument('source_playlist_uri', type=str, help='(string) uri of spotify playlist to source names of artists from (e.g. "spotify:playlist:i0dcGw2CvUDFwx833UdaLf"')
# (args starting with '--' are made optional)
parser.add_argument('dest_playlist_uri', type=str, help='(string) uri of spotify playlist to add the top songs to by the artists found in the source playlist OR set to "" to create a new playlist')
parser.add_argument('--delete_after', type=str, default='false', help='(optional bool) set "true" to delete songs in the source playlist as well. (default "false")')
parser.add_argument('--copy_num', type=int, default=3, help='(optional int) max number of top songs to add from each artist in addition to the initial source song (default 3)')
args = parser.parse_args()
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
exit(1)
args.delete_after = args.delete_after.lower()
if args.delete_after not in ["true", "false"]:
print("ERROR: argument for --delete_after must be 'true' or 'false'")
exit(1)
args.delete_after = True if args.delete_after == "true" else False
# setup API
scope = 'playlist-modify-public playlist-modify-private'
# TODO: note that this^ won't work for modifying a user's private playlists
# https://developer.spotify.com/documentation/general/guides/scopes/#playlist-modify-private
token = util.prompt_for_user_token(args.username, scope)
if not token:
print("Can't get Spotify API token for", args.username)
exit(1)
sp = spotipy.Spotify(auth=token)
sp.trace = False
# modify playlist
print("\nRunning cool_artists: " + str(datetime.today()) + "\n")
cool_artists(sp, args.username, args.source_playlist_uri, args.dest_playlist_uri, args.copy_num, args.delete_after)
if __name__ == "__main__":
main()