-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_library.py
executable file
·39 lines (34 loc) · 1.19 KB
/
get_library.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
#!/usr/bin/python3
import json
import datetime
import sys
import argparse
import spotutil
parser = argparse.ArgumentParser()
parser.add_argument("refresh_token",
help="Your personal Spotify refresh token")
parser.add_argument("output_dir", nargs="?",
help="Output directory", default='.')
parser.add_argument("-q", "--quiet",
help="No output", action='store_true')
args = parser.parse_args()
# open file now - fail fast :)
filename = args.output_dir + '/' + str(datetime.date.today()) + '-library.json'
outfile = open(filename, 'w')
auth_header = spotutil.get_auth_header_from_refresh_token(args.refresh_token)
url = 'https://api.spotify.com/v1/me/tracks?market=US&limit=50'
results = []
count = 0
while url:
response = spotutil.fetch_spotify_url_with_retry(url, auth_header)
for item in response['items']:
results.append(item)
count += 1
url = response['next']
if not args.quiet:
print("...%s" % count, end='', flush=True)
if not args.quiet:
print("\nRetrieved " + str(len(results)) +
" tracks, writing to " + filename)
filename = str(datetime.date.today()) + '.json'
json.dump(results, outfile)