1
# Copyright (C) 2011 by Guillaume Hain (zedtux) <zedtux@zedroot.org>
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
class AvatarProvider(object):
25
"""Base class for Avatar providers.
27
All AvatarProviderXxxx classes should inherite from this one
28
and override at least get_base_url.
30
def __init__(self, size=80):
34
def get_base_url(self):
35
"""Return the base URL of this provider.
37
raise NotImplementedError(self.get_base_url)
40
class AvatarDownloaderWorker(threading.Thread):
41
"""Threaded worker to retrieve avatar from a provider.
43
This creates a persistant connection to the provider in order
44
to get avatars quickly through the same socket (urllib2).
47
def __init__(self, provider_method):
50
:param provider_method: Provider method that returns fields
51
to send with the request.
53
threading.Thread.__init__(self)
54
self.__stop = threading.Event()
55
self.__queue = Queue.Queue()
57
self.__provider_method = provider_method
58
self.__end_thread = False
61
""" Stop this worker """
62
self.__end_thread = True
65
def set_callback_method(self, method):
66
""" Fire the given callback method when treatment is finished """
67
self.__callback_method = method
69
def queue(self, id_field):
70
"""Put in Queue the id_field to treat in the thread.
72
This id_field is for example with Gravatar the email address.
74
self.__queue.put(id_field)
77
"""Worker core code. """
78
while not self.__end_thread:
79
id_field = self.__queue.get()
80
# Call provider method to get fields to pass in the request
81
url = self.__provider_method(id_field)
83
response = urllib2.urlopen(url)
84
# Fire the callback method
85
if not self.__callback_method is None:
86
self.__callback_method(response, id_field)
87
self.__queue.task_done()
90
class AvatarProviderGravatar(AvatarProvider):
91
"""Gravatar provider."""
93
def get_base_url(self):
94
return "http://www.gravatar.com/avatar.php?"
96
def gravatar_id_for_email(self, email):
97
"""Return a gravatar URL for an email address.."""
98
return self.get_base_url() + \
100
'gravatar_id':hashlib.md5(email.lower()).hexdigest(),
101
'size':str(self.size)