From 4c268dddb9c1f189e063e6130a8faf39af62bf00 Mon Sep 17 00:00:00 2001 From: Xevion Date: Sun, 27 Oct 2019 14:02:18 -0500 Subject: [PATCH] logging + proper saving finish --- pull.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/pull.py b/pull.py index ba78504..64d01d8 100644 --- a/pull.py +++ b/pull.py @@ -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() \ No newline at end of file