Added avatar support

This commit is contained in:
Anna 2010-05-17 17:31:42 -04:00
parent 522a522129
commit ebf6ec6e22
2 changed files with 40 additions and 3 deletions

View File

@ -4,7 +4,8 @@ import re
import gtk, gobject import gtk, gobject
from threading import Thread,RLock from threading import Thread,RLock
from twitter import Api from twitter import Api
from urllib2 import HTTPError,URLError from urllib2 import HTTPError,URLError,urlopen
from avcache import AvCache
class CustomApi(Api): class CustomApi(Api):
@ -89,6 +90,11 @@ class GetTweets(ApiThread):
except (HTTPError, URLError): except (HTTPError, URLError):
statuses = None 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() gtk.gdk.threads_enter()
try: try:
self.pane.update_window(statuses) self.pane.update_window(statuses)
@ -115,6 +121,10 @@ class GetSingleTweet(ApiThread):
except (HTTPError, URLError): except (HTTPError, URLError):
statuses = None 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() gtk.gdk.threads_enter()
try: try:
self.pane.update_window(statuses) self.pane.update_window(statuses)
@ -262,3 +272,21 @@ def dms_to_statuses(direct_messages):
status.text = dm.text status.text = dm.text
statuses.append(status) statuses.append(status)
return statuses 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

View File

@ -2,6 +2,7 @@ import re
import datetime, dateutil.tz import datetime, dateutil.tz
import gtk, gobject import gtk, gobject
from threading import RLock from threading import RLock
from avcache import AvCache
class TweetPane(gtk.ScrolledWindow): class TweetPane(gtk.ScrolledWindow):
@ -249,8 +250,9 @@ class TweetBox(gtk.HBox):
def init_widgets(self): def init_widgets(self):
# Build the image # Build the image
self.avatar = gtk.Image() self.avatar = gtk.Image()
self.avatar.set_alignment(0.0, 0.0)
self.pack_start(self.avatar, expand=False, fill=False) self.pack_start(self.avatar, expand=False, fill=False)
self.avatar.hide()
# Everything else goes in a VBox beside the image # Everything else goes in a VBox beside the image
text_box = gtk.VBox() text_box = gtk.VBox()
@ -311,6 +313,14 @@ class TweetBox(gtk.HBox):
def set_status(self, status, read=True): 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) self.set_read(read)
timezone = dateutil.tz.gettz() timezone = dateutil.tz.gettz()
@ -345,7 +355,6 @@ class TweetBox(gtk.HBox):
self.reply_to_button.set_label('in reply to ' + self.in_reply_to_screen_name) self.reply_to_button.set_label('in reply to ' + self.in_reply_to_screen_name)
def clear_status(self): def clear_status(self):
self.header.set_label('') self.header.set_label('')
self.text.set_markup('') self.text.set_markup('')