6c4880b2de95ac4fa7caacc54f8f8bab902842f7
[hermes] / package / src / org / maemo / hermes / engine / gravatar / service.py
1 import urllib, hashlib, xmlrpclib
2 import org.maemo.hermes.engine.service
3 from org.maemo.hermes.engine.friend import Friend
4
5 class Service(org.maemo.hermes.engine.service.Service):
6     """Gravatar backend for Hermes.
7        
8        Copyright (c) Fredrik Wendt <fredrik@wendt.se> 2010.
9        Released under the Artistic Licence."""
10        
11     _image_url_base = "http://www.gravatar.com/avatar.php?"
12        
13     # -----------------------------------------------------------------------
14     def __init__(self, service_id, api_email, api_key):
15         """Initialise the Gravatar service.
16         
17         api_email is the email address to use when talking to the backend.
18         api_key is the "secret" key to use when talking to the backend.
19         """
20         
21         org.maemo.hermes.engine.service.Service.__init__(self, service_id)
22
23         self._api_email  = api_email
24         self._api_key    = api_key
25          
26         self._address_to_contact = {}
27         self._hash_to_address = {}
28         self._hash_has_gravatar = {}
29         self._empty_cache = True
30         
31         self._friends_by_contact = {}
32         self._contacts_by_friend = {}
33         
34         self._api_url = 'https://secure.gravatar.com/xmlrpc?user=' + hashlib.md5(self._api_email).hexdigest()
35     
36     
37     # -----------------------------------------------------------------------
38     def pre_process_contact(self, contact):
39         """Extracts addresses from the contact."""
40         for address in contact.get_emails():
41             self._address_to_contact[address] = contact
42     
43     
44     # -----------------------------------------------------------------------
45     def process_contact(self, contact):
46         """On first call (with a contact missing a photo), go get data from Gravatar's servers."""
47         
48         if not self._has_photo(contact):
49             for address in contact.get_emails():
50                 hash = self._get_hash_for_address(address)
51                 if (self._hash_has_gravatar.has_key(hash) and self._hash_has_gravatar[hash]):
52                     friend = Friend(contact.get_name())
53                     friend.set_photo_url(self._get_url_for_email_hash(hash))
54                     self._register_match(contact, friend)
55                     return friend
56         
57         return None
58
59
60     # -----------------------------------------------------------------------
61     def process_friends(self):
62         self._lookup_addresses()
63
64     
65     # -----------------------------------------------------------------------
66     def get_friends(self):
67         return self._contacts_by_friend.keys()
68
69     
70     def get_contacts_with_match(self):
71         """Returns a dict with Contact objects as keys and Friend objects as values"""
72         return self._friends_by_contact
73     
74
75     def get_unmatched_friends(self):
76         """Will always return None - Gravatar only reacts on e-mail address input."""
77         return None
78     
79     
80
81
82     # -----------------------------------------------------------------------
83     def _register_match(self, contact, friend):
84         friend.set_contact(contact)
85         self._friends_by_contact[contact] = friend
86         self._contacts_by_friend[friend] = contact
87         
88     # -----------------------------------------------------------------------
89     # FIXME
90     def _has_photo(self, contact):
91         return False
92     
93         
94     # -----------------------------------------------------------------------
95     def _lookup_addresses(self):
96         """Constructs hashes for address_to_contact, makes call to the Gravatar.com service and updates
97         self._hash_has_gravatar"""
98         
99         addresses = self._address_to_contact.keys()
100         if len(addresses) == 0:
101             self._set_hash_information({})
102         else:
103             args = { "apikey" : self._api_key}
104             hashes = self._construct_hashes(addresses)
105             args["hashes"] = list(hashes)
106             url = self._api_url
107             self._set_hash_information(self._get_hash_info_from_server(args, url))
108             
109
110     # -----------------------------------------------------------------------
111     def _get_hash_info_from_server(self, args, url):
112         """Makes the actual XML-RPC call - override this for testing"""
113         
114         service = xmlrpclib.ServerProxy(url)
115         return service.grav.exists(args)
116
117
118     # -----------------------------------------------------------------------
119     def _set_hash_information(self, hash_info):
120         self._hash_has_gravatar = hash_info
121         self._empty_cache = False
122         
123
124     # -----------------------------------------------------------------------
125     def _get_url_for_email_hash(self, hash, default="404", size="128"):
126         """Assembles the URL to a gravatar, based on a hash of an e-mail address"""
127     
128         return self._image_url_base + urllib.urlencode({'gravatar_id':hash, 'd':default, 'size':size})
129     
130     
131     # -----------------------------------------------------------------------
132     def _construct_hashes(self, addresses):
133         """Creates hashes for all addresses specified, returning a set of hashes"""
134     
135         result = set()
136         for address in addresses:
137             hash = self._get_hash_for_address(address)
138             self._hash_to_address[hash] = address
139             result.add(hash)
140     
141         return result
142
143     # -----------------------------------------------------------------------
144     def _get_hash_for_address(self, address):
145         return hashlib.md5(address.lower()).hexdigest()