Handle new Tweepy exceptions properly. #403

This commit is contained in:
Manuel Cortez 2021-10-07 09:20:06 -05:00
parent d8fca3b31a
commit 286e030f40
10 changed files with 50 additions and 46 deletions

View File

@ -192,7 +192,7 @@ class BaseBuffer(base.Buffer):
user_ids = [item.message_create["sender_id"] for item in val]
self.session.save_users(user_ids)
except TweepyException as e:
log.error("Error %s: %s" % (e.api_code, e.reason))
log.exception("Error %s" % (str(e)))
return
number_of_items = self.session.order_buffer(self.name, val)
log.debug("Number of items retrieved: %d" % (number_of_items,))
@ -230,7 +230,7 @@ class BaseBuffer(base.Buffer):
try:
items = getattr(self.session.twitter, self.function)(max_id=last_id, count=self.session.settings["general"]["max_tweets_per_call"], *self.args, **self.kwargs)
except TweepyException as e:
log.error("Error %s: %s" % (e.api_code, e.reason))
log.exception("Error %s" % (str(e)))
return
if items == None:
return

View File

@ -41,7 +41,7 @@ class DirectMessagesBuffer(base.BaseBuffer):
items = results
log.debug("Retrieved %d items for cursored search in function %s" % (len(items), self.function))
except TweepyException as e:
log.error("Error %s: %s" % (e.api_code, e.reason))
log.exception("Error %s" % (str(e)))
return
if items == None:
return

View File

@ -126,7 +126,7 @@ class PeopleBuffer(base.BaseBuffer):
val.reverse()
log.debug("Retrieved %d items from cursored search in function %s" % (len(val), self.function))
except TweepyException as e:
log.error("Error %s: %s" % (e.api_code, e.reason))
log.exception("Error %s" % (str(e)))
return
number_of_items = self.session.order_people(self.name, val)
log.debug("Number of items retrieved: %d" % (number_of_items,))
@ -156,7 +156,7 @@ class PeopleBuffer(base.BaseBuffer):
items = results
log.debug("Retrieved %d items from cursored search in function %s" % (len(items), self.function))
except TweepyException as e:
log.error("Error %s: %s" % (e.api_code, e.reason))
log.exception("Error %s" % (str(e)))
return
if items == None:
return

View File

@ -46,7 +46,7 @@ class TrendsBuffer(base.Buffer):
try:
data = self.session.twitter.get_place_trends(id=self.trendsFor)
except TweepyException as err:
log.error("Error %s: %s" % (err.api_code, err.reason))
log.exception("Error %s" % (str(err)))
if not hasattr(self, "name_"):
self.name_ = data[0]["locations"][0]["name"]
self.trends = data[0]["trends"]

View File

@ -50,8 +50,8 @@ class listsController(object):
self.session.db["lists"].append(new_list)
self.dialog.lista.insert_item(False, *compose.compose_list(new_list))
except TweepyException as e:
output.speak("error %s: %s" % (e.api_code, e.reason))
log.exception("error %s: %s" % (e.api_code, e.reason))
output.speak("error %s" % (str(e)))
log.exception("error %s" % (str(e)))
dialog.destroy()
def edit_list(self, *args, **kwargs):
@ -71,7 +71,8 @@ class listsController(object):
self.session.get_lists()
self.dialog.populate_list(self.get_all_lists(), True)
except TweepyException as e:
output.speak("error %s: %s" % (e.api_code, e.reason))
output.speak("error %s" % (str(e)))
log.exception("error %s" % (str(e)))
dialog.destroy()
def remove_list(self, *args, **kwargs):
@ -83,7 +84,8 @@ class listsController(object):
self.session.db["lists"].pop(self.dialog.get_item())
self.dialog.lista.remove_item(self.dialog.get_item())
except TweepyException as e:
output.speak("error %s: %s" % (e.api_code, e.reason))
output.speak("error %s" % (str(e)))
log.exception("error %s" % (str(e)))
def open_list_as_buffer(self, *args, **kwargs):
if self.dialog.lista.get_count() == 0: return
@ -98,7 +100,8 @@ class listsController(object):
item = utils.find_item(list.id, self.session.db["lists"])
self.session.db["lists"].append(list)
except TweepyException as e:
output.speak("error %s: %s" % (e.api_code, e.reason))
output.speak("error %s" % (str(e)))
log.exception("error %s" % (str(e)))
def unsubscribe(self, *args, **kwargs):
if self.dialog.lista.get_count() == 0: return
@ -107,4 +110,5 @@ class listsController(object):
list = self.session.twitter.unsubscribe_list(list_id=list_id)
self.session.db["lists"].remove(list)
except TweepyException as e:
output.speak("error %s: %s" % (e.api_code, e.reason))
output.speak("error %s" % (str(e)))
log.exception("error %s" % (str(e)))

