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
3 from org.maemo.hermes.engine.friend import Friend
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)
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         
30         self._friends_by_contact = {}
31         self._contacts_by_friend = {}
32         
33         self._api_url = 'https://secure.gravatar.com/xmlrpc?user=' + hashlib.md5(self._api_email).hexdigest()
34     
35     
36     # -----------------------------------------------------------------------
37     def pre_process_contact(self, contact):
38         """Extracts addresses from the contact."""
39         
40         for address in contact.get_emails():
41             self._address_to_contact[address] = contact
42     
43     
44     # -----------------------------------------------------------------------
45     def process_friends(self):
46         self._lookup_addresses()
48     
49     # -----------------------------------------------------------------------
50     def process_contact(self, contact):
51         """On first call (with a contact missing a photo), go get data from 
52            Gravatar's servers."""
53         
54         if not self._has_photo(contact):
55             for address in contact.get_emails():
56                 hash = self._get_hash_for_address(address)
57                 if (self._hash_has_gravatar.has_key(hash) and self._hash_has_gravatar[hash]):
58                     friend = Friend(contact.get_name())
59                     friend.set_photo_url(self._get_url_for_email_hash(hash))
60                     self._register_match(contact, friend)
61                     return friend
62         
63         return None
66     # -----------------------------------------------------------------------
67     def get_unmatched_friends(self):
68         """Will always return empty list - Gravatar only reacts on e-mail 
69            addresses from existing contacts."""
71         return []
72     
74     # -----------------------------------------------------------------------
75     def _get_friends(self):
76         return self._contacts_by_friend.keys()
79     # -----------------------------------------------------------------------    
80     def _get_contacts_with_match(self):
81         """Returns a dict with Contact objects as keys and Friend objects as 
82            values"""
83         
84         return self._friends_by_contact
87     # -----------------------------------------------------------------------
88     def _register_match(self, contact, friend):
89         friend.set_contact(contact)
90         self._friends_by_contact[contact] = friend
91         self._contacts_by_friend[friend] = contact
92         
93     # -----------------------------------------------------------------------
94     # FIXME
95     def _has_photo(self, contact):
96         return False
97     
98         
99     # -----------------------------------------------------------------------
100     def _lookup_addresses(self):
101         """Constructs hashes for address_to_contact, makes call to the Gravatar.com service and updates
102         self._hash_has_gravatar"""
103         
104         addresses = self._address_to_contact.keys()
105         if len(addresses) == 0:
106             self._set_hash_information({})
107         else:
108             args = { "apikey" : self._api_key}
109             hashes = self._construct_hashes(addresses)
110             args["hashes"] = list(hashes)
111             url = self._api_url
112             self._set_hash_information(self._get_hash_info_from_server(args, url))
113             
115     # -----------------------------------------------------------------------
116     def _get_hash_info_from_server(self, args, url):
117         """Makes the actual XML-RPC call - override this for testing"""
118         
119         service = xmlrpclib.ServerProxy(url)
120         return service.grav.exists(args)
123     # -----------------------------------------------------------------------
124     def _set_hash_information(self, hash_info):
125         self._hash_has_gravatar = hash_info
126         
128     # -----------------------------------------------------------------------
129     def _get_url_for_email_hash(self, hash, default="404", size="128"):
130         """Assembles the URL to a gravatar, based on a hash of an e-mail address"""
131     
132         return self._image_url_base + urllib.urlencode({'gravatar_id':hash, 'd':default, 'size':size})
133     
134     
135     # -----------------------------------------------------------------------
136     def _construct_hashes(self, addresses):
137         """Creates hashes for all addresses specified, returning a set of hashes"""
138     
139         result = set()
140         for address in addresses:
141             hash = self._get_hash_for_address(address)
142             self._hash_to_address[hash] = address
143             result.add(hash)
144     
145         return result
147     # -----------------------------------------------------------------------
148     def _get_hash_for_address(self, address):
149         return hashlib.md5(address.lower()).hexdigest()

Terms of Use    Privacy Policy    Contribution Guidelines    Feedback

Powered By GForge Collaborative Development Environment