diff --git a/auth.py b/auth.py index c52bfdb..264186e 100644 --- a/auth.py +++ b/auth.py @@ -3,7 +3,5 @@ client_id = "20cc828edc7140d88e1c8b0c08ed1672" client_secret = "519fb97148a14952859ca3454b886cf9" redirect_uri = "http://localhost:8888/callback" scope = ' '.join([ - 'user-library-read', - 'user-read-recently-played', - 'user-top-read' + 'user-library-read' ]) \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..3bacbff --- /dev/null +++ b/main.py @@ -0,0 +1,12 @@ +import os +import sys +import time +import json +import auth +import pull +import process +import logging + +cache = json.load(open(os.path.join(sys.path[0], f'.cache-{auth.username}'))) +if time.time() > cache['expires_at']: + pull.main() \ No newline at end of file diff --git a/process.py b/process.py index 148ac16..fcacc8e 100644 --- a/process.py +++ b/process.py @@ -1,5 +1,6 @@ import json +def main(): saved_response = json.load(open('saved_tracks.json', 'r')) for track in saved_response['items']: print('{} by {}'.format( diff --git a/pull.py b/pull.py index 64d01d8..4b11e17 100644 --- a/pull.py +++ b/pull.py @@ -9,7 +9,9 @@ import spotipy.util as util def main(): # Get Authorization - logging.info('Authorizing with Spotify (may require User interaction).') + logging.basicConfig(level=logging.INFO) + logging.info('Authorizing with Spotify via Spotipy') + logging.warning('May require User Interaction to authenticate properly!') token = util.prompt_for_user_token( username=auth.username, scope=auth.scope, @@ -18,23 +20,22 @@ def main(): redirect_uri=auth.redirect_uri ) sp = spotipy.Spotify(auth=token) - logging.info('Authorized with Spotify.') + logging.info('Authorized with Spotify via Spotipy') root = sys.path[0] - curoffset, curlimit = 900, 50 + curoffset, curlimit = 0, 50 # Start grabbing tracks (long running) while curoffset >= 0: - logging.debug('Requesting tracks {} to {}'.format(curoffset, curoffset + curlimit.)) + logging.info('Requesting tracks {} to {}'.format(curoffset, curoffset + curlimit)) response = sp.current_user_saved_tracks(limit=curlimit, offset=curoffset) received = len(response['items']) - logging.debug('Received tracks {} to {}'.format(curoffset, curoffset + received)) + logging.info('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))) + logging.info('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 + logging.info('Done requesting/saving tracks after {} tracks.'.format(curoffset + received)) + break + curoffset += curlimit \ No newline at end of file