View File

@ -29,7 +29,7 @@ from sessions.twitter import session as session_
from pubsub import pub
import sound
import output
from tweepy.errors import TweepyException
from tweepy.errors import TweepyException, Forbidden
from mysc.thread_utils import call_threaded
from mysc.repeating_timer import RepeatingTimer
from mysc import restart
@ -549,7 +549,8 @@ class Controller(object):
buff.session.db["lists"].pop(older_list)
buff.session.db["lists"].append(list)
except TweepyException as e:
output.speak("error %s: %s" % (e.api_code, e.reason))
log.exception("error %s" % (str(e)))
output.speak("error %s" % (str(e)))
def remove_from_list(self, *args, **kwargs):
buff = self.get_best_buffer()
@ -577,7 +578,8 @@ class Controller(object):
buff.session.db["lists"].pop(older_list)
buff.session.db["lists"].append(list)
except TweepyException as e:
output.speak("error %s: %s" % (e.api_code, e.reason))
output.speak("error %s" % (str(e)))
log.exception("error %s" % (str(e)))
def list_manager(self, *args, **kwargs):
s = self.get_best_buffer().session
@ -1342,10 +1344,9 @@ class Controller(object):
else:
i.start_stream(play_sound=False)
except TweepyException as err:
log.exception("Error %s starting buffer %s on account %s, with args %r and kwargs %r due to the following reason: %s" % (err.api_code, i.name, i.account, i.args, i.kwargs, err.reason))
log.exception("Error %s starting buffer %s on account %s, with args %r and kwargs %r." % (str(err), i.name, i.account, i.args, i.kwargs))
# Determine if this error was caused by a block applied to the current user (IE permission errors).
errors_allowed = [130]
if (err.api_code != None and err.api_code not in errors_allowed) or (err.api_code == None and 'Not authorized' in err.reason): # A twitter error, so safely try to remove the buffer.
if type(err) == Forbidden:
buff = self.view.search(i.name, i.account)
i.remove_buffer(force=True)
commonMessageDialogs.blocked_timeline()
@ -1548,10 +1549,9 @@ class Controller(object):
try:
i.start_stream(mandatory=True)
except TweepyException as err:
log.exception("Error %s starting buffer %s on account %s, with args %r and kwargs %r due to the following reason: %s" % (err.api_code, i.name, i.account, i.args, i.kwargs, err.reason))
log.exception("Error %s starting buffer %s on account %s, with args %r and kwargs %r." % (str(err), i.name, i.account, i.args, i.kwargs))
# Determine if this error was caused by a block applied to the current user (IE permission errors).
errors_allowed = [130]
if (err.api_code != None and err.api_code not in errors_allowed) or (err.api_code == None and 'Not authorized' in err.reason): # A twitter error, so safely try to remove the buffer.
if type(err) == Forbidden:
buff = self.view.search(i.name, i.account)
i.remove_buffer(force=True)
commonMessageDialogs.blocked_timeline()

View File

@ -6,7 +6,7 @@ import output
from wxUI.dialogs import update_profile, show_user
import logging
log = logging.getLogger("controller.user")
from tweepy.errors import TweepyException
from tweepy.errors import TweepyException, Forbidden, NotFound
from sessions.twitter import utils
class profileController(object):
@ -25,11 +25,11 @@ class profileController(object):
try:
self.get_data(screen_name=self.user)
except TweepyException as err:
if err.api_code == 50:
if type(err) == NotFound:
wx.MessageDialog(None, _(u"That user does not exist"), _(u"Error"), wx.ICON_ERROR).ShowModal()
if err.api_code == 63:
if type(err) == Forbidden:
wx.MessageDialog(None, _(u"User has been suspended"), _(u"Error"), wx.ICON_ERROR).ShowModal()
log.error("error %d: %s" % (err.api_code, err.reason))
log.error("error %s" % (str(err)))
return
self.dialog = show_user.showUserProfile()
string = self.get_user_info()
@ -84,11 +84,11 @@ class profileController(object):
try:
self.session.twitter.update_profile_image(image=self.file)
except TweepyException as e:
output.speak(u"Error %s. %s" % (e.api_code, e.reason))
output.speak(u"Error %s" % (str(e)))
try:
self.session.twitter.update_profile(name=name, description=description, location=location, url=url)
except TweepyException as e:
output.speak(u"Error %s. %s" % (e.api_code, e.reason))
output.speak(u"Error %s." % (str(e)))
def get_user_info(self):
string = u""

