1 from org.maemo.hermes.engine.twitter.user import User
8 """Twitter backend for Hermes. Inspired by
9 http://code.google.com/p/python-twitter/source/browse/twitter.py
11 Copyright (c) Andrew Flegg <andrew@bleb.org> 2010.
12 Released under the Artistic Licence."""
15 # -----------------------------------------------------------------------
16 def __init__(self, username, password):
17 self._username = username
18 self._password = password
21 # -----------------------------------------------------------------------
22 def get_friends(self):
23 '''Return the full list of people being followed by 'username'.'''
25 url = 'https://twitter.com/statuses/friends.json'
29 json = self._FetchUrl(url, parameters = { 'cursor': cursor})
30 data = simplejson.loads(json)
32 raise Exception(data['error'])
34 for x in data['users']:
35 users.append(User.NewFromJsonDict(x))
37 cursor = data['next_cursor']
38 if cursor <= data['previous_cursor']:
44 # -----------------------------------------------------------------------
48 '''Fetch a URL, optionally caching for a specified time.
54 A dict whose key/value pairs should encoded and added
55 to the query string. [optional]
58 A string containing the body of the response.
61 # Build the extra parameters dict
64 extra_params.update(parameters)
66 # Add key/value parameters to the query string of the url
67 url = self._BuildUrl(url, extra_params=extra_params)
69 # Get a url opener that can handle basic auth
70 basic_auth = base64.encodestring('%s:%s' % (self._username, self._password))[:-1]
72 handler = urllib2.HTTPBasicAuthHandler()
73 (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(url)
74 handler.add_password('Twitter API', netloc, self._username, self._password)
75 opener = urllib2.build_opener(handler)
76 opener.addheaders = {'Authorization': 'Basic %s' % basic_auth}.items()
78 url_data = opener.open(url, None).read()
83 # -----------------------------------------------------------------------
84 def _BuildUrl(self, url, path_elements=None, extra_params=None):
85 # Break url into consituent parts
86 (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(url)
88 # Add any additional path elements to the path
90 # Filter out the path elements that have a value of None
91 p = [i for i in path_elements if i]
92 if not path.endswith('/'):
96 # Add any additional query parameters to the query string
97 if extra_params and len(extra_params) > 0:
98 extra_query = self._EncodeParameters(extra_params)
99 # Add it to the existing query
101 query += '&' + extra_query
105 # Return the rebuilt URL
106 return urlparse.urlunparse((scheme, netloc, path, params, query, fragment))
110 # -----------------------------------------------------------------------
111 def _EncodeParameters(self, parameters):
112 '''Return a string in key=value&key=value form
114 Values of None are not included in the output string.
118 A dict of (key, value) tuples, where value is encoded as
119 specified by self._encoding
121 A URL-encoded string in "key=value&key=value" form
123 if parameters is None:
126 return urllib.urlencode(dict([(k, unicode(v).encode('utf-8')) for k, v in parameters.items() if v is not None]))