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

Terms of Use    Privacy Policy    Contribution Guidelines    Feedback

Powered By GForge Collaborative Development Environment