logging + proper saving finish

This commit is contained in:
Xevion
2019-10-27 14:02:18 -05:00
parent 08ccf64e5e
commit 4c268dddb9

30
pull.py
View File

@@ -4,11 +4,12 @@ import auth
import json
import pprint
import spotipy
import logging
import spotipy.util as util
def main():
# Get Authorization
print('Authorizing with Spotify')
logging.info('Authorizing with Spotify (may require User interaction).')
token = util.prompt_for_user_token(
username=auth.username,
scope=auth.scope,
@@ -17,16 +18,23 @@ def main():
redirect_uri=auth.redirect_uri
)
sp = spotipy.Spotify(auth=token)
print('Authorized')
logging.info('Authorized with Spotify.')
curoffset, curlimit = 0, 50
root = sys.path[0]
curoffset, curlimit = 900, 50
# Start grabbing tracks (long running)
while True:
while curoffset >= 0:
logging.debug('Requesting tracks {} to {}'.format(curoffset, curoffset + curlimit.))
response = sp.current_user_saved_tracks(limit=curlimit, offset=curoffset)
if response is not None:
print('Received ')
filename = f'saved-tracks-{curoffset}-{curoffset + curlimit}.json'
filepath = os.path.join(root, 'tracks', filename)
with open(filepath, 'w+') as file:
json.dump(response, file)
curoffset += curlimit
received = len(response['items'])
logging.debug('Received tracks {} to {}'.format(curoffset, curoffset + received))
filename = f'saved-tracks-{curoffset}-{curoffset + received}.json'
filepath = os.path.join(root, 'tracks', filename)
with open(filepath, 'w+') as file:
json.dump(response, file)
curoffset += curlimit
logging.debug('Saved at "{}" ({}KB)'.format(f'\\tracks\\{filename}', round(os.path.getsize(filepath) / 1024, 2)))
if received < curlimit:
logging.info('Done requesting/saving tracks after {} tracks.'.format())
main()