From 0a9669e84a7c069a397929aebe98778cc83a8d17 Mon Sep 17 00:00:00 2001 From: Anna Date: Mon, 12 Apr 2010 22:10:51 -0400 Subject: [PATCH] After some heinous, dirty hacking, we can search for hashtags and @replies on arbitrary users --- mytwitter.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/mytwitter.py b/mytwitter.py index 523060f..fbe0a57 100755 --- a/mytwitter.py +++ b/mytwitter.py @@ -80,6 +80,8 @@ class MyTwitter(): def update_windows(self): for i in range(0, self.tweet_notebook.get_n_pages()): + using_results = False + pane = self.tweet_notebook.get_nth_page(i) list_name = pane.get_list_name() @@ -89,12 +91,13 @@ class MyTwitter(): statuses = self.api.GetMentions(count=self.num_entries) elif re.match(r'user: ', list_name): statuses = self.api.GetUserTimeline(re.sub(r'^user: ', r'', list_name), count=self.num_entries) - elif re.match(r'#', list_name): + elif re.match(r'[#|@]', list_name): statuses = self.api.Search(list_name, rpp=self.num_entries) + using_results = True else: statuses = self.api.GetListStatuses(list_name, per_page=self.num_entries) - pane.update_window(statuses) + pane.update_window(statuses, using_results=using_results) # Update the label with the number of unread tweets pane_text = list_name @@ -198,7 +201,7 @@ class MyTwitter(): def on_search(self, event): search_string = self.search_entry.get_text() self.search_entry.set_text('') - if re.match(r'#', search_string): + if re.match(r'[#|@]', search_string): self.add_to_notebook(search_string) else: # Assume this is a user name we want to lookup @@ -261,7 +264,12 @@ class TweetPane(gtk.ScrolledWindow): self.show_all() - def update_window(self, statuses): + def update_window(self, raw_statuses, using_results=False): + if using_results: + statuses = self.statuses_from_results(raw_statuses) + else: + statuses = raw_statuses + # If this is our first load of this list, don't treat anything as new! if self.last_tweet_read is None: self.last_tweet_read = statuses[0].id @@ -298,6 +306,31 @@ class TweetPane(gtk.ScrolledWindow): def get_num_new_tweets(self): return self.num_new_tweets + + # 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 = "" + # 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 @@ -476,6 +509,19 @@ class CloseTabLabel(gtk.EventBox): ### end class CloseTabLabel +# A couple of lame faux-classes because the Twitter API needs a kick +# in the face +class Status(): + def __init__(self): + self.user = User() + self.id = None + self.created_at = None + +class User(): + def __init__(self): + self.screen_name = None + self.name = None + # main