From de7118caf4f2f9e7d4a95630ca47b0dfc56784f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Cort=C3=A9z?= Date: Thu, 12 Feb 2015 10:29:51 -0600 Subject: [PATCH] Tweet's timelines are possible, a way to restart stream endpoint has been created --- src/controller/mainController.py | 35 ++++++++++++++++++++-- src/sessionmanager/session.py | 10 ++++++- src/twitter/utils.py | 7 +++-- src/wxUI/commonMessageDialogs.py | 13 ++++++++- src/wxUI/dialogs/__init__.py | 2 +- src/wxUI/dialogs/userSelection.py | 48 ++++++++++++++++++++++--------- 6 files changed, 95 insertions(+), 20 deletions(-) diff --git a/src/controller/mainController.py b/src/controller/mainController.py index 5391c498..5f776964 100644 --- a/src/controller/mainController.py +++ b/src/controller/mainController.py @@ -95,6 +95,7 @@ class Controller(object): pub.subscribe(self.editing_keystroke, "editing_keystroke") pub.subscribe(self.manage_stream_errors, "stream-error") pub.subscribe(self.create_new_buffer, "create-new-buffer") + pub.subscribe(self.restart_streams, "restart-streams") widgetUtils.connect_event(self.view, widgetUtils.MENU, self.show_hide, menuitem=self.view.show_hide) widgetUtils.connect_event(self.view, widgetUtils.MENU, self.search, menuitem=self.view.menuitem_search) widgetUtils.connect_event(self.view, widgetUtils.MENU, self.get_trending_topics, menuitem=self.view.trends) @@ -462,8 +463,31 @@ class Controller(object): non_tweet = buffer.get_formatted_message() msg = messages.viewTweet(non_tweet, False) - def open_timeline(self, user, timeline_tipe): - pass + def open_timeline(self, *args, **kwargs): + buff = self.get_current_buffer() + if not hasattr(buff, "get_right_tweet"): return + tweet = buff.get_right_tweet() + if buff.type != "people": + users = utils.get_all_users(tweet, buff.session.db) + else: + users = [tweet["screen_name"]] + dlg = dialogs.userSelection.selectUserDialog(users=users) + if dlg.get_response() == widgetUtils.OK: + buffer = self.get_best_buffer() + if utils.if_user_exists(buffer.session.twitter.twitter, dlg.get_user()) != None: + if dlg.get_action() == "tweets": + if dlg.get_user() in buffer.session.settings["other_buffers"]["timelines"]: + commonMessageDialogs.timeline_exist() + return + tl = buffersController.baseBufferController(self.view.nb, "get_user_timeline", "%s-timeline" % (dlg.get_user(),), buffer.session, buffer.session.db["user_name"], bufferType=None, screen_name=dlg.get_user()) + tl.start_stream() + self.buffers.append(tl) + self.view.insert_buffer(tl.buffer, name=_(u"Timeline for {}".format(dlg.get_user())), pos=self.view.search("timelines", buffer.session.db["user_name"])) + buffer.session.settings["other_buffers"]["timelines"].append(dlg.get_user()) + pub.sendMessage("restart-streams", streams=["timelinesStream"], session=buffer.session) + buffer.session.sound.play("create_timeline.ogg") + else: + commonMessageDialogs.user_not_exist() def show_hide(self, *args, **kwargs): km = self.create_invisible_keyboard_shorcuts() @@ -796,6 +820,7 @@ class Controller(object): def manage_friend(self, data, user): buffer = self.search_buffer("friends", user) buffer.add_new_item(data) + pub.sendMessage("restart-streams", streams=["main_stream"], session=buffer.session) def manage_unfollowing(self, item, user): buffer = self.search_buffer("friends", user) @@ -880,5 +905,11 @@ class Controller(object): self.buffers.append(events) self.view.insert_buffer(events.buffer, name=_(u"Events"), pos=self.view.search(buff.session.db["user_name"], buff.session.db["user_name"])) + def restart_streams(self, streams=[], session=None): + for i in streams: + log.debug("RReconnecting the %s stream" % (i,)) + session.remove_stream(i) + session.check_connection() + def __del__(self): config.app.write() \ No newline at end of file diff --git a/src/sessionmanager/session.py b/src/sessionmanager/session.py index 80d1f10e..fc52a9ab 100644 --- a/src/sessionmanager/session.py +++ b/src/sessionmanager/session.py @@ -318,4 +318,12 @@ class Session(object): try: urllib2.urlopen("http://74.125.228.231", timeout=5) except urllib2.URLError: - pub.sendMessage("stream-error", session=self.session_id) \ No newline at end of file + pub.sendMessage("stream-error", session=self.session_id) + + def remove_stream(self, stream): + if stream == "timelinesStream": + self.timelinesStream.disconnect() + del self.timelinesStream + else: + self.main_stream.disconnect() + del self.main_stream \ No newline at end of file diff --git a/src/twitter/utils.py b/src/twitter/utils.py index 274b518a..8f2a4ecc 100644 --- a/src/twitter/utils.py +++ b/src/twitter/utils.py @@ -83,8 +83,11 @@ def if_user_exists(twitter, user): try: data = twitter.show_user(screen_name=user) return data["screen_name"] - except: - return None + except TwythonError as err: + if err.error_code == 404: + return None + else: + return user def api_call(parent=None, call_name=None, preexec_message="", success="", success_snd="", *args, **kwargs): if preexec_message: diff --git a/src/wxUI/commonMessageDialogs.py b/src/wxUI/commonMessageDialogs.py index c84acb5c..26d467e6 100644 --- a/src/wxUI/commonMessageDialogs.py +++ b/src/wxUI/commonMessageDialogs.py @@ -13,6 +13,7 @@ def exit_dialog(): def needs_restart(): wx.MessageDialog(None, _(u"The application requires to be restarted to save these changes. Press OK to do it now."), _("Restart TW Blue"), wx.OK).ShowModal() + def delete_user_from_db(): return wx.MessageDialog(None, _(u"Are you sure you want to delete this user from the database? This user will not appear on the autocomplete results anymore."), _(u"Confirm"), wx.YES_NO|wx.ICON_QUESTION).ShowModal() @@ -25,5 +26,15 @@ def get_ignored_client(): def clear_list(): dlg = wx.MessageDialog(None, _(u"Do you really want to empty this buffer? It's items will be removed from the list but not from Twitter"), _(u"Empty buffer"), wx.ICON_QUESTION|wx.YES_NO) return dlg.ShowModal() + def remove_buffer(): - return wx.MessageDialog(None, _(u"Do you really want to delete this timeline?"), _(u"Attention"), style=wx.ICON_QUESTION|wx.YES_NO).ShowModal() \ No newline at end of file + return wx.MessageDialog(None, _(u"Do you really want to delete this timeline?"), _(u"Attention"), style=wx.ICON_QUESTION|wx.YES_NO).ShowModal() + +def user_not_exist(): + return wx.MessageDialog(None, _(u"The user does not exist"), _(u"Error"), wx.ICON_ERROR).ShowModal() + +def timeline_exist(): + return wx.MessageDialog(None, _(u"There's currently a timeline for this user. You are not able to open another"), _(u"Existing timeline"), wx.ICON_ERROR).ShowModal() + +def no_tweets(): + return wx.MessageDialog(None, _(u"This user has no tweets. You can't open a timeline for this user"), _(u"Error!"), wx.ICON_ERROR).ShowModal() diff --git a/src/wxUI/dialogs/__init__.py b/src/wxUI/dialogs/__init__.py index f9de3bea..47d00ab9 100644 --- a/src/wxUI/dialogs/__init__.py +++ b/src/wxUI/dialogs/__init__.py @@ -1 +1 @@ -import baseDialog, trends, configuration, lists, message, search, show_user, update_profile, urlList +import baseDialog, trends, configuration, lists, message, search, show_user, update_profile, urlList, userSelection diff --git a/src/wxUI/dialogs/userSelection.py b/src/wxUI/dialogs/userSelection.py index a615775f..22a3711f 100644 --- a/src/wxUI/dialogs/userSelection.py +++ b/src/wxUI/dialogs/userSelection.py @@ -2,28 +2,50 @@ import wx class selectUserDialog(wx.Dialog): - def __init__(self, users, *args, **kwargs): - super(selectUserDialog, self).__init__(self, None, -1, *args, **kwargs) + def __init__(self, users=[], default="tweets", *args, **kwargs): + super(selectUserDialog, self).__init__(parent=None, *args, **kwargs) panel = wx.Panel(self) userSizer = wx.BoxSizer() - self.cb = wx.ComboBox(panel, -1, choices=users, value=users[0], size=wx.DefaultSize) + self.SetTitle(_(u"Timeline for %s") % (users[0])) + userLabel = wx.StaticText(panel, -1, _(u"User")) + self.cb = wx.ComboBox(panel, -1, choices=users, value=users[0]) self.cb.SetFocus() - userSizer.Add(wx.StaticText(panel, -1, _(u"User")), 0, wx.ALL, 5) - userSizer.Add(self.cb) + userSizer.Add(userLabel, 0, wx.ALL, 5) + userSizer.Add(self.cb, 0, wx.ALL, 5) + actionSizer = wx.BoxSizer(wx.VERTICAL) + label2 = wx.StaticText(panel, -1, _(u"Buffer type")) + self.tweets = wx.RadioButton(panel, -1, _(u"Tweets"), style=wx.RB_GROUP) + self.favourites = wx.RadioButton(panel, -1, _(u"Favourites")) + self.setup_default(default) + hSizer = wx.BoxSizer(wx.HORIZONTAL) + hSizer.Add(label2, 0, wx.ALL, 5) + actionSizer.Add(self.tweets, 0, wx.ALL, 5) + actionSizer.Add(self.favourites, 0, wx.ALL, 5) + hSizer.Add(actionSizer, 0, wx.ALL, 5) sizer = wx.BoxSizer(wx.VERTICAL) ok = wx.Button(panel, wx.ID_OK, _(u"OK")) ok.SetDefault() cancel = wx.Button(panel, wx.ID_CANCEL, _(u"Close")) btnsizer = wx.BoxSizer() - btnsizer.Add(ok, 0, wx.ALL, 5) - btnsizer.Add(cancel, 0, wx.ALL, 5) - sizer.Add(userSizer, 0, wx.ALL, 5) - sizer.Add(btnsizer, 0, wx.ALL, 5) + btnsizer.Add(ok) + btnsizer.Add(cancel) + sizer.Add(userSizer) + sizer.Add(hSizer, 0, wx.ALL, 5) + sizer.Add(btnsizer) panel.SetSizer(sizer) - self.SetClientSize(sizer.CalcMin()) - def get_selection(self): - return self.cb.GetValue() + def get_action(self): + if self.tweets.GetValue() == True: return "tweets" + elif self.favourites.GetValue() == True: return "favourites" + + def setup_default(self, default): + if default == "tweets": + self.tweets.SetValue(True) + elif default == "favourites": + self.favourites.SetValue(True) def get_response(self): - return self.ShowModal() \ No newline at end of file + return self.ShowModal() + + def get_user(self): + return self.cb.GetValue() \ No newline at end of file