Skip to content

Commit 1d792f4

Browse files
committed
merge conflict update
2 parents 1a28a85 + a3f8ff2 commit 1d792f4

File tree

9 files changed

+64
-101
lines changed

9 files changed

+64
-101
lines changed

NOTICE

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
11
NOTICE
22

3-
The simplejson library (http://simplejson.googlecode.com) is used under the terms of the MIT license and is copyright Bob Ippolito.
4-
See http://simplejson.googlecode.com/svn/trunk/LICENSE.txt for details.
5-
6-
The python-oauth2 library (http://github.com/simplegeo/python-oauth2) is used under the terms of the MIT license and is copyright Leah Culver.
7-
See http://github.com/simplegeo/python-oauth2/blob/master/LICENSE.txt for details.
8-
9-
The httplib2 library (http://code.google.com/p/httplib2) is used under the terms of the MIT license and is copyright Joe Gregorio.
10-
See http://code.google.com/p/httplib2/source/browse/python2/httplib2/__init__.py for details.
11-
123
This code is made available under the Apache License and is copyright the Python-Twitter Developers.
13-

README.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ By the Python-Twitter Developers <python-twitter@googlegroups.com>
88
Introduction
99
============
1010

11-
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.
11+
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.
1212

1313
`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.
1414

@@ -38,7 +38,6 @@ Check out the latest development version anonymously with::
3838
Dependencies
3939

4040
* [Requests](http://docs.python-requests.org/en/latest/)
41-
* [SimpleJson](http://cheeseshop.python.org/pypi/simplejson)
4241
* [Requests OAuthlib](https://requests-oauthlib.readthedocs.org/en/latest/)
4342

4443
=============

doc/index.rst

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Author: The Python-Twitter Developers <python-twitter@googlegroups.com>
1111

1212
Introduction
1313
------------
14-
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.
14+
This library provides a pure Python interface for the `Twitter API <https://dev.twitter.com/>`_. It works with Python version 2.6+. Python 3 support is under development.
1515

1616
`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.
1717

@@ -22,14 +22,8 @@ From source:
2222

2323
Install the dependencies:
2424

25-
- `SimpleJson <http://cheeseshop.python.org/pypi/simplejson>`_
26-
- `Requests OAuthlib <https://requests-oauthlib.readthedocs.org/en/latest/>`_
27-
- `HTTPLib2 <http://code.google.com/p/httplib2/>`_
28-
29-
This branch is currently in development to replace the OAuth and HTTPLib2 libarays with the following:
30-
3125
- `Requests <http://docs.python-requests.org/en/latest/>`_
32-
26+
- `Requests OAuthlib <https://requests-oauthlib.readthedocs.org/en/latest/>`_
3327

3428
Alternatively use `pip`::
3529

examples/shorten_url.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def __init__(self,
4848
self.password = password
4949

5050
def Shorten(self,
51-
longURL):
51+
longURL):
5252
'''Call TinyURL API and returned shortened URL result
5353
5454
Args:

get_access_token.py

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,8 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
# parse_qsl moved to urlparse module in v2.6
18-
try:
19-
from urlparse import parse_qsl
20-
except:
21-
from cgi import parse_qsl
22-
2317
import webbrowser
24-
import oauth2 as oauth
18+
from requests_oauthlib import OAuth1Session
2519

2620
REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token'
2721
ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token'
@@ -31,50 +25,48 @@
3125

3226
def get_access_token(consumer_key, consumer_secret):
3327

34-
signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()
35-
oauth_consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret)
36-
oauth_client = oauth.Client(oauth_consumer)
28+
oauth_client = OAuth1Session(consumer_key, client_secret=consumer_secret)
3729

3830
print 'Requesting temp token from Twitter'
3931

40-
resp, content = oauth_client.request(REQUEST_TOKEN_URL, 'POST', body="oauth_callback=oob")
41-
42-
if resp['status'] != '200':
43-
print 'Invalid respond from Twitter requesting temp token: %s' % resp['status']
44-
else:
45-
request_token = dict(parse_qsl(content))
46-
url = '%s?oauth_token=%s' % (AUTHORIZATION_URL, request_token['oauth_token'])
47-
48-
print ''
49-
print 'I will try to start a browser to visit the following Twitter page'
50-
print 'if a browser will not start, copy the URL to your browser'
51-
print 'and retrieve the pincode to be used'
52-
print 'in the next step to obtaining an Authentication Token:'
53-
print ''
54-
print url
55-
print ''
56-
57-
webbrowser.open(url)
58-
pincode = raw_input('Pincode? ')
59-
60-
token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
61-
token.set_verifier(pincode)
62-
63-
print ''
64-
print 'Generating and signing request for an access token'
65-
print ''
66-
67-
oauth_client = oauth.Client(oauth_consumer, token)
68-
resp, content = oauth_client.request(ACCESS_TOKEN_URL, method='POST', body='oauth_callback=oob&oauth_verifier=%s' % pincode)
69-
access_token = dict(parse_qsl(content))
70-
71-
if resp['status'] != '200':
72-
print 'The request for a Token did not succeed: %s' % resp['status']
73-
print access_token
74-
else:
75-
print 'Your Twitter Access Token key: %s' % access_token['oauth_token']
76-
print ' Access Token secret: %s' % access_token['oauth_token_secret']
77-
print ''
32+
try:
33+
resp = oauth_client.fetch_request_token(REQUEST_TOKEN_URL)
34+
except ValueError, e:
35+
print 'Invalid respond from Twitter requesting temp token: %s' % e
36+
return
37+
url = oauth_client.authorization_url(AUTHORIZATION_URL)
38+
39+
print ''
40+
print 'I will try to start a browser to visit the following Twitter page'
41+
print 'if a browser will not start, copy the URL to your browser'
42+
print 'and retrieve the pincode to be used'
43+
print 'in the next step to obtaining an Authentication Token:'
44+
print ''
45+
print url
46+
print ''
47+
48+
webbrowser.open(url)
49+
pincode = raw_input('Pincode? ')
50+
51+
52+
print ''
53+
print 'Generating and signing request for an access token'
54+
print ''
55+
56+
oauth_client = OAuth1Session(consumer_key, client_secret=consumer_secret,
57+
resource_owner_key=resp.get('oauth_token'),
58+
resource_owner_secret=resp.get('oauth_token_secret'),
59+
verifier=pincode
60+
)
61+
try:
62+
resp = oauth_client.fetch_access_token(ACCESS_TOKEN_URL)
63+
except ValueError, e:
64+
print 'Invalid respond from Twitter requesting access token: %s' % e
65+
return
66+
67+
print 'Your Twitter Access Token key: %s' % resp.get('oauth_token')
68+
print ' Access Token secret: %s' % resp.get('oauth_token_secret')
69+
print ''
7870

7971

8072
def main():

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def read(*paths):
3838
read('AUTHORS.rst') + '\n\n' +
3939
read('CHANGES')),
4040
packages=find_packages(exclude=['tests*']),
41-
install_requires = ['simplejson', 'requests', 'requests-oauthlib'],
41+
install_requires = ['requests', 'requests-oauthlib'],
4242
classifiers=[
4343
'Development Status :: 5 - Production/Stable',
4444
'Intended Audience :: Developers',

twitter/__init__.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,7 @@
2121
__author__ = 'python-twitter@googlegroups.com'
2222
__version__ = '2.0'
2323

24-
try:
25-
# Python >= 2.6
26-
import json as simplejson
27-
except ImportError:
28-
try:
29-
# Python < 2.6
30-
import simplejson
31-
except ImportError:
32-
try:
33-
# Google App Engine
34-
from django.utils import simplejson
35-
except ImportError:
36-
raise ImportError, "Unable to load a json library"
37-
# parse_qsl moved to urlparse module in v2.6
38-
try:
39-
from urlparse import parse_qsl, parse_qs
40-
except ImportError:
41-
from cgi import parse_qsl, parse_qs
24+
import json as simplejson
4225

4326
try:
4427
from hashlib import md5

twitter/api.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def __init__(self,
119119
stream_url=None,
120120
use_gzip_compression=False,
121121
debugHTTP=False,
122-
requests_timeout=None):
122+
timeout=None):
123123
'''Instantiate a new twitter.Api object.
124124
125125
Args:
@@ -152,7 +152,7 @@ def __init__(self,
152152
debugHTTP:
153153
Set to True to enable debug output from urllib2 when performing
154154
any HTTP requests. Defaults to False. [Optional]
155-
requests_timeout:
155+
timeout:
156156
Set timeout (in seconds) of the http/https requests. If None the
157157
requests lib default will be used. Defaults to None. [Optional]
158158
'''
@@ -163,7 +163,7 @@ def __init__(self,
163163
self._use_gzip = use_gzip_compression
164164
self._debugHTTP = debugHTTP
165165
self._shortlink_size = 19
166-
self._requests_timeout = requests_timeout
166+
self._timeout = timeout
167167

168168
self._InitializeRequestHeaders(request_headers)
169169
self._InitializeUserAgent()
@@ -897,7 +897,8 @@ def PostMedia(self,
897897
status:
898898
the text of your update
899899
media:
900-
location of media(PNG, JPG, GIF)
900+
This can be the location of media(PNG, JPG, GIF) on the local file
901+
system or at an HTTP URL, it can also be a file-like object
901902
possibly_sensitive:
902903
set true if content is "advanced." [Optional]
903904
in_reply_to_status_id:
@@ -925,10 +926,14 @@ def PostMedia(self,
925926
u_status = unicode(status, self._input_encoding)
926927

927928
data = {'status': status}
928-
if media.startswith('http'):
929+
if not hasattr(media, 'read'):
930+
if media.startswith('http'):
929931
data['media'] = urllib2.urlopen(media).read()
932+
else:
933+
with open(str(media), 'rb') as f:
934+
data['media'] = f.read()
930935
else:
931-
data['media'] = open(str(media), 'rb').read()
936+
data['media'] = media.read()
932937
if possibly_sensitive:
933938
data['possibly_sensitive'] = 'true'
934939
if in_reply_to_status_id:
@@ -940,7 +945,7 @@ def PostMedia(self,
940945
data['place_id'] = str(place_id)
941946
if display_coordinates:
942947
data['display_coordinates'] = 'true'
943-
948+
944949
json = self._RequestUrl(url, 'POST', data=data)
945950
data = self._ParseAndCheckTwitter(json.content)
946951

@@ -3460,7 +3465,7 @@ def _RequestUrl(self, url, verb, data=None):
34603465
url,
34613466
files=data,
34623467
auth=self.__auth,
3463-
timeout=self._requests_timeout
3468+
timeout=self._timeout
34643469
)
34653470
except requests.RequestException as e:
34663471
raise TwitterError(str(e))
@@ -3470,7 +3475,7 @@ def _RequestUrl(self, url, verb, data=None):
34703475
url,
34713476
data=data,
34723477
auth=self.__auth,
3473-
timeout=self._requests_timeout
3478+
timeout=self._timeout
34743479
)
34753480
except requests.RequestException as e:
34763481
raise TwitterError(str(e))
@@ -3480,7 +3485,7 @@ def _RequestUrl(self, url, verb, data=None):
34803485
return requests.get(
34813486
url,
34823487
auth=self.__auth,
3483-
timeout=self._requests_timeout
3488+
timeout=self._timeout
34843489
)
34853490
except requests.RequestException as e:
34863491
raise TwitterError(str(e))
@@ -3504,15 +3509,15 @@ def _RequestStream(self, url, verb, data=None):
35043509
try:
35053510
return requests.post(url, data=data, stream=True,
35063511
auth=self.__auth,
3507-
timeout=self._requests_timeout
3512+
timeout=self._timeout
35083513
)
35093514
except requests.RequestException as e:
35103515
raise TwitterError(str(e))
35113516
if verb == 'GET':
35123517
url = self._BuildUrl(url, extra_params=data)
35133518
try:
35143519
return requests.get(url, stream=True, auth=self.__auth,
3515-
timeout=self._requests_timeout
3520+
timeout=self._timeout
35163521
)
35173522
except requests.RequestException as e:
35183523
raise TwitterError(str(e))

twitter_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
__author__ = 'python-twitter@googlegroups.com'
2222

2323
import os
24-
import simplejson
24+
import json as simplejson
2525
import time
2626
import calendar
2727
import unittest

0 commit comments

Comments
 (0)