summaryrefslogtreecommitdiff
path: root/sunshine
diff options
context:
space:
mode:
authorKrzysztof Klinikowski <kkszysiu@gmail.com>2010-05-26 21:08:05 +0200
committerKrzysztof Klinikowski <kkszysiu@gmail.com>2010-05-26 21:08:05 +0200
commit2bd3787185b4e7095b93cc058f02d6b5b42967e4 (patch)
treec3c279ea6bd8ea104df938a258ec6ccb8c8f94dc /sunshine
parentd1ce179a85c7209b72ad715b9068fe10855d0794 (diff)
Huge avatars changes. Should work a way better now (+caching)
Diffstat (limited to 'sunshine')
-rw-r--r--sunshine/avatars.py65
1 files changed, 36 insertions, 29 deletions
diff --git a/sunshine/avatars.py b/sunshine/avatars.py
index 83920e8..8c831c6 100644
--- a/sunshine/avatars.py
+++ b/sunshine/avatars.py
@@ -46,8 +46,7 @@ MAXIMUM_AVATAR_BYTES = dbus.UInt32(500 * 1024)
class SunshineAvatars(telepathy.server.ConnectionInterfaceAvatars):
def __init__(self):
- print 'SunshineAvatars called.'
- self._avatar_known = False
+ self.avatars_urls = {}
telepathy.server.ConnectionInterfaceAvatars.__init__(self)
dbus_interface = telepathy.CONNECTION_INTERFACE_AVATARS
@@ -75,9 +74,8 @@ class SunshineAvatars(telepathy.server.ConnectionInterfaceAvatars):
handle = self.handle(telepathy.HANDLE_TYPE_CONTACT, handle_id)
if handle == self.GetSelfHandle():
#tutaj kiedys trzeba napisac kod odp za naszego avatara
- contact = None
- result[handle] = handle.name
-
+ result[handle] = ""
+ """
else:
contact = handle.contact
@@ -90,17 +88,21 @@ class SunshineAvatars(telepathy.server.ConnectionInterfaceAvatars):
result[handle] = av_token
elif self._avatar_known:
result[handle] = ""
+ """
+ url = 'http://api.gadu-gadu.pl/avatars/%s/0.xml' % (str(handle.name))
+ d = getPage(url, timeout=10)
+ d.addCallback(self.on_fetch_avatars_file_ok, url, handle)
+ d.addErrback(self.on_fetch_avatars_file_failed, url, handle)
return result
def RequestAvatars(self, contacts):
for handle_id in contacts:
handle = self.handle(telepathy.HANDLE_TYPE_CONTACT, handle_id)
- url = 'http://api.gadu-gadu.pl/avatars/%s/0.xml' % (str(handle.name))
- d = getPage(url, timeout=10)
- d.addCallback(self.on_fetch_avatars_file_ok, url, handle_id)
- d.addErrback(self.on_fetch_avatars_file_failed, url, handle_id)
-
+ d = getPage(str(self.avatars_urls[handle.name]['avatar']), timeout=20)
+ d.addCallback(self.on_fetch_avatars_ok, handle)
+ d.addErrback(self.on_fetch_avatars_failed, handle)
+
def SetAvatar(self, avatar, mime_type):
if check_requirements() == True:
if not isinstance(avatar, str):
@@ -112,8 +114,6 @@ class SunshineAvatars(telepathy.server.ConnectionInterfaceAvatars):
def ClearAvatar(self):
pass
-# self.msn_client.profile.msn_object = None
-# self._avatar_known = True
def getAvatar(self, sender, url):
logger.info("getAvatar: %s %s" % (sender, url))
@@ -124,36 +124,43 @@ class SunshineAvatars(telepathy.server.ConnectionInterfaceAvatars):
d.addCallback(self.on_fetch_avatars_ok, str(url), handle_id)
d.addErrback(self.on_fetch_avatars_failed, str(url), handle_id)
- def on_fetch_avatars_file_ok(self, result, url, handle_id):
+ def on_fetch_avatars_file_ok(self, result, url, handle):
try:
if result:
- logger.info("Avatar file retrieved from %s" % (url))
+ #logger.info("Avatar file retrieved from %s" % (url))
e = minidom.parseString(result)
if e.getElementsByTagName('avatar')[0].attributes["blank"].value != '1':
- data = e.getElementsByTagName('bigAvatar')[0].firstChild.data
-
- d = getPage(str(data), timeout=20)
- d.addCallback(self.on_fetch_avatars_ok, data, handle_id)
- d.addErrback(self.on_fetch_avatars_failed, data, handle_id)
+ timestamp = e.getElementsByTagName('timestamp')[0].firstChild.data
+ avatar = e.getElementsByTagName('bigAvatar')[0].firstChild.data
+
+ h = hashlib.new('md5')
+ h.update(handle.name)
+ h.update(timestamp)
+ token = h.hexdigest()
+
+ self.avatars_urls[handle.name] = {}
+ self.avatars_urls[handle.name]['token'] = token
+ self.avatars_urls[handle.name]['avatar'] = avatar
+
+ self.AvatarUpdated(handle, token)
except:
logger.info("Avatar file can't be retrieved from %s" % (url))
def on_fetch_avatars_file_failed(self, error, url, handle_id):
logger.info("Avatar file can't be retrieved from %s, error: %s" % (url, error.getErrorMessage()))
- def on_fetch_avatars_ok(self, result, url, handle_id):
- try:
- handle = self.handle(telepathy.constants.HANDLE_TYPE_CONTACT, handle_id)
- logger.info("Avatar retrieved for %s from %s" % (handle.name, url))
+ def on_fetch_avatars_ok(self, result, handle):
+ #try:
+ logger.info("Avatar retrieved for %s" % (handle.name))
type = imghdr.what('', result)
if type is None: type = 'jpeg'
avatar = dbus.ByteArray(result)
- h = hashlib.new('md5')
- h.update(url)
- token = h.hexdigest()
+
+ token = self.avatars_urls[handle.name]['token']
+
self.AvatarRetrieved(handle, token, avatar, 'image/' + type)
- except:
- logger.debug("Avatar retrieved but something went wrong.")
+ #except:
+ # logger.debug("Avatar retrieved but something went wrong.")
- def on_fetch_avatars_failed(self, error, url, handle_id):
+ def on_fetch_avatars_failed(self, error, handle):
logger.debug("Avatar not retrieved, error: %s" % (error.getErrorMessage()))