-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify_tracks.py
91 lines (75 loc) · 2.45 KB
/
spotify_tracks.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
import requests
import threading
import time
import sys
import os
class PlaylistGetter:
def __init__(self, token):
self.token = token
def __call__(self):
global playlists
global arg
headers = {'Authorization':'Bearer ' + self.token}
r = requests.get("https://api.spotify.com/v1/users/" + arg + "/playlists", params = {'limit':50}, headers = headers)
playlists = []
while True:
jsonResponse = r.json()
try:
nextReq = jsonResponse['next']
except:
print("Sorry, user not found")
os._exit(-1)
temp = jsonResponse['items']
playlists.extend(map(lambda x: x['id'], temp))
if nextReq is None:
break
else:
r = requests.get(nextReq, headers = headers)
class TrackGetter:
def __call__(self):
global tracks
global token
headers = {'Authorization':'Bearer ' + token}
tracks = {}
for playlist in playlists:
r = requests.get("https://api.spotify.com/v1/playlists/" + playlist + "/tracks", headers = headers, params = {'limit':100})
while True:
if r.status_code == 429:
waitTime = r.headers['Retry-After']
time.sleep(waitTime)
r = requests.get(r.request.url, headers = headers)
else:
jsonResponse = r.json()
nextReq = jsonResponse['next']
temp = jsonResponse['items']
temp = map(lambda x: x['track'], temp)
tracks.update(map(lambda x: [x['name'],x['artists'][0]['name']], temp))
if nextReq is None:
break
else:
r = requests.get(nextReq, headers = headers)
def printTracks(list):
global tracks
f = open("tracks.txt", "w+")
for track in tracks:
# tracks is a dictionary containing track names as keys and track author as a value
f.write(tracks[track] + " - " + track+ "\r\n")
f.close()
arg = len(sys.argv)
if arg != 2:
raise EnvironmentError("You must insert a Spotify username as the only parameter")
arg = sys.argv[1]
body_param = {'grant_type':'client_credentials'}
heads = {'Authorization':'Basic OGY3NzE4ODg1ZDJhNDEzMDgwMjQwYTRjZmUzMWQ0ZDQ6OTBhNWZlNmE3MWZlNDRjMjhjYTVmNWY1YzM1ZjY4OTI='}
r = requests.post("https://accounts.spotify.com/api/token", data = body_param, headers = heads)
token = r.json()['access_token']
getter = threading.Thread(target = PlaylistGetter(token))
getter.start()
getter.join()
getter = threading.Thread(target = TrackGetter())
getter.start()
print("TrackList is retrieving all your tracks... Please be patient")
getter.join()
print("DONE!")
printTracks(tracks)
print("You have " + str(len(tracks)) + " tracks in your playlists")