diff --git a/NOTICE b/NOTICE index 3fd547fd..ce8e6d5d 100644 --- a/NOTICE +++ b/NOTICE @@ -1,13 +1,3 @@ NOTICE -The simplejson library (http://simplejson.googlecode.com) is used under the terms of the MIT license and is copyright Bob Ippolito. -See http://simplejson.googlecode.com/svn/trunk/LICENSE.txt for details. - -The python-oauth2 library (http://github.com/simplegeo/python-oauth2) is used under the terms of the MIT license and is copyright Leah Culver. -See http://github.com/simplegeo/python-oauth2/blob/master/LICENSE.txt for details. - -The httplib2 library (http://code.google.com/p/httplib2) is used under the terms of the MIT license and is copyright Joe Gregorio. -See http://code.google.com/p/httplib2/source/browse/python2/httplib2/__init__.py for details. - This code is made available under the Apache License and is copyright the Python-Twitter Developers. - diff --git a/README.rst b/README.rst index d2e70a30..71e8ecaa 100644 --- a/README.rst +++ b/README.rst @@ -8,7 +8,7 @@ By the Python-Twitter Developers Introduction ============ -This library provides a pure Python interface for the `Twitter API https://dev.twitter.com/`. It works with Python versions from 2.5 to 2.7. Python 3 support is under development. +This library provides a pure Python interface for the `Twitter API https://dev.twitter.com/`. It works with Python versions from 2.6+. Python 3 support is under development. `Twitter http://twitter.com` provides a service that allows people to connect via the web, IM, and SMS. Twitter exposes a `web services API http://dev.twitter.com/doc` and this library is intended to make it even easier for Python programmers to use. @@ -38,7 +38,6 @@ Check out the latest development version anonymously with:: Dependencies * [Requests](http://docs.python-requests.org/en/latest/) -* [SimpleJson](http://cheeseshop.python.org/pypi/simplejson) * [Requests OAuthlib](https://requests-oauthlib.readthedocs.org/en/latest/) ============= diff --git a/doc/index.rst b/doc/index.rst index db13599a..a9b88ed7 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -11,7 +11,7 @@ Author: The Python-Twitter Developers Introduction ------------ -This library provides a pure Python interface for the `Twitter API `_. It works with Python versions from 2.5 to 2.7. Python 3 support is under development. +This library provides a pure Python interface for the `Twitter API `_. It works with Python version 2.6+. Python 3 support is under development. `Twitter `_ provides a service that allows people to connect via the web, IM, and SMS. Twitter exposes a `web services API `_ and this library is intended to make it even easier for Python programmers to use. @@ -22,14 +22,8 @@ From source: Install the dependencies: -- `SimpleJson `_ -- `Requests OAuthlib `_ -- `HTTPLib2 `_ - -This branch is currently in development to replace the OAuth and HTTPLib2 libarays with the following: - - `Requests `_ - +- `Requests OAuthlib `_ Alternatively use `pip`:: diff --git a/examples/shorten_url.py b/examples/shorten_url.py index 01eaacd4..3401bec2 100755 --- a/examples/shorten_url.py +++ b/examples/shorten_url.py @@ -48,7 +48,7 @@ def __init__(self, self.password = password def Shorten(self, - longURL): + longURL): '''Call TinyURL API and returned shortened URL result Args: diff --git a/get_access_token.py b/get_access_token.py index 2af7a40f..c77f8946 100755 --- a/get_access_token.py +++ b/get_access_token.py @@ -14,14 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -# parse_qsl moved to urlparse module in v2.6 -try: - from urlparse import parse_qsl -except: - from cgi import parse_qsl - import webbrowser -import oauth2 as oauth +from requests_oauthlib import OAuth1Session REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token' ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token' @@ -31,50 +25,48 @@ def get_access_token(consumer_key, consumer_secret): - signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1() - oauth_consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret) - oauth_client = oauth.Client(oauth_consumer) + oauth_client = OAuth1Session(consumer_key, client_secret=consumer_secret) print 'Requesting temp token from Twitter' - resp, content = oauth_client.request(REQUEST_TOKEN_URL, 'POST', body="oauth_callback=oob") - - if resp['status'] != '200': - print 'Invalid respond from Twitter requesting temp token: %s' % resp['status'] - else: - request_token = dict(parse_qsl(content)) - url = '%s?oauth_token=%s' % (AUTHORIZATION_URL, request_token['oauth_token']) - - print '' - print 'I will try to start a browser to visit the following Twitter page' - print 'if a browser will not start, copy the URL to your browser' - print 'and retrieve the pincode to be used' - print 'in the next step to obtaining an Authentication Token:' - print '' - print url - print '' - - webbrowser.open(url) - pincode = raw_input('Pincode? ') - - token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret']) - token.set_verifier(pincode) - - print '' - print 'Generating and signing request for an access token' - print '' - - oauth_client = oauth.Client(oauth_consumer, token) - resp, content = oauth_client.request(ACCESS_TOKEN_URL, method='POST', body='oauth_callback=oob&oauth_verifier=%s' % pincode) - access_token = dict(parse_qsl(content)) - - if resp['status'] != '200': - print 'The request for a Token did not succeed: %s' % resp['status'] - print access_token - else: - print 'Your Twitter Access Token key: %s' % access_token['oauth_token'] - print ' Access Token secret: %s' % access_token['oauth_token_secret'] - print '' + try: + resp = oauth_client.fetch_request_token(REQUEST_TOKEN_URL) + except ValueError, e: + print 'Invalid respond from Twitter requesting temp token: %s' % e + return + url = oauth_client.authorization_url(AUTHORIZATION_URL) + + print '' + print 'I will try to start a browser to visit the following Twitter page' + print 'if a browser will not start, copy the URL to your browser' + print 'and retrieve the pincode to be used' + print 'in the next step to obtaining an Authentication Token:' + print '' + print url + print '' + + webbrowser.open(url) + pincode = raw_input('Pincode? ') + + + print '' + print 'Generating and signing request for an access token' + print '' + + oauth_client = OAuth1Session(consumer_key, client_secret=consumer_secret, + resource_owner_key=resp.get('oauth_token'), + resource_owner_secret=resp.get('oauth_token_secret'), + verifier=pincode + ) + try: + resp = oauth_client.fetch_access_token(ACCESS_TOKEN_URL) + except ValueError, e: + print 'Invalid respond from Twitter requesting access token: %s' % e + return + + print 'Your Twitter Access Token key: %s' % resp.get('oauth_token') + print ' Access Token secret: %s' % resp.get('oauth_token_secret') + print '' def main(): diff --git a/setup.py b/setup.py index 18e20a36..c629ac21 100755 --- a/setup.py +++ b/setup.py @@ -38,7 +38,7 @@ def read(*paths): read('AUTHORS.rst') + '\n\n' + read('CHANGES')), packages=find_packages(exclude=['tests*']), - install_requires = ['simplejson', 'requests', 'requests-oauthlib'], + install_requires = ['requests', 'requests-oauthlib'], classifiers=[ 'Development Status :: 5 - Production/Stable', 'Intended Audience :: Developers', diff --git a/twitter/__init__.py b/twitter/__init__.py index 22bf869b..7b2c79a8 100644 --- a/twitter/__init__.py +++ b/twitter/__init__.py @@ -21,24 +21,7 @@ __author__ = 'python-twitter@googlegroups.com' __version__ = '2.0' -try: - # Python >= 2.6 - import json as simplejson -except ImportError: - try: - # Python < 2.6 - import simplejson - except ImportError: - try: - # Google App Engine - from django.utils import simplejson - except ImportError: - raise ImportError, "Unable to load a json library" -# parse_qsl moved to urlparse module in v2.6 -try: - from urlparse import parse_qsl, parse_qs -except ImportError: - from cgi import parse_qsl, parse_qs +import json as simplejson try: from hashlib import md5 diff --git a/twitter/api.py b/twitter/api.py index 011d1397..2aeed395 100644 --- a/twitter/api.py +++ b/twitter/api.py @@ -119,7 +119,7 @@ def __init__(self, stream_url=None, use_gzip_compression=False, debugHTTP=False, - requests_timeout=None): + timeout=None): '''Instantiate a new twitter.Api object. Args: @@ -152,7 +152,7 @@ def __init__(self, debugHTTP: Set to True to enable debug output from urllib2 when performing any HTTP requests. Defaults to False. [Optional] - requests_timeout: + timeout: Set timeout (in seconds) of the http/https requests. If None the requests lib default will be used. Defaults to None. [Optional] ''' @@ -163,7 +163,7 @@ def __init__(self, self._use_gzip = use_gzip_compression self._debugHTTP = debugHTTP self._shortlink_size = 19 - self._requests_timeout = requests_timeout + self._timeout = timeout self._InitializeRequestHeaders(request_headers) self._InitializeUserAgent() @@ -897,7 +897,8 @@ def PostMedia(self, status: the text of your update media: - location of media(PNG, JPG, GIF) + This can be the location of media(PNG, JPG, GIF) on the local file + system or at an HTTP URL, it can also be a file-like object possibly_sensitive: set true if content is "advanced." [Optional] in_reply_to_status_id: @@ -925,10 +926,14 @@ def PostMedia(self, u_status = unicode(status, self._input_encoding) data = {'status': status} - if media.startswith('http'): + if not hasattr(media, 'read'): + if media.startswith('http'): data['media'] = urllib2.urlopen(media).read() + else: + with open(str(media), 'rb') as f: + data['media'] = f.read() else: - data['media'] = open(str(media), 'rb').read() + data['media'] = media.read() if possibly_sensitive: data['possibly_sensitive'] = 'true' if in_reply_to_status_id: @@ -940,7 +945,7 @@ def PostMedia(self, data['place_id'] = str(place_id) if display_coordinates: data['display_coordinates'] = 'true' - + json = self._RequestUrl(url, 'POST', data=data) data = self._ParseAndCheckTwitter(json.content) @@ -3460,7 +3465,7 @@ def _RequestUrl(self, url, verb, data=None): url, files=data, auth=self.__auth, - timeout=self._requests_timeout + timeout=self._timeout ) except requests.RequestException as e: raise TwitterError(str(e)) @@ -3470,7 +3475,7 @@ def _RequestUrl(self, url, verb, data=None): url, data=data, auth=self.__auth, - timeout=self._requests_timeout + timeout=self._timeout ) except requests.RequestException as e: raise TwitterError(str(e)) @@ -3480,7 +3485,7 @@ def _RequestUrl(self, url, verb, data=None): return requests.get( url, auth=self.__auth, - timeout=self._requests_timeout + timeout=self._timeout ) except requests.RequestException as e: raise TwitterError(str(e)) @@ -3504,7 +3509,7 @@ def _RequestStream(self, url, verb, data=None): try: return requests.post(url, data=data, stream=True, auth=self.__auth, - timeout=self._requests_timeout + timeout=self._timeout ) except requests.RequestException as e: raise TwitterError(str(e)) @@ -3512,7 +3517,7 @@ def _RequestStream(self, url, verb, data=None): url = self._BuildUrl(url, extra_params=data) try: return requests.get(url, stream=True, auth=self.__auth, - timeout=self._requests_timeout + timeout=self._timeout ) except requests.RequestException as e: raise TwitterError(str(e)) diff --git a/twitter_test.py b/twitter_test.py index 53ecbfd9..54664bf0 100755 --- a/twitter_test.py +++ b/twitter_test.py @@ -21,7 +21,7 @@ __author__ = 'python-twitter@googlegroups.com' import os -import simplejson +import json as simplejson import time import calendar import unittest