Added support for Direct Messages view
This commit is contained in:
parent
2ae8eb11e6
commit
d47b12124f
68
mytwitter.py
68
mytwitter.py
|
@ -61,7 +61,7 @@ class MyTwitter():
|
||||||
# Add the Home tab to the notebook
|
# Add the Home tab to the notebook
|
||||||
self.add_to_notebook('Home')
|
self.add_to_notebook('Home')
|
||||||
|
|
||||||
# Put Home, @user, and lists in the View menu...
|
# Put Home, @user, Direct Messages, and lists in the View menu...
|
||||||
lists = self.api.GetUserLists()
|
lists = self.api.GetUserLists()
|
||||||
list_names = []
|
list_names = []
|
||||||
for l in lists['lists']:
|
for l in lists['lists']:
|
||||||
|
@ -69,6 +69,7 @@ class MyTwitter():
|
||||||
list_names.sort()
|
list_names.sort()
|
||||||
list_names.insert(0, 'Home')
|
list_names.insert(0, 'Home')
|
||||||
list_names.insert(1, '@' + self.username)
|
list_names.insert(1, '@' + self.username)
|
||||||
|
list_names.insert(2, 'Direct Messages')
|
||||||
for l in list_names:
|
for l in list_names:
|
||||||
menu_item = gtk.MenuItem(l)
|
menu_item = gtk.MenuItem(l)
|
||||||
self.view_menu.append(menu_item)
|
self.view_menu.append(menu_item)
|
||||||
|
@ -81,8 +82,6 @@ class MyTwitter():
|
||||||
|
|
||||||
def update_windows(self):
|
def update_windows(self):
|
||||||
for i in range(0, self.tweet_notebook.get_n_pages()):
|
for i in range(0, self.tweet_notebook.get_n_pages()):
|
||||||
using_results = False
|
|
||||||
|
|
||||||
pane = self.tweet_notebook.get_nth_page(i)
|
pane = self.tweet_notebook.get_nth_page(i)
|
||||||
list_name = pane.get_list_name()
|
list_name = pane.get_list_name()
|
||||||
|
|
||||||
|
@ -92,15 +91,16 @@ class MyTwitter():
|
||||||
statuses = self.api.GetHomeTimeline(count=self.num_entries)
|
statuses = self.api.GetHomeTimeline(count=self.num_entries)
|
||||||
elif list_name == '@' + self.username:
|
elif list_name == '@' + self.username:
|
||||||
statuses = self.api.GetMentions(count=self.num_entries)
|
statuses = self.api.GetMentions(count=self.num_entries)
|
||||||
|
elif list_name == 'Direct Messages':
|
||||||
|
statuses = self.dms_to_statuses(self.api.GetDirectMessages())
|
||||||
elif re.match(r'user: ', list_name):
|
elif re.match(r'user: ', list_name):
|
||||||
statuses = self.api.GetUserTimeline(re.sub(r'^user: ', r'', list_name), count=self.num_entries)
|
statuses = self.api.GetUserTimeline(re.sub(r'^user: ', r'', list_name), count=self.num_entries)
|
||||||
elif re.match(r'list: ', list_name):
|
elif re.match(r'list: ', list_name):
|
||||||
statuses = self.api.GetListStatuses(re.sub(r'^list: ', r'', list_name), per_page=self.num_entries)
|
statuses = self.api.GetListStatuses(re.sub(r'^list: ', r'', list_name), per_page=self.num_entries)
|
||||||
else:
|
else:
|
||||||
statuses = self.api.Search(list_name, rpp=self.num_entries)
|
statuses = self.results_to_statuses(self.api.Search(list_name, rpp=self.num_entries))
|
||||||
using_results = True
|
|
||||||
|
|
||||||
pane.update_window(statuses, using_results=using_results)
|
pane.update_window(statuses)
|
||||||
|
|
||||||
# We have to return true, so the timeout_add event will keep happening
|
# We have to return true, so the timeout_add event will keep happening
|
||||||
return True
|
return True
|
||||||
|
@ -156,7 +156,7 @@ class MyTwitter():
|
||||||
|
|
||||||
|
|
||||||
def on_view_selected(self, event, name):
|
def on_view_selected(self, event, name):
|
||||||
if name == 'Home' or name == '@' + self.username:
|
if name == 'Home' or name == '@' + self.username or name == 'Direct Messages':
|
||||||
full_name = name
|
full_name = name
|
||||||
else:
|
else:
|
||||||
full_name = 'list: ' + name
|
full_name = 'list: ' + name
|
||||||
|
@ -300,9 +300,63 @@ class MyTwitter():
|
||||||
self.remove_view(current_pane.get_list_name())
|
self.remove_view(current_pane.get_list_name())
|
||||||
|
|
||||||
|
|
||||||
|
# To keep things simple elsewhere and improve code reuse
|
||||||
|
# we'll build a list of home-cooked Status objects out of results.
|
||||||
|
# Why is this even necessary?
|
||||||
|
# Why can't we have more consistency out of the Twitter API?
|
||||||
|
def results_to_statuses(self, results):
|
||||||
|
statuses = []
|
||||||
|
for result in results.results:
|
||||||
|
status = Status()
|
||||||
|
status.id = result.id
|
||||||
|
status.user = User()
|
||||||
|
status.user.screen_name = result.from_user
|
||||||
|
status.user.name = ""
|
||||||
|
status.in_reply_to_screen_name = result.to_user
|
||||||
|
# The Twitter Search API has different timestamps than the
|
||||||
|
# REST API... balls
|
||||||
|
# fixme:
|
||||||
|
# Gotta be a cleaner way to do this, but I can't think of it
|
||||||
|
# right now
|
||||||
|
created_at = re.sub(',', '', result.created_at)
|
||||||
|
created_split = re.split(' ', created_at)
|
||||||
|
status.created_at = created_split[0] + ' ' + created_split[2] + ' ' + created_split[1] + ' ' + created_split[4] + ' ' + created_split[5] + ' ' + created_split[3]
|
||||||
|
status.text = result.text
|
||||||
|
statuses.append(status)
|
||||||
|
return statuses
|
||||||
|
|
||||||
|
|
||||||
|
def dms_to_statuses(self, direct_messages):
|
||||||
|
statuses = []
|
||||||
|
for dm in direct_messages:
|
||||||
|
status = Status()
|
||||||
|
status.id = dm.id
|
||||||
|
status.user = User()
|
||||||
|
status.user.screen_name = dm.sender.screen_name
|
||||||
|
status.user.name = dm.sender.name
|
||||||
|
status.created_at = dm.created_at
|
||||||
|
status.text = dm.text
|
||||||
|
statuses.append(status)
|
||||||
|
return statuses
|
||||||
|
|
||||||
### end class MyTwitter
|
### end class MyTwitter
|
||||||
|
|
||||||
|
|
||||||
|
# We use these classes to emulate a Status object when we need
|
||||||
|
# one to be built out of something else.
|
||||||
|
class Status():
|
||||||
|
def __init__(self):
|
||||||
|
self.user = User()
|
||||||
|
self.id = None
|
||||||
|
self.created_at = None
|
||||||
|
self.in_reply_to_screen_name = None
|
||||||
|
self.in_reply_to_status_id = None
|
||||||
|
|
||||||
|
class User():
|
||||||
|
def __init__(self):
|
||||||
|
self.screen_name = None
|
||||||
|
self.name = None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# main
|
# main
|
||||||
|
|
|
@ -68,12 +68,7 @@ class TweetPane(gtk.ScrolledWindow):
|
||||||
self.show_all()
|
self.show_all()
|
||||||
|
|
||||||
|
|
||||||
def update_window(self, raw_statuses, using_results=False):
|
def update_window(self, statuses):
|
||||||
if using_results:
|
|
||||||
statuses = self.statuses_from_results(raw_statuses)
|
|
||||||
else:
|
|
||||||
statuses = raw_statuses
|
|
||||||
|
|
||||||
if self.updated_once is False:
|
if self.updated_once is False:
|
||||||
self.updated_once = True
|
self.updated_once = True
|
||||||
|
|
||||||
|
@ -160,30 +155,6 @@ class TweetPane(gtk.ScrolledWindow):
|
||||||
def get_is_user(self):
|
def get_is_user(self):
|
||||||
return self.is_user
|
return self.is_user
|
||||||
|
|
||||||
# To keep things simple elsewhere and improve code reuse
|
|
||||||
# we'll build a list of home-cooked Status objects out of results.
|
|
||||||
# Why is this even necessary?
|
|
||||||
# Why can't we have more consistency out of the Twitter API?
|
|
||||||
def statuses_from_results(self, results):
|
|
||||||
statuses = []
|
|
||||||
for result in results.results:
|
|
||||||
status = Status()
|
|
||||||
status.id = result.id
|
|
||||||
status.user = User()
|
|
||||||
status.user.screen_name = result.from_user
|
|
||||||
status.user.name = ""
|
|
||||||
status.in_reply_to_screen_name = result.to_user
|
|
||||||
# The Twitter Search API has different timestamps than the
|
|
||||||
# REST API... balls
|
|
||||||
# fixme:
|
|
||||||
# Gotta be a cleaner way to do this, but I can't think of it
|
|
||||||
# right now
|
|
||||||
created_at = re.sub(',', '', result.created_at)
|
|
||||||
created_split = re.split(' ', created_at)
|
|
||||||
status.created_at = created_split[0] + ' ' + created_split[2] + ' ' + created_split[1] + ' ' + created_split[4] + ' ' + created_split[5] + ' ' + created_split[3]
|
|
||||||
status.text = result.text
|
|
||||||
statuses.append(status)
|
|
||||||
return statuses
|
|
||||||
|
|
||||||
### end class TweetPane
|
### end class TweetPane
|
||||||
|
|
||||||
|
@ -445,18 +416,3 @@ gobject.signal_new("close-clicked", CloseTabLabel,
|
||||||
gobject.signal_new("label-clicked", CloseTabLabel,
|
gobject.signal_new("label-clicked", CloseTabLabel,
|
||||||
gobject.SIGNAL_RUN_LAST,
|
gobject.SIGNAL_RUN_LAST,
|
||||||
gobject.TYPE_NONE, ())
|
gobject.TYPE_NONE, ())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Status():
|
|
||||||
def __init__(self):
|
|
||||||
self.user = User()
|
|
||||||
self.id = None
|
|
||||||
self.created_at = None
|
|
||||||
self.in_reply_to_screen_name = None
|
|
||||||
self.in_reply_to_status_id = None
|
|
||||||
|
|
||||||
class User():
|
|
||||||
def __init__(self):
|
|
||||||
self.screen_name = None
|
|
||||||
self.name = None
|
|
||||||
|
|
Reference in New Issue
Block a user