-
Notifications
You must be signed in to change notification settings - Fork 2
/
playlist_info_2.py
113 lines (88 loc) · 3.4 KB
/
playlist_info_2.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from tokenize import String
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import config
import json
import requests
import shutil
import os
client_id = config.spotify_CLIENT_ID
client_secret = config.spotify_CLIENT_SECRET
client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
def run(user_id):
#may not be using all of the imports in this file
CLIENT_ID = config.spotify_CLIENT_ID
CLIENT_SECRET = config.spotify_CLIENT_SECRET
#client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
AUTH_URL = 'https://accounts.spotify.com/api/token'
# POST
auth_response = requests.post(AUTH_URL, {
'grant_type': 'client_credentials',
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
})
# convert the response to JSON
auth_response_data = auth_response.json()
# save the access token
access_token = auth_response_data['access_token']
headers = {
'Authorization': 'Bearer {token}'.format(token=access_token)
}
# base URL of all Spotify API endpoints
BASE_URL = 'https://api.spotify.com/v1/'
# Track ID from the URI
user_id = user_id
# actual GET request with proper header
r = requests.get(BASE_URL + 'users/' + user_id+ '/playlists?offset=0&limit=50', headers=headers)
r = r.json()
with open("Mehul's_playlists.json", "w") as outfile:
json.dump(r, outfile)
lis = [] # this is the list of playlist ids for current user
for i in range(len(r['items'])):
lis.append((r['items'][i]['name'],r['items'][i]['id']))
return lis
def get_playlist_tracks(username,playlist_info):
playlist_name = playlist_info[0]
playlist_id = playlist_info[1]
# Track ID from the URI
play_id = str(playlist_id)
results = sp.user_playlist_tracks(username,play_id)
tracks = results['items']
while results['next']:
results = sp.next(results)
tracks.extend(results['items'])
#if isEnglish(playlist_name) == False:
# playlist_name = play_id
if os.path.exists(f'{username}_playlists/{play_id}.txt') :
return
with open(play_id+".txt", "w") as outfile:
for i in range(len(tracks)):
if tracks[i]['track'] != None:
if isEnglish(tracks[i]['track']['name']) and isEnglish(tracks[i]['track']['artists'][0]['name']):
outfile.write(tracks[i]['track']['name'] +"; " + tracks[i]['track']['artists'][0]['name'] +"\n")
directory = username+"_playlists"
# Parent Directory path
parent_dir = os.getcwd() + "/Users"
# Path
path = os.path.join(parent_dir, directory)
if os.path.exists(f'Users/{username}_playlists') == False :
os.mkdir(path)
shutil.move(play_id+'.txt', f'Users/{username}_playlists/{play_id}.txt')
return 0
def isEnglish(s):
if s is None:
return False
try:
s.encode(encoding='utf-8').decode('ascii')
except UnicodeDecodeError:
return False
else:
return True
def main(user_id):
list_of_playlists = run(user_id)
for i in range(len(list_of_playlists)):
print(get_playlist_tracks(user_id,list_of_playlists[i]))
if __name__ == '__main__':
main('swimguin')
#print(get_playlist_tracks("mmurali20","0KeRsGnBhW4YkE9MECH3X0"))