moved add_to_cache to avcache.py and added the profile_image_url to results from searches and dms
This commit is contained in:
parent
324bd13120
commit
86af5ede96
|
@ -4,8 +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,urlopen
|
from urllib2 import HTTPError,URLError
|
||||||
from avcache import AvCache
|
import avcache
|
||||||
|
|
||||||
|
|
||||||
class CustomApi(Api):
|
class CustomApi(Api):
|
||||||
|
@ -93,7 +93,7 @@ class GetTweets(ApiThread):
|
||||||
# For each user id present, populate the AvCache with an image
|
# For each user id present, populate the AvCache with an image
|
||||||
if statuses:
|
if statuses:
|
||||||
for status in statuses:
|
for status in statuses:
|
||||||
add_to_cache(status)
|
avcache.add_to_cache(status.user)
|
||||||
|
|
||||||
gtk.gdk.threads_enter()
|
gtk.gdk.threads_enter()
|
||||||
try:
|
try:
|
||||||
|
@ -123,7 +123,7 @@ class GetSingleTweet(ApiThread):
|
||||||
|
|
||||||
# In case we've never seen this user, grab their profile image
|
# In case we've never seen this user, grab their profile image
|
||||||
for status in statuses:
|
for status in statuses:
|
||||||
add_to_cache(status)
|
avcache.add_to_cache(status.user)
|
||||||
|
|
||||||
gtk.gdk.threads_enter()
|
gtk.gdk.threads_enter()
|
||||||
try:
|
try:
|
||||||
|
@ -219,6 +219,16 @@ gobject.signal_new("lists-ready", SigProxy,
|
||||||
|
|
||||||
# We use these classes to emulate a Status object when we need
|
# We use these classes to emulate a Status object when we need
|
||||||
# one to be built out of something else.
|
# 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():
|
class Status():
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.user = User()
|
self.user = User()
|
||||||
|
@ -228,12 +238,6 @@ class Status():
|
||||||
self.in_reply_to_status_id = None
|
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
|
# To keep things simple elsewhere and improve code reuse
|
||||||
# we'll build a list of home-cooked Status objects out of results.
|
# we'll build a list of home-cooked Status objects out of results.
|
||||||
# Why is this even necessary?
|
# Why is this even necessary?
|
||||||
|
@ -246,6 +250,7 @@ def results_to_statuses(results):
|
||||||
status.user = User()
|
status.user = User()
|
||||||
status.user.screen_name = result.from_user
|
status.user.screen_name = result.from_user
|
||||||
status.user.name = ""
|
status.user.name = ""
|
||||||
|
status.user.profile.image_url = result.profile_image_url
|
||||||
status.in_reply_to_screen_name = result.to_user
|
status.in_reply_to_screen_name = result.to_user
|
||||||
# The Twitter Search API has different timestamps than the
|
# The Twitter Search API has different timestamps than the
|
||||||
# REST API... balls
|
# REST API... balls
|
||||||
|
@ -268,25 +273,8 @@ def dms_to_statuses(direct_messages):
|
||||||
status.user = User()
|
status.user = User()
|
||||||
status.user.screen_name = dm.sender_screen_name
|
status.user.screen_name = dm.sender_screen_name
|
||||||
status.user.name = dm.sender.name
|
status.user.name = dm.sender.name
|
||||||
|
status.user.profile.image_url = dm.sender.profile_image_url
|
||||||
status.created_at = dm.created_at
|
status.created_at = dm.created_at
|
||||||
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
|
|
||||||
|
|
25
avcache.py
25
avcache.py
|
@ -1,4 +1,6 @@
|
||||||
from threading import RLock
|
from threading import RLock
|
||||||
|
from urllib2 import URLError,urlopen
|
||||||
|
import gtk
|
||||||
|
|
||||||
|
|
||||||
class AvCache:
|
class AvCache:
|
||||||
|
@ -38,3 +40,26 @@ class AvCache:
|
||||||
return setattr(self.__instance, attr, value)
|
return setattr(self.__instance, attr, value)
|
||||||
|
|
||||||
# end class AvCache
|
# 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
|
||||||
|
|
Reference in New Issue
Block a user