diff --git a/apithreads.py b/apithreads.py index b67847f..e810bcb 100644 --- a/apithreads.py +++ b/apithreads.py @@ -263,7 +263,8 @@ class GetUserLists(ApiThread): try: with self.api.lock: lists = self.api.GetUserLists() - except (HTTPError, URLError): + except (HTTPError, URLError) as exception: + print 'debug: GetUserLists had error: ' + str(exception.code) done = False self.sig_proxy.emit('lists-ready', lists, None) diff --git a/hrafn.py b/hrafn.py index 9ffba34..784e12d 100755 --- a/hrafn.py +++ b/hrafn.py @@ -64,18 +64,15 @@ class Hrafn(): self.num_entries = int(config.get('global', 'entries')) self.refresh_time = int(config.get('global', 'refreshtime')) + if not self.db.has_key('active_user'): + self.db['active_user'] = None + # Now set up the accounts and their corresponding APIs self.accounts = {} for token in self.db['tokens']: api = apithreads.CustomApi(token) self.add_account(api) - if not self.db.has_key('active_user'): - try: - self.db['active_user'] = self.accounts.keys()[0] - except IndexError: - self.db['active_user'] = None - self.username = self.db['active_user'] try: @@ -148,7 +145,7 @@ class Hrafn(): def update_account_label(self): - if self.username: + if self.username is not None: self.account_label.set_text(self.username + ': ') diff --git a/oauthtwitter.py b/oauthtwitter.py index d36d003..940f34d 100755 --- a/oauthtwitter.py +++ b/oauthtwitter.py @@ -1,6 +1,10 @@ #!/usr/bin/env python # # Copyright under the latest Apache License 2.0 +# +# Patched for hrafn to include fixes to issues 2, 3, 9, 10 +# +# Additional patch to make it work with the 'dev' branch of python-twitter '''A class the inherits everything from python-twitter and allows oauth based access @@ -17,9 +21,12 @@ __version__ = "0.2" from twitter import Api,NewUserFromJsonDict from twitter_pb2 import User +import logging import simplejson import oauth.oauth as oauth - +import time +from urllib import urlencode +from urllib2 import BaseHandler # Taken from oauth implementation at: http://github.com/harperreed/twitteroauth-python/tree/master @@ -28,6 +35,17 @@ ACCESS_TOKEN_URL = 'https://twitter.com/oauth/access_token' AUTHORIZATION_URL = 'http://twitter.com/oauth/authorize' SIGNIN_URL = 'http://twitter.com/oauth/authenticate' +class LoggingHandler(BaseHandler): + """ debugging handler that logs HTTP errors """ + + def __init__(self): + # ensures that this handler runs first + self.handler_order=0 + + def http_error_default(self, req, fp, code, msg, hdrs): + logging.debug("%s %s: %s" % (code, msg, fp.read())) + + class OAuthApi(Api): def __init__(self, consumer_key, consumer_secret, access_token=None): @@ -41,7 +59,7 @@ class OAuthApi(Api): def _GetOpener(self): - opener = self._urllib.build_opener() + opener = self._urllib.build_opener(LoggingHandler()) return opener def _FetchUrl(self, @@ -73,8 +91,9 @@ class OAuthApi(Api): # Add key/value parameters to the query string of the url #url = self._BuildUrl(url, extra_params=extra_params) - if post_data: + if post_data is not None: http_method = "POST" + post_data=dict([(k, self._Encode(v)) for k, v in post_data.items()]) extra_params.update(post_data) else: http_method = "GET" @@ -87,37 +106,33 @@ class OAuthApi(Api): # Get a url opener that can handle Oauth basic auth opener = self._GetOpener() - #encoded_post_data = self._EncodePostData(post_data) - if post_data: + if post_data is not None: encoded_post_data = req.to_postdata() - url = req.get_normalized_http_url() + req_url = req.get_normalized_http_url() else: - url = req.to_url() - encoded_post_data = "" - - no_cache=True + encoded_post_data = None + req_url = req.to_url() + + # Open and return the URL immediately if we're not going to cache # OR we are posting data - if encoded_post_data or no_cache: - if encoded_post_data: - url_data = opener.open(url, encoded_post_data).read() - else: - url_data = opener.open(url).read() + if encoded_post_data or no_cache or not self._cache or not self._cache_timeout: + url_data = opener.open(req_url, encoded_post_data).read() opener.close() else: # Unique keys are a combination of the url and the username if self._username: - key = self._username + ':' + url + key = self._username + ':' + url + '?' + urlencode(extra_params) else: - key = url + key = url + '?' + urlencode(extra_params) # See if it has been cached before last_cached = self._cache.GetCachedTime(key) # If the cached version is outdated then fetch another and store it if not last_cached or time.time() >= last_cached + self._cache_timeout: - url_data = opener.open(url).read() + url_data = opener.open(req_url, encoded_post_data).read() opener.close() self._cache.Set(key, url_data) else: @@ -198,7 +213,7 @@ class OAuthApi(Api): token = oauth.OAuthToken.from_string(resp) return token - def GetUserInfo(self, url='https://twitter.com/account/verify_credentials.json'): + def GetUserInfo(self, url='http://api.twitter.com/1/account/verify_credentials.json'): '''Get user information from twitter Returns: