refactoring for pull/processing abstraction

This commit is contained in:
Xevion
2019-10-27 14:24:28 -05:00
parent dc14adeca2
commit 92b109241e
4 changed files with 25 additions and 13 deletions

View File

@@ -3,7 +3,5 @@ client_id = "20cc828edc7140d88e1c8b0c08ed1672"
client_secret = "519fb97148a14952859ca3454b886cf9" client_secret = "519fb97148a14952859ca3454b886cf9"
redirect_uri = "http://localhost:8888/callback" redirect_uri = "http://localhost:8888/callback"
scope = ' '.join([ scope = ' '.join([
'user-library-read', 'user-library-read'
'user-read-recently-played',
'user-top-read'
]) ])

12
main.py Normal file
View File

@@ -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()

View File

@@ -1,5 +1,6 @@
import json import json
def main():
saved_response = json.load(open('saved_tracks.json', 'r')) saved_response = json.load(open('saved_tracks.json', 'r'))
for track in saved_response['items']: for track in saved_response['items']:
print('{} by {}'.format( print('{} by {}'.format(

21
pull.py
View File

@@ -9,7 +9,9 @@ import spotipy.util as util
def main(): def main():
# Get Authorization # 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( token = util.prompt_for_user_token(
username=auth.username, username=auth.username,
scope=auth.scope, scope=auth.scope,
@@ -18,23 +20,22 @@ def main():
redirect_uri=auth.redirect_uri redirect_uri=auth.redirect_uri
) )
sp = spotipy.Spotify(auth=token) sp = spotipy.Spotify(auth=token)
logging.info('Authorized with Spotify.') logging.info('Authorized with Spotify via Spotipy')
root = sys.path[0] root = sys.path[0]
curoffset, curlimit = 900, 50 curoffset, curlimit = 0, 50
# Start grabbing tracks (long running) # Start grabbing tracks (long running)
while curoffset >= 0: 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) response = sp.current_user_saved_tracks(limit=curlimit, offset=curoffset)
received = len(response['items']) 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' filename = f'saved-tracks-{curoffset}-{curoffset + received}.json'
filepath = os.path.join(root, 'tracks', filename) filepath = os.path.join(root, 'tracks', filename)
with open(filepath, 'w+') as file: with open(filepath, 'w+') as file:
json.dump(response, file) json.dump(response, file)
curoffset += curlimit logging.info('Saved at "{}" ({}KB)'.format(f'\\tracks\\{filename}', round(os.path.getsize(filepath) / 1024, 2)))
logging.debug('Saved at "{}" ({}KB)'.format(f'\\tracks\\{filename}', round(os.path.getsize(filepath) / 1024, 2)))
if received < curlimit: if received < curlimit:
logging.info('Done requesting/saving tracks after {} tracks.'.format()) logging.info('Done requesting/saving tracks after {} tracks.'.format(curoffset + received))
break
main() curoffset += curlimit