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
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

View File

@ -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):