From 9b798f7ac0fecf310e9a1a34fd9ffef0038c1121 Mon Sep 17 00:00:00 2001 From: jonathan vanasco Date: Tue, 10 Apr 2012 10:21:00 -0400 Subject: [PATCH 1/3] added error code tracking into the TwythonError --- twython/twython.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/twython/twython.py b/twython/twython.py index c4b425c..2f7f787 100644 --- a/twython/twython.py +++ b/twython/twython.py @@ -63,6 +63,7 @@ class TwythonError(AttributeError): """ def __init__(self, msg, error_code=None): self.msg = msg + self.error_code = error_code if error_code is not None and error_code in twitter_http_status_codes: self.msg = '%s: %s -- %s' % \ @@ -71,7 +72,7 @@ class TwythonError(AttributeError): self.msg) if error_code == 400 or error_code == 420: - raise TwythonAPILimit(self.msg) + raise TwythonAPILimit( self.msg , error_code) def __str__(self): return repr(self.msg) @@ -83,8 +84,9 @@ class TwythonAPILimit(TwythonError): docs if you're running into issues here, Twython does not concern itself with this matter beyond telling you that you've done goofed. """ - def __init__(self, msg): + def __init__(self, msg, error_code=None): self.msg = msg + self.error_code = error_code def __str__(self): return repr(self.msg) @@ -123,8 +125,9 @@ class TwythonAuthError(TwythonError): Raised when you try to access a protected resource and it fails due to some issue with your authentication. """ - def __init__(self, msg): + def __init__(self, msg, error_code=None ): self.msg = msg + self.error_code = error_code def __str__(self): return repr(self.msg) @@ -135,8 +138,9 @@ class AuthError(TwythonError): Raised when you try to access a protected resource and it fails due to some issue with your authentication. """ - def __init__(self, msg): + def __init__(self, msg , error_code=None ): self.msg = '%s\n Notice: AuthError is deprecated and soon to be removed, catch on TwythonAuthError instead!' % msg + self.error_code = error_code def __str__(self): return repr(self.msg) @@ -405,7 +409,7 @@ class Twython(object): if request.status_code in [301, 201, 200]: return request.text else: - raise TwythonError('shortenURL() failed with a %s error code.' % request.status_code) + raise TwythonError('shortenURL() failed with a %s error code.' % request.status_code , request.status_code ) @staticmethod def constructApiURL(base_url, params): From 343dcb87ffe33938a8bb5f61bb66cb298736e22f Mon Sep 17 00:00:00 2001 From: Mohammed ALDOUB Date: Sat, 14 Apr 2012 05:34:22 +0300 Subject: [PATCH 2/3] I fixed line 479 to properly URL encode the querystring (q parameter) for the search functionality. According to http://dev.twitter.com/doc/get/search, the q parameter should be URL encoded, but Twython.unicode2utf8 doesn't urlencode the query. So I enclosed it in a urllib.quote_plus function call. examples: >>> urllib.quote_plus(Twython.unicode2utf8('h ^&$')) 'h+%5E%26%24' >>> Twython.unicode2utf8('h ^&$') 'h ^&$' >>> --- twython/twython.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/twython/twython.py b/twython/twython.py index 2f7f787..98d1f6f 100644 --- a/twython/twython.py +++ b/twython/twython.py @@ -476,7 +476,7 @@ class Twython(object): e.g x.searchGen("python", page="2") or x.searchGen(search_query = "python", page = "2") """ - searchURL = Twython.constructApiURL("http://search.twitter.com/search.json?q=%s" % Twython.unicode2utf8(search_query), kwargs) + searchURL = Twython.constructApiURL("http://search.twitter.com/search.json?q=%s" % urllib.quote_plus(Twython.unicode2utf8(search_query)), kwargs) try: response = self.client.get(searchURL, headers=self.headers) data = simplejson.loads(response.content.decode('utf-8')) From 01a7284a8f3928739c410eebd969715a9cccc791 Mon Sep 17 00:00:00 2001 From: Mohammed ALDOUB Date: Sat, 14 Apr 2012 05:53:51 +0300 Subject: [PATCH 3/3] I have added the following lines to the function 'request' starting in line 287: # convert any http Twitter url into https, for the sake of user security # only convert the protocol part, not all occurences of http://, in case users want to search that endpoint = endpoint.replace('http://','https://',1) This is to ensure all passed Twitter urls are converted into https, without messing with the rest of the url. --- twython/twython.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/twython/twython.py b/twython/twython.py index 2f7f787..978856e 100644 --- a/twython/twython.py +++ b/twython/twython.py @@ -289,6 +289,9 @@ class Twython(object): # In case they want to pass a full Twitter URL # i.e. http://search.twitter.com/ + # convert any http Twitter url into https, for the sake of user security + # only convert the protocol part, not all occurences of http://, in case users want to search that + endpoint = endpoint.replace('http://','https://',1) if endpoint.startswith('http://'): url = endpoint else: