def grab_list(text_file): """Download all songs from the list.""" with open(text_file, 'r') as listed: lines = (listed.read()).splitlines() # ignore blank lines in text_file (if any) try: lines.remove('') except ValueError: pass print(u'Total songs in list: {0} songs'.format(len(lines))) print('') # nth input song number = 1 for raw_song in lines: try: grab_single(raw_song, number=number) # token expires after 1 hour except spotipy.oauth2.SpotifyOauthError: # refresh token when it expires new_token = misc.generate_token() global spotify spotify = spotipy.Spotify(auth=new_token) grab_single(raw_song, number=number) # detect network problems except KeyboardInterrupt: misc.grace_quit() finally: print('') misc.trim_song(text_file) number += 1
def feed_album(album): new_token = misc.generate_token() spotify = spotipy.Spotify(auth=new_token) album_tracks = spotify.album_tracks(album, limit=50, offset=0) for tracks in album_tracks['items']: print(tracks['name']) print(tracks['href']) # nth input song number = 1 raw_song = tracks['href'] raw_song = raw_song.replace('tracks', 'track').replace('api', 'open').replace( 'v1/', '') print('Fetching album: ' + tracks['name']) try: grab_single(raw_song, number=number) # token expires after 1 hour # detect network problems except (urllib.request.URLError, TypeError, IOError): # wait 0.5 sec to avoid infinite looping new_token = misc.generate_token() #global spotify spotify = spotipy.Spotify(auth=new_token) print('network issue') time.sleep(0.5) continue except KeyboardInterrupt: misc.grace_quit() finally: print('') number += 1
def grab_list(text_file): """Download all songs from the list.""" with open(text_file, 'r') as listed: lines = (listed.read()).splitlines() # ignore blank lines in text_file (if any) try: lines.remove('') except ValueError: pass print(u'Total songs in list: {0} songs'.format(len(lines))) print('') # nth input song number = 1 for raw_song in lines: try: grab_single(raw_song, number=number) get_song_name(raw_song) # token expires after 1 hour except spotipy.oauth2.SpotifyOauthError: # refresh token when it expires #new_token = misc.generate_token() global spotify #spotify = spotipy.Spotify(auth=new_token) spotify = generate_token() grab_single(raw_song, number=number) # detect network problems except (urllib.request.URLError, TypeError, IOError): lines.append(raw_song) # remove the downloaded song from .txt misc.trim_song(text_file) # and append it to the last line in .txt with open(text_file, 'a') as myfile: myfile.write(raw_song + '\n') print('Failed to download song. Will retry after other songs.') # wait 0.5 sec to avoid infinite looping time.sleep(0.5) continue except KeyboardInterrupt: misc.grace_quit() finally: print('') misc.trim_song(text_file) number += 1
def grab_list(file): with open(file, 'r') as listed: lines = (listed.read()).splitlines() # ignore blank lines in file (if any) try: lines.remove('') except ValueError: pass print('Total songs in list = ' + str(len(lines)) + ' songs') print('') # nth input song number = 1 for raw_song in lines: try: grab_single(raw_song, number=number) # token expires after 1 hour except spotipy.oauth2.SpotifyOauthError: # refresh token when it expires token = misc.generate_token() global spotify spotify = spotipy.Spotify(auth=token) grab_single(raw_song, number=number) # detect network problems except (urllib2.URLError, TypeError, IOError): lines.append(raw_song) # remove the downloaded song from .txt misc.trim_song(file) # and append it to the last line in .txt with open(file, 'a') as myfile: myfile.write(raw_song) print('Failed to download song. Will retry after other songs.') continue except KeyboardInterrupt: misc.grace_quit() finally: print('') misc.trim_song(file) number += 1