Refactored refresh token (#408)

* Outputs error details when track download fails from list file

* Refactored Spotipy token refreshing

* Reverted to old refreshing method

Kept refresh_token() in spotify_tools.py
This commit is contained in:
Manveer Basra
2018-10-25 10:30:46 -04:00
committed by Ritiek Malhotra
parent 680525ea3d
commit 94dc27a77b
2 changed files with 13 additions and 7 deletions

View File

@@ -97,8 +97,7 @@ def download_list(tracks_file, skip_file=None, write_successful_file=None):
except spotipy.client.SpotifyException: except spotipy.client.SpotifyException:
# refresh token when it expires # refresh token when it expires
log.debug("Token expired, generating new one and authorizing") log.debug("Token expired, generating new one and authorizing")
new_token = spotify_tools.generate_token() spotify_tools.refresh_token()
spotify_tools.spotify = spotipy.Spotify(auth=new_token)
download_single(raw_song, number=number) download_single(raw_song, number=number)
# detect network problems # detect network problems
except (urllib.request.URLError, TypeError, IOError) as e: except (urllib.request.URLError, TypeError, IOError) as e:

View File

@@ -10,6 +10,7 @@ from titlecase import titlecase
import pprint import pprint
import sys import sys
def generate_token(): def generate_token():
""" Generate the token. Please respect these credentials :) """ """ Generate the token. Please respect these credentials :) """
credentials = oauth2.SpotifyClientCredentials( credentials = oauth2.SpotifyClientCredentials(
@@ -20,6 +21,12 @@ def generate_token():
return token return token
def refresh_token():
""" Refresh expired token"""
global spotify
new_token = generate_token()
spotify = spotipy.Spotify(auth=new_token)
# token is mandatory when using Spotify's API # token is mandatory when using Spotify's API
# https://developer.spotify.com/news-stories/2017/01/27/removing-unauthenticated-calls-to-the-web-api/ # https://developer.spotify.com/news-stories/2017/01/27/removing-unauthenticated-calls-to-the-web-api/
token = generate_token() token = generate_token()