cleaned scan controller and made progress dialog to work

This commit is contained in:
Manuel Cortez 2022-07-29 12:11:03 -05:00
parent 6e80b320c9
commit 02e1793d08
No known key found for this signature in database
GPG Key ID: 9E0735CA15EFE790

View File

@ -1,17 +1,28 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" Scanning code for autocompletion feature on TWBlue. This module can retrieve user objects from the selected Twitter account automatically. """
import time import time
import wx
import widgetUtils import widgetUtils
import output import output
from tweepy.cursor import Cursor from tweepy.cursor import Cursor
from tweepy.errors import TweepyException from tweepy.errors import TweepyException
from pubsub import pub from pubsub import pub
from mysc.thread_utils import call_threaded
from . import wx_scan from . import wx_scan
from . import manage from . import manage
from . import storage from . import storage
from mysc.thread_utils import call_threaded
class autocompletionScan(object): class autocompletionScan(object):
def __init__(self, config, buffer, window): def __init__(self, config, buffer, window):
""" Class constructor. This class will take care of scanning the selected Twitter account to populate the database with users automatically upon request.
:param config: Config for the session that will be scanned in search for users.
:type config: dict
:param buffer: home buffer for the focused session.
:type buffer: controller.buffers.twitter.base.baseBuffer
:param window: Main Window of TWBlue.
:type window:wx.Frame
"""
super(autocompletionScan, self).__init__() super(autocompletionScan, self).__init__()
self.config = config self.config = config
self.buffer = buffer self.buffer = buffer
@ -22,16 +33,16 @@ class autocompletionScan(object):
if self.dialog.get_response() == widgetUtils.OK: if self.dialog.get_response() == widgetUtils.OK:
confirmation = wx_scan.confirm() confirmation = wx_scan.confirm()
if confirmation == True: if confirmation == True:
self.progress_dialog = wx_scan.get_progress_dialog() self.progress_dialog = wx_scan.get_progress_dialog(parent=self.dialog)
call_threaded(self.scan) # connect method to update progress dialog
# connect method to update progress dialog pub.subscribe(self.on_update_progress, "on-update-progress")
pub.subscribe(self.on_update_progress, "on-update-progress") scanner = call_threaded(self.scan)
def on_update_progress(self, percent): def on_update_progress(self, percent):
print(percent) print(percent)
if percent > 100: if percent > 100:
percent = 100 percent = 100
self.progress_dialog.Update(percent) wx.CallAfter(self.progress_dialog.Update, percent)
def scan(self): def scan(self):
""" Attempts to add all users selected by current user to the autocomplete database. """ """ Attempts to add all users selected by current user to the autocomplete database. """
@ -40,27 +51,17 @@ class autocompletionScan(object):
self.config["mysc"]["save_followers_in_autocompletion_db"] = self.dialog.get("followers") self.config["mysc"]["save_followers_in_autocompletion_db"] = self.dialog.get("followers")
output.speak(_("Updating database... You can close this window now. A message will tell you when the process finishes.")) output.speak(_("Updating database... You can close this window now. A message will tell you when the process finishes."))
database = storage.storage(self.buffer.session.session_id) database = storage.storage(self.buffer.session.session_id)
total_steps = 0
if self.dialog.get("friends") == True:
total_steps = total_steps + 2
if self.dialog.get("followers") == True:
total_steps = total_steps + 2
max_per_stage = 100/total_steps
percent = 0 percent = 0
# Retrieve ids of all following users # Retrieve ids of all following users
if self.dialog.get("friends") == True: if self.dialog.get("friends") == True:
for i in Cursor(self.buffer.session.twitter.get_friend_ids, count=5000).items(): for i in Cursor(self.buffer.session.twitter.get_friend_ids, count=5000).items():
if str(i) not in ids: if str(i) not in ids:
ids.append(str(i)) ids.append(str(i))
percent = percent + (100*max_per_stage/100)
pub.sendMessage("on-update-progress", percent=percent)
# same step, but for followers. # same step, but for followers.
if self.dialog.get("followers") == True: if self.dialog.get("followers") == True:
for i in Cursor(self.buffer.session.twitter.get_follower_ids, count=5000).items(): for i in Cursor(self.buffer.session.twitter.get_follower_ids, count=5000).items():
if str(i) not in ids: if str(i) not in ids:
ids.append(str(i)) ids.append(str(i))
percent = percent + (100*max_per_stage/100)
pub.sendMessage("on-update-progress", percent=percent)
# As next step requires batches of 100s users, let's split our user Ids so we won't break the param rules. # 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)] split_users = [ids[i:i + 100] for i in range(0, len(ids), 100)]
# store returned users in this list. # store returned users in this list.
@ -73,37 +74,13 @@ class autocompletionScan(object):
results = self.buffer.session.twitter.lookup_users(user_id=z) results = self.buffer.session.twitter.lookup_users(user_id=z)
users.extend(results) users.extend(results)
time.sleep(1) time.sleep(1)
percent = percent + (max_per_stage/len(split_users)) percent = percent + (100/len(split_users))
pub.sendMessage("on-update-progress", percent=percent) pub.sendMessage("on-update-progress", percent=percent)
for user in users: for user in users:
database.set_user(user.screen_name, user.name, 1) database.set_user(user.screen_name, user.name, 1)
self.progress_dialog.Destroy() wx.CallAfter(self.progress_dialog.Destroy)
wx_scan.show_success_dialog() wx.CallAfter(self.dialog.Destroy)
self.dialog.Destroy() pub.unsubscribe(self.on_update_progress, "on-update-progress")
def add_users_to_database(self):
self.config["mysc"]["save_friends_in_autocompletion_db"] = self.dialog.get("friends_buffer")
self.config["mysc"]["save_followers_in_autocompletion_db"] = self.dialog.get("followers_buffer")
output.speak(_(u"Updating database... You can close this window now. A message will tell you when the process finishes."))
database = storage.storage(self.buffer.session.session_id)
if self.dialog.get("followers_buffer") == True:
buffer = self.window.search_buffer("followers", self.config["twitter"]["user_name"])
for i in buffer.session.db[buffer.name]:
database.set_user(i.screen_name, i.name, 1)
else:
database.remove_by_buffer(1)
if self.dialog.get("friends_buffer") == True:
buffer = self.window.search_buffer("friends", self.config["twitter"]["user_name"])
for i in buffer.session.db[buffer.name]:
database.set_user(i.screen_name, i.name, 2)
else:
database.remove_by_buffer(2)
wx_settings.show_success_dialog()
self.dialog.destroy()
def view_list(self, ev):
q = manage.autocompletionManager(self.buffer.session)
q.show_settings()
def execute_at_startup(window, buffer, config): def execute_at_startup(window, buffer, config):
database = storage.storage(buffer.session.session_id) database = storage.storage(buffer.session.session_id)
@ -118,7 +95,4 @@ def execute_at_startup(window, buffer, config):
for i in buffer.session.db[buffer.name]: for i in buffer.session.db[buffer.name]:
database.set_user(i.screen_name, i.name, 2) database.set_user(i.screen_name, i.name, 2)
else: else:
database.remove_by_buffer(2) database.remove_by_buffer(2)
def __del__(self):
pub.unsubscribe(self.on_update_progress, "on-update-progress")