2015-04-15 11:09:36 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import widgetUtils
|
|
|
|
import output
|
2021-01-19 17:47:54 -06:00
|
|
|
import logging
|
2015-04-15 11:09:36 -05:00
|
|
|
from wxUI.dialogs import lists
|
2021-01-19 17:47:54 -06:00
|
|
|
from tweepy.error import TweepError
|
2018-08-16 17:26:19 -05:00
|
|
|
from sessions.twitter import compose, utils
|
2015-06-05 05:21:49 -05:00
|
|
|
from pubsub import pub
|
2015-04-15 11:09:36 -05:00
|
|
|
|
2021-01-19 17:47:54 -06:00
|
|
|
log = logging.getLogger("controller.listsController")
|
|
|
|
|
2015-04-15 11:09:36 -05:00
|
|
|
class listsController(object):
|
2021-06-16 16:18:41 -05:00
|
|
|
def __init__(self, session, user=None):
|
|
|
|
super(listsController, self).__init__()
|
|
|
|
self.session = session
|
|
|
|
if user == None:
|
|
|
|
self.dialog = lists.listViewer()
|
|
|
|
self.dialog.populate_list(self.get_all_lists())
|
|
|
|
widgetUtils.connect_event(self.dialog.createBtn, widgetUtils.BUTTON_PRESSED, self.create_list)
|
|
|
|
widgetUtils.connect_event(self.dialog.editBtn, widgetUtils.BUTTON_PRESSED, self.edit_list)
|
|
|
|
widgetUtils.connect_event(self.dialog.deleteBtn, widgetUtils.BUTTON_PRESSED, self.remove_list)
|
|
|
|
widgetUtils.connect_event(self.dialog.view, widgetUtils.BUTTON_PRESSED, self.open_list_as_buffer)
|
|
|
|
widgetUtils.connect_event(self.dialog.deleteBtn, widgetUtils.BUTTON_PRESSED, self.remove_list)
|
|
|
|
else:
|
|
|
|
self.dialog = lists.userListViewer(user)
|
|
|
|
self.dialog.populate_list(self.get_user_lists(user))
|
|
|
|
widgetUtils.connect_event(self.dialog.createBtn, widgetUtils.BUTTON_PRESSED, self.subscribe)
|
|
|
|
widgetUtils.connect_event(self.dialog.deleteBtn, widgetUtils.BUTTON_PRESSED, self.unsubscribe)
|
|
|
|
self.dialog.get_response()
|
2015-04-15 11:09:36 -05:00
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def get_all_lists(self):
|
|
|
|
return [compose.compose_list(item) for item in self.session.db["lists"]]
|
2015-04-15 11:09:36 -05:00
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def get_user_lists(self, user):
|
|
|
|
self.lists = self.session.twitter.lists_all(reverse=True, screen_name=user)
|
|
|
|
return [compose.compose_list(item) for item in self.lists]
|
2015-09-29 09:22:19 -05:00
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def create_list(self, *args, **kwargs):
|
|
|
|
dialog = lists.createListDialog()
|
|
|
|
if dialog.get_response() == widgetUtils.OK:
|
|
|
|
name = dialog.get("name")
|
|
|
|
description = dialog.get("description")
|
|
|
|
p = dialog.get("public")
|
|
|
|
if p == True:
|
|
|
|
mode = "public"
|
|
|
|
else:
|
|
|
|
mode = "private"
|
|
|
|
try:
|
|
|
|
new_list = self.session.twitter.create_list(name=name, description=description, mode=mode)
|
|
|
|
self.session.db["lists"].append(new_list)
|
|
|
|
self.dialog.lista.insert_item(False, *compose.compose_list(new_list))
|
|
|
|
except TweepError as e:
|
|
|
|
output.speak("error %s: %s" % (e.api_code, e.reason))
|
|
|
|
log.exception("error %s: %s" % (e.api_code, e.reason))
|
|
|
|
dialog.destroy()
|
2015-04-15 11:09:36 -05:00
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def edit_list(self, *args, **kwargs):
|
|
|
|
if self.dialog.lista.get_count() == 0: return
|
|
|
|
list = self.session.db["lists"][self.dialog.get_item()]
|
|
|
|
dialog = lists.editListDialog(list)
|
|
|
|
if dialog.get_response() == widgetUtils.OK:
|
|
|
|
name = dialog.get("name")
|
|
|
|
description = dialog.get("description")
|
|
|
|
p = dialog.get("public")
|
|
|
|
if p == True:
|
|
|
|
mode = "public"
|
|
|
|
else:
|
|
|
|
mode = "private"
|
|
|
|
try:
|
|
|
|
self.session.twitter.update_list(list_id=list.id, name=name, description=description, mode=mode)
|
|
|
|
self.session.get_lists()
|
|
|
|
self.dialog.populate_list(self.get_all_lists(), True)
|
|
|
|
except TweepError as e:
|
|
|
|
output.speak("error %s: %s" % (e.api_code, e.reason))
|
|
|
|
dialog.destroy()
|
2015-04-15 11:09:36 -05:00
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def remove_list(self, *args, **kwargs):
|
|
|
|
if self.dialog.lista.get_count() == 0: return
|
|
|
|
list = self.session.db["lists"][self.dialog.get_item()].id
|
|
|
|
if lists.remove_list() == widgetUtils.YES:
|
|
|
|
try:
|
|
|
|
self.session.twitter.destroy_list(list_id=list)
|
|
|
|
self.session.db["lists"].pop(self.dialog.get_item())
|
|
|
|
self.dialog.lista.remove_item(self.dialog.get_item())
|
|
|
|
except TweepError as e:
|
|
|
|
output.speak("error %s: %s" % (e.api_code, e.reason))
|
2015-06-05 05:21:49 -05:00
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def open_list_as_buffer(self, *args, **kwargs):
|
|
|
|
if self.dialog.lista.get_count() == 0: return
|
|
|
|
list = self.session.db["lists"][self.dialog.get_item()]
|
|
|
|
pub.sendMessage("create-new-buffer", buffer="list", account=self.session.db["user_name"], create=list.name)
|
2015-10-03 04:34:43 -05:00
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def subscribe(self, *args, **kwargs):
|
|
|
|
if self.dialog.lista.get_count() == 0: return
|
|
|
|
list_id = self.lists[self.dialog.get_item()].id
|
|
|
|
try:
|
|
|
|
list = self.session.twitter.subscribe_list(list_id=list_id)
|
|
|
|
item = utils.find_item(list.id, self.session.db["lists"])
|
|
|
|
self.session.db["lists"].append(list)
|
|
|
|
except TweepError as e:
|
|
|
|
output.speak("error %s: %s" % (e.api_code, e.reason))
|
2015-09-29 09:22:19 -05:00
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def unsubscribe(self, *args, **kwargs):
|
|
|
|
if self.dialog.lista.get_count() == 0: return
|
|
|
|
list_id = self.lists[self.dialog.get_item()].id
|
|
|
|
try:
|
|
|
|
list = self.session.twitter.unsubscribe_list(list_id=list_id)
|
|
|
|
self.session.db["lists"].remove(list)
|
|
|
|
except TweepError as e:
|
|
|
|
output.speak("error %s: %s" % (e.api_code, e.reason))
|