From ebf6ec6e222df636afe707fb014e18259cc7d0f5 Mon Sep 17 00:00:00 2001 From: Anna Date: Mon, 17 May 2010 17:31:42 -0400 Subject: [PATCH] Added avatar support --- apithreads.py | 30 +++++++++++++++++++++++++++++- twitterwidgets.py | 13 +++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/apithreads.py b/apithreads.py index 0f612cd..b173ccf 100644 --- a/apithreads.py +++ b/apithreads.py @@ -4,7 +4,8 @@ import re import gtk, gobject from threading import Thread,RLock from twitter import Api -from urllib2 import HTTPError,URLError +from urllib2 import HTTPError,URLError,urlopen +from avcache import AvCache class CustomApi(Api): @@ -89,6 +90,11 @@ class GetTweets(ApiThread): except (HTTPError, URLError): statuses = None + # For each user id present, populate the AvCache with an image + if statuses: + for status in statuses: + add_to_cache(status) + gtk.gdk.threads_enter() try: self.pane.update_window(statuses) @@ -115,6 +121,10 @@ class GetSingleTweet(ApiThread): except (HTTPError, URLError): statuses = None + # In case we've never seen this user, grab their profile image + for status in statuses: + add_to_cache(status) + gtk.gdk.threads_enter() try: self.pane.update_window(statuses) @@ -262,3 +272,21 @@ def dms_to_statuses(direct_messages): status.text = dm.text statuses.append(status) return statuses + + +def add_to_cache(status): + with AvCache().lock: + hit = AvCache().map.has_key(status.user.screen_name) + if not hit: + try: + url = urlopen(status.user.profile.image_url) + data = url.read() + loader = gtk.gdk.PixbufLoader() + loader.write(str(data)) + image = loader.get_pixbuf() + loader.close() + + with AvCache().lock: + AvCache().map[status.user.screen_name] = image + except URLError: + pass # Nothing needs be done, just catch & ignore diff --git a/twitterwidgets.py b/twitterwidgets.py index 8627d9f..9878087 100644 --- a/twitterwidgets.py +++ b/twitterwidgets.py @@ -2,6 +2,7 @@ import re import datetime, dateutil.tz import gtk, gobject from threading import RLock +from avcache import AvCache class TweetPane(gtk.ScrolledWindow): @@ -249,8 +250,9 @@ class TweetBox(gtk.HBox): def init_widgets(self): # Build the image self.avatar = gtk.Image() - + self.avatar.set_alignment(0.0, 0.0) self.pack_start(self.avatar, expand=False, fill=False) + self.avatar.hide() # Everything else goes in a VBox beside the image text_box = gtk.VBox() @@ -311,6 +313,14 @@ class TweetBox(gtk.HBox): def set_status(self, status, read=True): + # Set avatar + try: + with AvCache().lock: + self.avatar.set_from_pixbuf(AvCache().map[status.user.screen_name]) + self.avatar.show() + except KeyError: + self.avatar.hide() + self.set_read(read) timezone = dateutil.tz.gettz() @@ -343,7 +353,6 @@ class TweetBox(gtk.HBox): # If this is in reply to something, set appropriate label if self.in_reply_to_screen_name and self.in_reply_to_id: self.reply_to_button.set_label('in reply to ' + self.in_reply_to_screen_name) - def clear_status(self):