-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
79 lines (64 loc) · 2.65 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
from spotipy.oauth2 import SpotifyOAuth
from auth import *
from dom_color import *
from sort import sorter
#Please input your client_id, secret, and your personal user_id.
# print('Please input your client_id, secret, and your personal user_id. ')
# client_id = str(input("Your client ID: "))
# secret = str(input("Your client secret: "))
# user_id = str(input("Your user ID: "))
auth = SpotifyClientCredentials(client_id, secret)
sp = spotipy.Spotify(auth_manager=auth)
# playlist_to_sort = str(input("Exact name of PUBLIC playlist to sort: "))
playlists = sp.user_playlists('spotify', limit = 50)
user_playlists = sp.user_playlists(user_id)
def get_playlist_tracks(u, playlist_id):
results = sp.playlist_items(playlist_id=playlist_id, market = 'us')
tracks = results['items']
while results['next']:
results = sp.next(results)
tracks.extend(results['items'])
return tracks
def playlister(user, playlists, playlist_to_sort):
pl_uri_list = {}
while playlists: #gets uri for each playlist
for i, playlist in enumerate(playlists['items']):
pl_uri_list[playlist['name']] = playlist['uri']
if playlists['next']:
playlists = sp.next(playlists)
else:
playlists = None
song_and_art = {}
playlist = pl_uri_list[playlist_to_sort]
playlist_items = get_playlist_tracks(user_id, playlist_id = playlist)
for song in range(len(playlist_items)):
song_uri = playlist_items[song]['track']['uri']
# # song_name = playlist_items[song]['track']['name']
# # band_name = playlist_items[song]['track']['album']['name']
song_art = playlist_items[song]['track']['album']['images'][0]['url']
song_and_art[song_uri] = song_art
print('Playlister done!')
return song_and_art
p = playlister(user_id, user_playlists, playlist_to_sort)
d = dom_col(p)
tracklist = sorter(d)
if len(tracklist) > 1000:
sorted_playlist = playlist_to_sort + '-HhlumSVLum sort80'
new_list = sp.user_playlist_create(user_id, sorted_playlist)
playlist_id = new_list["id"]
if len(tracklist) < 50:
sp.playlist_add_items(playlist_id, tracklist)
else:
lower = 0
upper = 10
bound = range(round(int(len(tracklist))/10) + 1)
for i in bound:
tracks_100 = tracklist[lower:upper]
# print(tracks_100)
if (tracks_100 != []):
sp.playlist_add_items(playlist_id, tracks_100)
lower+=10
upper+=10
print('Playlist done! Check your Spotify!')