23
24
class AvatarProvider(object):
25
Master class for Avatar providers.
25
"""Base class for Avatar providers.
27
27
All AvatarProviderXxxx classes should inherite from this one
28
28
and override at least get_base_url.
30
30
def __init__(self, size=80):
31
31
""" Constructor """
34
34
def get_base_url(self):
36
Override this methode in your provider class in order to return
37
base url of your provider.
39
raise NotImplementedError("You must implement the get_base_url method.")
35
"""Return the base URL of this provider.
37
raise NotImplementedError(self.get_base_url)
42
40
class AvatarDownloaderWorker(threading.Thread):
44
Threaded worker to retrieve avatar from a provider.
46
It create a persitante connection to the provider in order
47
to get avatars quickly through the same socket (urllib3).
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).
50
47
def __init__(self, provider_method):
54
provider_method: Provider method that return fields
55
to send with the request.
50
:param provider_method: Provider method that returns fields
51
to send with the request.
57
53
threading.Thread.__init__(self)
58
54
self.__stop = threading.Event()
59
55
self.__queue = Queue.Queue()
61
57
self.__provider_method = provider_method
62
58
self.__end_thread = False
65
61
""" Stop this worker """
66
62
self.__end_thread = True
69
65
def set_callback_method(self, method):
70
66
""" Fire the given callback method when treatment is finished """
71
67
self.__callback_method = method
73
69
def queue(self, id_field):
75
Put in Queue the id_field to treat in the thread.
70
"""Put in Queue the id_field to treat in the thread.
76
72
This id_field is for example with Gravatar the email address.
78
74
self.__queue.put(id_field)
81
""" Worker core code. """
77
"""Worker core code. """
82
78
while not self.__end_thread:
83
79
id_field = self.__queue.get()
84
80
# Call provider method to get fields to pass in the request
94
90
class AvatarProviderGravatar(AvatarProvider):
95
""" Gravatar provider """
99
super(AvatarProviderGravatar, self).__init__()
91
"""Gravatar provider."""
101
93
def get_base_url(self):
102
94
return "http://www.gravatar.com/avatar.php?"
104
96
def gravatar_id_for_email(self, email):
105
""" Return a converted email address to a gravatar_id """
97
"""Return a gravatar URL for an email address.."""
106
98
return self.get_base_url() + \
107
99
urllib.urlencode({
108
100
'gravatar_id':hashlib.md5(email.lower()).hexdigest(),