From 6fcd0274a9a99825ee3a05ca0fd7f3805fec51e8 Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Fri, 29 Jul 2022 13:30:43 -0500 Subject: [PATCH] Added exception handling to scan for users --- src/extra/autocompletionUsers/scan.py | 22 +++++++++++++++------- src/extra/autocompletionUsers/wx_scan.py | 7 ++++++- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/extra/autocompletionUsers/scan.py b/src/extra/autocompletionUsers/scan.py index 8decea35..c699e27a 100644 --- a/src/extra/autocompletionUsers/scan.py +++ b/src/extra/autocompletionUsers/scan.py @@ -39,7 +39,6 @@ class autocompletionScan(object): scanner = call_threaded(self.scan) def on_update_progress(self, percent): - print(percent) if percent > 100: percent = 100 wx.CallAfter(self.progress_dialog.Update, percent) @@ -59,25 +58,34 @@ class autocompletionScan(object): ids.append(str(i)) # same step, but for followers. if self.dialog.get("followers") == True: - for i in Cursor(self.buffer.session.twitter.get_follower_ids, count=5000).items(): - if str(i) not in ids: - ids.append(str(i)) + try: + for i in Cursor(self.buffer.session.twitter.get_follower_ids, count=5000).items(): + if str(i) not in ids: + ids.append(str(i)) + except TweepyException: + wx.CallAfter(wx_scan.show_error) + self.done() # As next step requires batches of 100s users, let's split our user Ids so we won't break the param rules. split_users = [ids[i:i + 100] for i in range(0, len(ids), 100)] # store returned users in this list. users = [] for z in split_users: if len(z) == 0: - print("Invalid user count") continue - print(len(z)) - results = self.buffer.session.twitter.lookup_users(user_id=z) + try: + results = selff.buffer.session.twitter.lookup_users(user_id=z) + except TweepyException: + wx.CallAfter(wx_scan.show_error) + self.done() users.extend(results) time.sleep(1) percent = percent + (100/len(split_users)) pub.sendMessage("on-update-progress", percent=percent) for user in users: database.set_user(user.screen_name, user.name, 1) + self.done() + + def done(self): wx.CallAfter(self.progress_dialog.Destroy) wx.CallAfter(self.dialog.Destroy) pub.unsubscribe(self.on_update_progress, "on-update-progress") diff --git a/src/extra/autocompletionUsers/wx_scan.py b/src/extra/autocompletionUsers/wx_scan.py index 24255174..f7c2b3eb 100644 --- a/src/extra/autocompletionUsers/wx_scan.py +++ b/src/extra/autocompletionUsers/wx_scan.py @@ -22,10 +22,15 @@ class autocompletionScanDialog(widgetUtils.BaseDialog): self.SetClientSize(sizer.CalcMin()) def confirm(): - with wx.MessageDialog(None, _("This process will retrieve the users you selected from Twitter, and add them to the user autocomplete database. Please note that if there are many users or you have tried to perform this action less than 15 minutes ago, TWBlue may reach a limit in Twitter API calls when trying to load the users into the database. If this happens, we will show you an error, in which case you will have to try this process again in a few minutes. If this process finish with no error, you will be redirected back to the account settings dialog. Do you want to continue?"), _("Attention"), style=wx.ICON_QUESTION|wx.YES_NO) as result: + with wx.MessageDialog(None, _("This process will retrieve the users you selected from Twitter, and add them to the user autocomplete database. Please note that if there are many users or you have tried to perform this action less than 15 minutes ago, TWBlue may reach a limit in Twitter API calls when trying to load the users into the database. If this happens, we will show you an error, in which case you will have to try this process again in a few minutes. If this process ends with no error, you will be redirected back to the account settings dialog. Do you want to continue?"), _("Attention"), style=wx.ICON_QUESTION|wx.YES_NO) as result: if result.ShowModal() == wx.ID_YES: return True return False def get_progress_dialog(parent=None): return wx.ProgressDialog(_("Retrieving Twitter users from account..."), _("working..."), parent=parent, maximum=100, style=wx.PD_APP_MODAL) + +def show_error(): + dlg = wx.MessageDialog(None, _("Error adding users from Twitter. Please try again in about 15 minutes."), _("Error"), style=wx.ICON_ERROR) + dlg.ShowModal() + dlg.Destroy() \ No newline at end of file