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