mirror of
https://github.com/Xevion/spotify-explicit.git
synced 2025-12-08 20:08:28 -06:00
logging + proper saving finish
This commit is contained in:
30
pull.py
30
pull.py
@@ -4,11 +4,12 @@ import auth
|
|||||||
import json
|
import json
|
||||||
import pprint
|
import pprint
|
||||||
import spotipy
|
import spotipy
|
||||||
|
import logging
|
||||||
import spotipy.util as util
|
import spotipy.util as util
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Get Authorization
|
# Get Authorization
|
||||||
print('Authorizing with Spotify')
|
logging.info('Authorizing with Spotify (may require User interaction).')
|
||||||
token = util.prompt_for_user_token(
|
token = util.prompt_for_user_token(
|
||||||
username=auth.username,
|
username=auth.username,
|
||||||
scope=auth.scope,
|
scope=auth.scope,
|
||||||
@@ -17,16 +18,23 @@ def main():
|
|||||||
redirect_uri=auth.redirect_uri
|
redirect_uri=auth.redirect_uri
|
||||||
)
|
)
|
||||||
sp = spotipy.Spotify(auth=token)
|
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)
|
# 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)
|
response = sp.current_user_saved_tracks(limit=curlimit, offset=curoffset)
|
||||||
if response is not None:
|
received = len(response['items'])
|
||||||
print('Received ')
|
logging.debug('Received tracks {} to {}'.format(curoffset, curoffset + received))
|
||||||
filename = f'saved-tracks-{curoffset}-{curoffset + curlimit}.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
|
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()
|
||||||
Reference in New Issue
Block a user