diff --git a/apithreads.py b/apithreads.py index b173ccf..c674984 100644 --- a/apithreads.py +++ b/apithreads.py @@ -4,8 +4,8 @@ import re import gtk, gobject from threading import Thread,RLock from twitter import Api -from urllib2 import HTTPError,URLError,urlopen -from avcache import AvCache +from urllib2 import HTTPError,URLError +import avcache class CustomApi(Api): @@ -93,7 +93,7 @@ class GetTweets(ApiThread): # For each user id present, populate the AvCache with an image if statuses: for status in statuses: - add_to_cache(status) + avcache.add_to_cache(status.user) gtk.gdk.threads_enter() try: @@ -123,7 +123,7 @@ class GetSingleTweet(ApiThread): # In case we've never seen this user, grab their profile image for status in statuses: - add_to_cache(status) + avcache.add_to_cache(status.user) gtk.gdk.threads_enter() try: @@ -219,6 +219,16 @@ gobject.signal_new("lists-ready", SigProxy, # We use these classes to emulate a Status object when we need # one to be built out of something else. +class User(): + def __init__(self): + self.screen_name = None + self.name = None + self.profile = Profile() + +class Profile(): + def __init__(self): + self.image_url = None + class Status(): def __init__(self): self.user = User() @@ -228,12 +238,6 @@ class Status(): self.in_reply_to_status_id = None -class User(): - def __init__(self): - self.screen_name = None - self.name = None - - # 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? @@ -246,6 +250,7 @@ def results_to_statuses(results): status.user = User() status.user.screen_name = result.from_user status.user.name = "" + status.user.profile.image_url = result.profile_image_url status.in_reply_to_screen_name = result.to_user # The Twitter Search API has different timestamps than the # REST API... balls @@ -268,25 +273,8 @@ def dms_to_statuses(direct_messages): status.user = User() status.user.screen_name = dm.sender_screen_name status.user.name = dm.sender.name + status.user.profile.image_url = dm.sender.profile_image_url status.created_at = dm.created_at 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/avcache.py b/avcache.py index 94936b6..9ca8031 100644 --- a/avcache.py +++ b/avcache.py @@ -1,4 +1,6 @@ from threading import RLock +from urllib2 import URLError,urlopen +import gtk class AvCache: @@ -38,3 +40,26 @@ class AvCache: return setattr(self.__instance, attr, value) # end class AvCache + + +def add_to_cache(user): + """ + This helping function takes a python-twitter User object, grabs the image + data from the image_url and adds it to the AvCache with the screen_name as + the key. + """ + with AvCache().lock: + hit = AvCache().map.has_key(user.screen_name) + if not hit: + try: + url = urlopen(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[user.screen_name] = image + except URLError: + pass # Nothing needs be done, just catch & ignore