View File

@ -30,43 +30,43 @@ class userActionsController(object):
try:
self.session.twitter.create_friendship(screen_name=user )
except TweepyException as err:
output.speak("Error %s: %s" % (err.api_code, err.reason), True)
output.speak("Error %s" % (str(err)), True)
def unfollow(self, user):
try:
id = self.session.twitter.destroy_friendship(screen_name=user )
except TweepyException as err:
output.speak("Error %s: %s" % (err.api_code, err.reason), True)
output.speak("Error %s" % (str(err)), True)
def mute(self, user):
try:
id = self.session.twitter.create_mute(screen_name=user )
except TweepyException as err:
output.speak("Error %s: %s" % (err.api_code, err.reason), True)
output.speak("Error %s" % (str(err)), True)
def unmute(self, user):
try:
id = self.session.twitter.destroy_mute(screen_name=user )
except TweepyException as err:
output.speak("Error %s: %s" % (err.api_code, err.reason), True)
output.speak("Error %s" % (str(err)), True)
def report(self, user):
try:
id = self.session.twitter.report_spam(screen_name=user )
except TweepyException as err:
output.speak("Error %s: %s" % (err.api_code, err.reason), True)
output.speak("Error %s" % (str(err)), True)
def block(self, user):
try:
id = self.session.twitter.create_block(screen_name=user )
except TweepyException as err:
output.speak("Error %s: %s" % (err.api_code, err.reason), True)
output.speak("Error %s" % (str(err)), True)
def unblock(self, user):
try:
id = self.session.twitter.destroy_block(screen_name=user )
except TweepyException as err:
output.speak("Error %s: %s" % (err.api_code, err.reason), True)
output.speak("Error %s" % (str(err)), True)
def ignore_client(self, user):
tweet = self.buffer.get_right_tweet()

View File

@ -10,7 +10,7 @@ import output
import application
from pubsub import pub
import tweepy
from tweepy.errors import TweepyException
from tweepy.errors import TweepyException, Forbidden, NotFound
from tweepy.models import User as UserModel
from mysc.thread_utils import call_threaded
from keys import keyring
@ -200,13 +200,13 @@ class Session(base.baseSession):
val = getattr(self.twitter, call_name)(*args, **kwargs)
finished = True
except TweepyException as e:
output.speak(e.reason)
output.speak(str(e))
val = None
if e.error_code != 403 and e.error_code != 404:
if type(e) != NotFound and type(e) != Forvidden:
tries = tries+1
time.sleep(5)
elif report_failure and hasattr(e, 'reason'):
output.speak(_("%s failed. Reason: %s") % (action, e.reason))
elif report_failure:
output.speak(_("%s failed. Reason: %s") % (action, str(e)))
finished = True
# except:
# tries = tries + 1
@ -422,7 +422,7 @@ class Session(base.baseSession):
user.screen_name = "deleted_user"
user.id = id
user.name = _("Deleted account")
if hasattr(err, "api_code") and err.api_code == 50:
if type(err) == NotFound:
self.deleted_users[id] = user
return user
else:
@ -490,7 +490,7 @@ class Session(base.baseSession):
log.debug("Added %d new users" % (len(users)))
self.db["users"] = users_db
except TweepyException as err:
if hasattr(err, "api_code") and err.api_code == 17: # Users not found.
if type(err) == NotFound: # User not found.
log.error("The specified users {} were not found in twitter.".format(user_ids))
# Creates a deleted user object for every user_id not found here.
# This will make TWBlue to not waste Twitter API calls when attempting to retrieve those users again.

View File

@ -6,7 +6,7 @@ import logging
import requests
import time
import sound
from tweepy.errors import TweepyException
from tweepy.errors import TweepyException, NotFound, Forbidden
log = logging.getLogger("twitter.utils")
""" Some utilities for the twitter interface."""
@ -160,7 +160,7 @@ def if_user_exists(twitter, user):
data = twitter.get_user(screen_name=user)
return data
except TweepyException as err:
if err.api_code == 50:
if type(err) == NotFound:
return None
else:
return user
@ -227,12 +227,12 @@ def filter_tweet(tweet, tweet_data, settings, buffer_name):
return True
def twitter_error(error):
if error.api_code == 179:
if type(error) == Forbidden:
msg = _(u"Sorry, you are not authorised to see this status.")
elif error.api_code == 144:
elif type(error) == NotFound:
msg = _(u"No status found with that ID")
else:
msg = _(u"Error code {0}").format(error.api_code,)
msg = _(u"Error {0}").format(str(error),)
output.speak(msg)
def expand_urls(text, entities):