2.3.2, latest @michaelhelmick fixes
This commit is contained in:
commit
0bcc15ecf3
3 changed files with 35 additions and 62 deletions
|
|
@ -9,7 +9,7 @@
|
|||
"""
|
||||
|
||||
__author__ = "Ryan McGrath <ryan@venodesigns.net>"
|
||||
__version__ = "2.3.1"
|
||||
__version__ = "2.3.2"
|
||||
|
||||
import urllib
|
||||
import re
|
||||
|
|
@ -27,12 +27,7 @@ except ImportError:
|
|||
# table is a file with a dictionary of every API endpoint that Twython supports.
|
||||
from twitter_endpoints import base_url, api_table, twitter_http_status_codes
|
||||
|
||||
|
||||
# There are some special setups (like, oh, a Django application) where
|
||||
# simplejson exists behind the scenes anyway. Past Python 2.6, this should
|
||||
# never really cause any problems to begin with.
|
||||
try:
|
||||
# If they have the library, chances are they're gonna want to use that.
|
||||
import simplejson
|
||||
except ImportError:
|
||||
try:
|
||||
|
|
@ -40,7 +35,6 @@ except ImportError:
|
|||
import json as simplejson
|
||||
except ImportError:
|
||||
try:
|
||||
# This case gets rarer by the day, but if we need to, we can pull it from Django provided it's there.
|
||||
from django.utils import simplejson
|
||||
except:
|
||||
# Seriously wtf is wrong with you if you get this Exception.
|
||||
|
|
@ -110,45 +104,34 @@ class Twython(object):
|
|||
self.authenticate_url = self.api_url % 'oauth/authenticate'
|
||||
|
||||
# Enforce unicode on keys and secrets
|
||||
self.app_key = None
|
||||
if app_key is not None or twitter_token is not None:
|
||||
self.app_key = u'%s' % (app_key or twitter_token)
|
||||
self.app_key = app_key and unicode(app_key) or twitter_token and unicode(twitter_token)
|
||||
self.app_secret = app_key and unicode(app_secret) or twitter_secret and unicode(twitter_secret)
|
||||
|
||||
self.app_secret = None
|
||||
if app_secret is not None or twitter_secret is not None:
|
||||
self.app_secret = u'%s' % (app_secret or twitter_secret)
|
||||
|
||||
self.oauth_token = None
|
||||
if oauth_token is not None:
|
||||
self.oauth_token = u'%s' % oauth_token
|
||||
|
||||
self.oauth_token_secret = None
|
||||
if oauth_token_secret is not None:
|
||||
self.oauth_token_secret = u'%s' % oauth_token_secret
|
||||
self.oauth_token = oauth_token and u'%s' % oauth_token
|
||||
self.oauth_token_secret = oauth_token_secret and u'%s' % oauth_token_secret
|
||||
|
||||
self.callback_url = callback_url
|
||||
|
||||
# If there's headers, set them, otherwise be an embarassing parent for their own good.
|
||||
self.headers = headers
|
||||
if self.headers is None:
|
||||
self.headers = {'User-agent': 'Twython Python Twitter Library v' + __version__}
|
||||
self.headers = headers or {'User-Agent': 'Twython v' + __version__}
|
||||
|
||||
self.client = None
|
||||
# Allow for unauthenticated requests
|
||||
self.client = requests.session(proxies=proxies)
|
||||
self.auth = None
|
||||
|
||||
if self.app_key is not None and self.app_secret is not None:
|
||||
if self.app_key is not None and self.app_secret is not None and \
|
||||
self.oauth_token is None and self.oauth_token_secret is None:
|
||||
self.auth = OAuth1(self.app_key, self.app_secret,
|
||||
signature_type='auth_header')
|
||||
|
||||
if self.oauth_token is not None and self.oauth_token_secret is not None:
|
||||
if self.app_key is not None and self.app_secret is not None and \
|
||||
self.oauth_token is not None and self.oauth_token_secret is not None:
|
||||
self.auth = OAuth1(self.app_key, self.app_secret,
|
||||
self.oauth_token, self.oauth_token_secret,
|
||||
signature_type='auth_header')
|
||||
|
||||
if self.client is None:
|
||||
# If they don't do authentication, but still want to request
|
||||
# unprotected resources, we need an opener.
|
||||
self.client = requests.session(proxies=proxies)
|
||||
if self.auth is not None:
|
||||
self.client = requests.session(headers=self.headers, auth=self.auth, proxies=proxies)
|
||||
|
||||
# register available funcs to allow listing name when debugging.
|
||||
def setFunc(key):
|
||||
|
|
@ -169,11 +152,7 @@ class Twython(object):
|
|||
base_url + fn['url']
|
||||
)
|
||||
|
||||
method = fn['method'].lower()
|
||||
if not method in ('get', 'post'):
|
||||
raise TwythonError('Method must be of GET or POST')
|
||||
|
||||
content = self._request(url, method=method, params=kwargs)
|
||||
content = self._request(url, method=fn['method'], params=kwargs)
|
||||
|
||||
return content
|
||||
|
||||
|
|
@ -181,18 +160,19 @@ class Twython(object):
|
|||
'''Internal response generator, no sense in repeating the same
|
||||
code twice, right? ;)
|
||||
'''
|
||||
myargs = {}
|
||||
method = method.lower()
|
||||
if not method in ('get', 'post'):
|
||||
raise TwythonError('Method must be of GET or POST')
|
||||
|
||||
params = params or {}
|
||||
|
||||
if method == 'get':
|
||||
url = '%s?%s' % (url, urllib.urlencode(params))
|
||||
else:
|
||||
myargs = params
|
||||
|
||||
func = getattr(self.client, method)
|
||||
response = func(url, data=myargs, files=files, headers=self.headers, auth=self.auth)
|
||||
if method == 'get':
|
||||
# Still wasn't fixed in `requests` 0.13.2? :(
|
||||
url = url + '?' + urllib.urlencode(params)
|
||||
response = func(url)
|
||||
else:
|
||||
response = func(url, data=params, files=files)
|
||||
content = response.content.decode('utf-8')
|
||||
|
||||
# create stash for last function intel
|
||||
|
|
@ -207,10 +187,6 @@ class Twython(object):
|
|||
'content': content,
|
||||
}
|
||||
|
||||
# Python 2.6 `json` will throw a ValueError if it
|
||||
# can't load the string as valid JSON,
|
||||
# `simplejson` will throw simplejson.decoder.JSONDecodeError
|
||||
# But excepting just ValueError will work with both. o.O
|
||||
try:
|
||||
content = simplejson.loads(content)
|
||||
except ValueError:
|
||||
|
|
@ -287,7 +263,7 @@ class Twython(object):
|
|||
request_args['oauth_callback'] = self.callback_url
|
||||
|
||||
req_url = self.request_token_url + '?' + urllib.urlencode(request_args)
|
||||
response = self.client.get(req_url, headers=self.headers, auth=self.auth)
|
||||
response = self.client.get(req_url)
|
||||
|
||||
if response.status_code != 200:
|
||||
raise TwythonAuthError("Seems something couldn't be verified with your OAuth junk. Error: %s, Message: %s" % (response.status_code, response.content))
|
||||
|
|
@ -313,7 +289,7 @@ class Twython(object):
|
|||
def get_authorized_tokens(self):
|
||||
"""Returns authorized tokens after they go through the auth_url phase.
|
||||
"""
|
||||
response = self.client.get(self.access_token_url, headers=self.headers, auth=self.auth)
|
||||
response = self.client.get(self.access_token_url)
|
||||
authorized_tokens = dict(parse_qsl(response.content))
|
||||
if not authorized_tokens:
|
||||
raise TwythonError('Unable to decode authorized tokens.')
|
||||
|
|
@ -382,8 +358,6 @@ class Twython(object):
|
|||
|
||||
e.g x.search(q='jjndf', page='2')
|
||||
"""
|
||||
if 'q' in kwargs:
|
||||
kwargs['q'] = urllib.quote_plus(Twython.unicode2utf8(kwargs['q'], ':'))
|
||||
|
||||
return self.get('https://search.twitter.com/search.json', params=kwargs)
|
||||
|
||||
|
|
@ -398,7 +372,7 @@ class Twython(object):
|
|||
for result in search:
|
||||
print result
|
||||
"""
|
||||
kwargs['q'] = urllib.quote_plus(Twython.unicode2utf8(search_query))
|
||||
kwargs['q'] = search_query
|
||||
content = self.get('https://search.twitter.com/search.json', params=kwargs)
|
||||
|
||||
if not content['results']:
|
||||
|
|
@ -458,7 +432,6 @@ class Twython(object):
|
|||
return self._media_update(url,
|
||||
{'image': (file_, open(file_, 'rb'))})
|
||||
|
||||
# statuses/update_with_media
|
||||
def updateStatusWithMedia(self, file_, version=1, **params):
|
||||
"""Updates the users status with media
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue