Use logging.info instead of rolling own

pull/30/head
Casey Chu 2020-10-09 19:39:23 -07:00
parent 79fa60be54
commit 7a5cc0edd1
1 changed files with 13 additions and 12 deletions

View File

@ -5,6 +5,7 @@ import codecs
import http.client import http.client
import http.server import http.server
import json import json
import logging
import re import re
import sys import sys
import time import time
@ -13,6 +14,9 @@ import urllib.parse
import urllib.request import urllib.request
import webbrowser import webbrowser
logging.basicConfig(level=20, datefmt='%I:%M:%S', format='[%(asctime)s] %(message)s')
class SpotifyAPI: class SpotifyAPI:
# Requires an OAuth token. # Requires an OAuth token.
@ -36,9 +40,9 @@ class SpotifyAPI:
reader = codecs.getreader('utf-8') reader = codecs.getreader('utf-8')
return json.load(reader(res)) return json.load(reader(res))
except Exception as err: except Exception as err:
log('Couldn\'t load URL: {} ({})'.format(url, err)) logging.info('Couldn\'t load URL: {} ({})'.format(url, err))
time.sleep(2) time.sleep(2)
log('Trying again...') logging.info('Trying again...')
sys.exit(1) sys.exit(1)
# The Spotify API breaks long lists into multiple pages. This method automatically # The Spotify API breaks long lists into multiple pages. This method automatically
@ -60,7 +64,7 @@ class SpotifyAPI:
'scope': scope, 'scope': scope,
'redirect_uri': 'http://127.0.0.1:{}/redirect'.format(SpotifyAPI._SERVER_PORT) 'redirect_uri': 'http://127.0.0.1:{}/redirect'.format(SpotifyAPI._SERVER_PORT)
}) })
log(f'Logging in (click if it doesn\'t open automatically): {url}') logging.info(f'Logging in (click if it doesn\'t open automatically): {url}')
webbrowser.open(url) webbrowser.open(url)
# Start a simple, local HTTP server to listen for the authorization token... (i.e. a hack). # Start a simple, local HTTP server to listen for the authorization token... (i.e. a hack).
@ -101,7 +105,7 @@ class SpotifyAPI:
self.wfile.write(b'<script>close()</script>Thanks! You may now close this window.') self.wfile.write(b'<script>close()</script>Thanks! You may now close this window.')
access_token = re.search('access_token=([^&]*)', self.path).group(1) access_token = re.search('access_token=([^&]*)', self.path).group(1)
log(f'Received access token from Spotify: {access_token}') logging.info(f'Received access token from Spotify: {access_token}')
raise SpotifyAPI._Authorization(access_token) raise SpotifyAPI._Authorization(access_token)
else: else:
@ -115,10 +119,6 @@ class SpotifyAPI:
def __init__(self, access_token): def __init__(self, access_token):
self.access_token = access_token self.access_token = access_token
def log(str):
#print('[{}] {}'.format(time.strftime('%I:%M:%S'), str).encode(sys.stdout.encoding, errors='replace'))
sys.stdout.buffer.write('[{}] {}\n'.format(time.strftime('%I:%M:%S'), str).encode(sys.stdout.encoding, errors='replace'))
sys.stdout.flush()
def main(): def main():
# Parse arguments. # Parse arguments.
@ -142,19 +142,20 @@ def main():
else: else:
spotify = SpotifyAPI.authorize(client_id='5c098bcc800e45d49e476265bc9b6934', spotify = SpotifyAPI.authorize(client_id='5c098bcc800e45d49e476265bc9b6934',
scope='playlist-read-private playlist-read-collaborative user-library-read') scope='playlist-read-private playlist-read-collaborative user-library-read')
logging.info('Loading info...')
# Get the ID of the logged in user. # Get the ID of the logged in user.
me = spotify.get('me') me = spotify.get('me')
log('Logged in as {display_name} ({id})'.format(**me)) logging.info('Logged in as {display_name} ({id})'.format(**me))
# List all playlists and all track in each playlist. # List all playlists and all track in each playlist.
playlists = spotify.list('users/{user_id}/playlists'.format(user_id=me['id']), {'limit': 50}) playlists = spotify.list('users/{user_id}/playlists'.format(user_id=me['id']), {'limit': 50})
for playlist in playlists: for playlist in playlists:
log('Loading playlist: {name} ({tracks[total]} songs)'.format(**playlist)) logging.info('Loading playlist: {name} ({tracks[total]} songs)'.format(**playlist))
playlist['tracks'] = spotify.list(playlist['tracks']['href'], {'limit': 100}) playlist['tracks'] = spotify.list(playlist['tracks']['href'], {'limit': 100})
# Write the file. # Write the file.
log('Writing files...') logging.info('Writing files...')
with open(args.file, 'w', encoding='utf-8') as f: with open(args.file, 'w', encoding='utf-8') as f:
# JSON file. # JSON file.
if args.format == 'json': if args.format == 'json':
@ -172,7 +173,7 @@ def main():
album=track['track']['album']['name'] album=track['track']['album']['name']
)) ))
f.write('\r\n') f.write('\r\n')
log('Wrote file: ' + args.file) logging.info('Wrote file: ' + args.file)
if __name__ == '__main__': if __name__ == '__main__':
main() main()