I am currently working on a Python script that essentially scrapes a given Twitter account for Spotify tracks and creates a playlist of the tracks found. The script is functioning properly for the most part but I am trying to incorporate some error handling and am running into some issues. The first error I want to tackle is when a user enters a Spotify/Twitter username that isn't valid.
So far, I have created 2 separate try-except loops to continuously prompt the user for their Twitter and Spotify usernames until valid ones are entered. The Twitter try-except loop seems to be functioning properly but the Spotify loop gets stuck if an incorrect username is entered (ie does not except valid username and won't terminate when ^C is entered).
Twitter try-except loop:
# Get request token from Twitter
reqclient = Twython(consumer_key, consumer_secret)
reqcreds = reqclient.get_authentication_tokens()
# Prompt user for Twitter account information until valid account is found
validated = False
while validated == False:
try:
t_username = input("Please enter your TWITTER username: ")
user_info = reqclient.show_user(screen_name=t_username)
# except TwythonError as e:
except:
print("Could not find Twitter account " + t_username + ". Please try again.")
# print(e)
continue
else:
t_user_id = user_info["id"]
validated = True
Spotify try-except loop:
# Create a SpotifyOAuth object to begin authroization code flow
scope = 'playlist-modify-public playlist-read-collaborative playlist-read-private playlist-modify-private' # Specifies access/user authorization needed to perform request (i.e. create playlist)
sp_oauth = spotipy.oauth2.SpotifyOAuth(client_id=client_id,client_secret=client_secret,redirect_uri=redirect_uri,scope=scope)
# Prompt user for Spotify account information until valid account is found
validated = False
while validated == False:
try:
s_username = input("Please enter your SPOTIFY username: ")
user_info = sp_oauth.current_user()
except:
print("Could not find Spotify account " + s_username + ". Please try again.")
continue
else:
s_user_id = user_info["id"]
validated = True
Am I missing something here? I feel like it's really obvious but I can't seem to find the issue (and apologies if this is the case)?
There are two things going on here.
First, the value of s_username
that you enter is never actually applied to any sort of Spotify API object, so the result of sp_oauth.current_user()
is never going to change.
The second, and more important, issue is that you are using a blanket except
statement which catches all exceptions. This is causing problems for you because it's also catching the KeyboardInterrupt
exception that normally allows you to exit a program by pressing CTRL-C
. You should almost always narrow the exception you are catching to the specific error type you want to handle.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.