Automatically refresh token when expired when downloading from list

This commit is contained in:
Ritiek
2017-06-16 02:37:01 +05:30
parent 74511cf596
commit f1e5913e6e

View File

@@ -16,6 +16,7 @@ from mutagen.easyid3 import EasyID3
from mutagen.id3 import ID3, APIC from mutagen.id3 import ID3, APIC
from mutagen.mp4 import MP4, MP4Cover from mutagen.mp4 import MP4, MP4Cover
from slugify import slugify from slugify import slugify
import spotipy.oauth2.SpotifyOauthError
import spotipy import spotipy
import spotipy.oauth2 as oauth2 import spotipy.oauth2 as oauth2
import urllib2 import urllib2
@@ -356,9 +357,11 @@ def grab_list(file):
for raw_song in lines: for raw_song in lines:
try: try:
grab_single(raw_song, number=number) grab_single(raw_song, number=number)
trim_song(file) except spotipy.oauth2.SpotifyOauthError:
number += 1 token = generate_token()
print('') global spotify
spotify = spotipy.Spotify(auth=token)
grab_single(raw_song, number=number)
except KeyboardInterrupt: except KeyboardInterrupt:
grace_quit() grace_quit()
except (urllib2.URLError, IOError): except (urllib2.URLError, IOError):
@@ -367,6 +370,11 @@ def grab_list(file):
with open(file, 'a') as myfile: with open(file, 'a') as myfile:
myfile.write(raw_song) myfile.write(raw_song)
print('Failed to download song. Will retry after other songs.') print('Failed to download song. Will retry after other songs.')
continue
finally:
print('')
trim_song(file)
number += 1
# Logic behind preparing the song to download to finishing meta-tags # Logic behind preparing the song to download to finishing meta-tags
def grab_single(raw_song, number=None): def grab_single(raw_song, number=None):