mirror of
https://github.com/MCV-Software/TWBlue.git
synced 2025-08-27 10:19:22 +00:00
Mastodon: Added user autocompletion module (not yet implemented on GUI)
This commit is contained in:
0
src/extra/autocompletionUsers/mastodon/__init__.py
Normal file
0
src/extra/autocompletionUsers/mastodon/__init__.py
Normal file
103
src/extra/autocompletionUsers/mastodon/scan.py
Normal file
103
src/extra/autocompletionUsers/mastodon/scan.py
Normal file
@@ -0,0 +1,103 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
""" Scanning code for autocompletion feature on TWBlue. This module can retrieve user objects from the selected Mastodon account automatically. """
|
||||
import time
|
||||
import wx
|
||||
import widgetUtils
|
||||
import output
|
||||
from pubsub import pub
|
||||
from . import wx_scan
|
||||
from extra.autocompletionUsers import manage, storage
|
||||
|
||||
class autocompletionScan(object):
|
||||
def __init__(self, config, buffer, window):
|
||||
""" Class constructor. This class will take care of scanning the selected Mastodon 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.mastodon.base.baseBuffer
|
||||
:param window: Main Window of TWBlue.
|
||||
:type window:wx.Frame
|
||||
"""
|
||||
super(autocompletionScan, self).__init__()
|
||||
self.config = config
|
||||
self.buffer = buffer
|
||||
self.window = window
|
||||
|
||||
def show_dialog(self):
|
||||
""" displays a dialog to confirm which buffers should be scanned (followers or following users). """
|
||||
self.dialog = wx_scan.autocompletionScanDialog()
|
||||
self.dialog.set("friends", self.config["mysc"]["save_friends_in_autocompletion_db"])
|
||||
self.dialog.set("followers", self.config["mysc"]["save_followers_in_autocompletion_db"])
|
||||
if self.dialog.get_response() == widgetUtils.OK:
|
||||
confirmation = wx_scan.confirm()
|
||||
return confirmation
|
||||
|
||||
def prepare_progress_dialog(self):
|
||||
self.progress_dialog = wx_scan.autocompletionScanProgressDialog()
|
||||
# connect method to update progress dialog
|
||||
pub.subscribe(self.on_update_progress, "on-update-progress")
|
||||
self.progress_dialog.Show()
|
||||
|
||||
def on_update_progress(self):
|
||||
wx.CallAfter(self.progress_dialog.progress_bar.Pulse)
|
||||
|
||||
def scan(self):
|
||||
""" Attempts to add all users selected by current user to the autocomplete database. """
|
||||
self.config["mysc"]["save_friends_in_autocompletion_db"] = self.dialog.get("friends")
|
||||
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."))
|
||||
database = storage.storage(self.buffer.session.session_id)
|
||||
percent = 0
|
||||
users = []
|
||||
if self.dialog.get("friends") == True:
|
||||
first_page = self.buffer.session.api.account_following(id=self.buffer.session.db["user_id"], limit=80)
|
||||
pub.sendMessage("on-update-progress")
|
||||
if first_page != None:
|
||||
for user in first_page:
|
||||
users.append(user)
|
||||
next_page = first_page
|
||||
while next_page != None:
|
||||
next_page = self.buffer.session.api.fetch_next(next_page)
|
||||
pub.sendMessage("on-update-progress")
|
||||
if next_page == None:
|
||||
break
|
||||
for user in next_page:
|
||||
users.append(user)
|
||||
# same step, but for followers.
|
||||
if self.dialog.get("followers") == True:
|
||||
first_page = self.buffer.session.api.account_followers(id=self.buffer.session.db["user_id"], limit=80)
|
||||
pub.sendMessage("on-update-progress")
|
||||
if first_page != None:
|
||||
for user in first_page:
|
||||
if user not in users:
|
||||
users.append(user)
|
||||
next_page = first_page
|
||||
while next_page != None:
|
||||
next_page = self.buffer.session.api.fetch_next(next_page)
|
||||
pub.sendMessage("on-update-progress")
|
||||
if next_page == None:
|
||||
break
|
||||
for user in next_page:
|
||||
if user not in users:
|
||||
users.append(user)
|
||||
# except TweepyException:
|
||||
# wx.CallAfter(wx_scan.show_error)
|
||||
# return self.done()
|
||||
for user in users:
|
||||
name = user.display_name if user.display_name != None and user.display_name != "" else user.username
|
||||
database.set_user(user.acct, name, 1)
|
||||
wx.CallAfter(wx_scan .show_success, len(users))
|
||||
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")
|
||||
|
||||
def add_user(session, database, user):
|
||||
""" Adds an user to the database. """
|
||||
user = session.api.account_lookup(user)
|
||||
if user != None:
|
||||
name = user.display_name if user.display_name != None and user.display_name != "" else user.username
|
||||
database.set_user(user.acct, name, 1)
|
45
src/extra/autocompletionUsers/mastodon/wx_scan.py
Normal file
45
src/extra/autocompletionUsers/mastodon/wx_scan.py
Normal file
@@ -0,0 +1,45 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import wx
|
||||
import widgetUtils
|
||||
import application
|
||||
|
||||
class autocompletionScanDialog(widgetUtils.BaseDialog):
|
||||
def __init__(self):
|
||||
super(autocompletionScanDialog, self).__init__(parent=None, id=-1, title=_(u"Autocomplete users' settings"))
|
||||
panel = wx.Panel(self)
|
||||
sizer = wx.BoxSizer(wx.VERTICAL)
|
||||
self.followers = wx.CheckBox(panel, -1, _("Add followers to database"))
|
||||
self.friends = wx.CheckBox(panel, -1, _("Add following to database"))
|
||||
sizer.Add(self.followers, 0, wx.ALL, 5)
|
||||
sizer.Add(self.friends, 0, wx.ALL, 5)
|
||||
ok = wx.Button(panel, wx.ID_OK)
|
||||
cancel = wx.Button(panel, wx.ID_CANCEL)
|
||||
sizerBtn = wx.BoxSizer(wx.HORIZONTAL)
|
||||
sizerBtn.Add(ok, 0, wx.ALL, 5)
|
||||
sizer.Add(cancel, 0, wx.ALL, 5)
|
||||
sizer.Add(sizerBtn, 0, wx.ALL, 5)
|
||||
panel.SetSizer(sizer)
|
||||
self.SetClientSize(sizer.CalcMin())
|
||||
|
||||
class autocompletionScanProgressDialog(widgetUtils.BaseDialog):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(autocompletionScanProgressDialog, self).__init__(parent=None, id=wx.ID_ANY, title=_("Updating autocompletion database"), *args, **kwargs)
|
||||
panel = wx.Panel(self)
|
||||
sizer = wx.BoxSizer(wx.VERTICAL)
|
||||
self.progress_bar = wx.Gauge(parent=panel)
|
||||
sizer.Add(self.progress_bar)
|
||||
panel.SetSizerAndFit(sizer)
|
||||
|
||||
def confirm():
|
||||
with wx.MessageDialog(None, _("This process will retrieve the users you selected from your Mastodon account, 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 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 show_success(users):
|
||||
with wx.MessageDialog(None, _("TWBlue has imported {} users successfully.").format(users), _("Done")) as dlg:
|
||||
dlg.ShowModal()
|
||||
|
||||
def show_error():
|
||||
with wx.MessageDialog(None, _("Error adding users from Mastodon. Please try again in about 15 minutes."), _("Error"), style=wx.ICON_ERROR) as dlg:
|
||||
dlg.ShowModal()
|
Reference in New Issue
Block a user