24
23
class AvatarProvider(object):
25
"""Base class for Avatar providers.
25
Master 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):
35
"""Return the base URL of this provider.
37
raise NotImplementedError(self.get_base_url)
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.")
40
42
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).
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).
47
50
def __init__(self, provider_method):
50
:param provider_method: Provider method that returns fields
51
to send with the request.
54
provider_method: Provider method that return fields
55
to send with the request.
53
57
threading.Thread.__init__(self)
54
58
self.__stop = threading.Event()
55
59
self.__queue = Queue.Queue()
57
61
self.__provider_method = provider_method
58
62
self.__end_thread = False
61
65
""" Stop this worker """
62
66
self.__end_thread = True
65
69
def set_callback_method(self, method):
66
70
""" Fire the given callback method when treatment is finished """
67
71
self.__callback_method = method
69
73
def queue(self, id_field):
70
"""Put in Queue the id_field to treat in the thread.
75
Put in Queue the id_field to treat in the thread.
72
76
This id_field is for example with Gravatar the email address.
74
78
self.__queue.put(id_field)
77
"""Worker core code. """
81
""" Worker core code. """
78
82
while not self.__end_thread:
79
83
id_field = self.__queue.get()
80
84
# Call provider method to get fields to pass in the request
90
94
class AvatarProviderGravatar(AvatarProvider):
91
"""Gravatar provider."""
95
""" Gravatar provider """
99
super(AvatarProviderGravatar, self).__init__()
93
101
def get_base_url(self):
94
102
return "http://www.gravatar.com/avatar.php?"
96
104
def gravatar_id_for_email(self, email):
97
"""Return a gravatar URL for an email address.."""
105
""" Return a converted email address to a gravatar_id """
98
106
return self.get_base_url() + \
99
107
urllib.urlencode({
100
108
'gravatar_id':hashlib.md5(email.lower()).hexdigest(),