-
Notifications
You must be signed in to change notification settings - Fork 1
/
oauthtwitter.py
291 lines (234 loc) · 10 KB
/
oauthtwitter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python
#
# Copyright under the latest Apache License 2.0
'''
A modification of the python twitter oauth library by Hameedullah Khan.
Instead of inheritance from the python-twitter library, it currently
exists standalone with an all encompasing ApiCall function. There are
plans to provide wrapper functions around common requests in the future.
Requires:
simplejson
oauth2
'''
__author__ = "Konpaku Kogasa, Hameedullah Khan"
__version__ = "0.1"
# Library modules
import urllib
import urllib2
import urlparse
import time
# Non library modules
import simplejson
import oauth2 as oauth
# Taken from oauth implementation at: http://github.com/harperreed/twitteroauth-python/tree/master
REQUEST_TOKEN_URL = 'https://twitter.com/oauth/request_token'
ACCESS_TOKEN_URL = 'https://twitter.com/oauth/access_token'
AUTHORIZATION_URL = 'http://twitter.com/oauth/authorize'
SIGNIN_URL = 'http://twitter.com/oauth/authenticate'
class OAuthApi():
def __init__(self, consumer_key, consumer_secret, token=None, token_secret=None):
if token and token_secret:
token = oauth.Token(token, token_secret)
else:
token = None
self._Consumer = oauth.Consumer(consumer_key, consumer_secret)
self._signature_method = oauth.SignatureMethod_HMAC_SHA1()
self._access_token = token
def _GetOpener(self):
opener = urllib2.build_opener()
return opener
def _FetchUrl(self,
url,
http_method=None,
parameters=None):
'''Fetch a URL, optionally caching for a specified time.
Args:
url: The URL to retrieve
http_method:
One of "GET" or "POST" to state which kind
of http call is being made
parameters:
A dict whose key/value pairs should encoded and added
to the query string, or generated into post data. [OPTIONAL]
depending on the http_method parameter
Returns:
A string containing the body of the response.
'''
# Build the extra parameters dict
extra_params = {}
if parameters:
extra_params.update(parameters)
req = self._makeOAuthRequest(url, params=extra_params,
http_method=http_method)
# Get a url opener that can handle Oauth basic auth
opener = self._GetOpener()
if http_method == "POST":
encoded_post_data = req.to_postdata()
url = req.get_normalized_http_url()
else:
url = req.to_url()
encoded_post_data = ""
if encoded_post_data:
url_data = opener.open(url, encoded_post_data).read()
else:
url_data = opener.open(url).read()
opener.close()
# Always return the latest version
return url_data
def _makeOAuthRequest(self, url, token=None,
params=None, http_method="GET"):
'''Make a OAuth request from url and parameters
Args:
url: The Url to use for creating OAuth Request
parameters:
The URL parameters
http_method:
The HTTP method to use
Returns:
A OAauthRequest object
'''
oauth_base_params = {
'oauth_version': "1.0",
'oauth_nonce': oauth.generate_nonce(),
'oauth_timestamp': int(time.time())
}
if params:
params.update(oauth_base_params)
else:
params = oauth_base_params
if not token:
token = self._access_token
request = oauth.Request(method=http_method,url=url,parameters=params)
request.sign_request(self._signature_method, self._Consumer, token)
return request
def getAuthorizationURL(self, token, url=AUTHORIZATION_URL):
'''Create a signed authorization URL
Returns:
A signed OAuthRequest authorization URL
'''
return "%s?oauth_token=%s" % (url, token['oauth_token'])
def getRequestToken(self, url=REQUEST_TOKEN_URL):
'''Get a Request Token from Twitter
Returns:
A OAuthToken object containing a request token
'''
resp, content = oauth.Client(self._Consumer).request(url, "GET")
if resp['status'] != '200':
raise Exception("Invalid response %s." % resp['status'])
return dict(urlparse.parse_qsl(content))
def getAccessToken(self, token, verifier, url=ACCESS_TOKEN_URL):
'''Get a Request Token from Twitter
Returns:
A OAuthToken object containing a request token
'''
token = oauth.Token(token['oauth_token'], token['oauth_token_secret'])
token.set_verifier(verifier)
client = oauth.Client(self._Consumer, token)
resp, content = client.request(url, "POST")
return dict(urlparse.parse_qsl(content))
def FollowUser(self, user_id, options = {}):
'''Follow a user with a given user id
Args:
user_id: The id of the user to follow
options:
A dict of options for the friendships/create call.
See the link below for what options can be passed
http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-friendships%C2%A0create
'''
options['user_id'] = user_id
self.ApiCall("friendships/create", "POST", options)
def GetFriends(self, options={}):
'''Return a list of users you are following
Args:
options:
A dict of options for the statuses/friends call.
See the link below for what options can be passed
http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses%C2%A0friends
options['cursor']:
By default twitter returns a list of 100
followers. If you have more, you will need to
use the cursor value to paginate the results.
A value of -1 means to get the first page of results.
the returned data will have next_cursor and previous_cursor
to help you continue pagination
Return: Up to 100 friends in dict format
'''
return self.ApiCall("statuses/friends", "GET", options)
def GetFollowers(self, options={}):
'''Return followers
Args:
options:
A dict of options for the statuses/followers call.
See the link below for what options can be passed
http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses%C2%A0followers
options['cursor']:
By default twitter returns a list of 100
followers. If you have more, you will need to
use the cursor value to paginate the results.
A value of -1 means to get the first page of results.
the returned data will have next_cursor and previous_cursor
to help you continue pagination
Return: Up to 100 followers in dict format
'''
return self.ApiCall("statuses/followers", "GET", options)
def GetFriendsTimeline(self, options = {}):
'''Get the friends timeline. Does not contain retweets.
Args:
options:
A dict of options for the statuses/friends_timeline call.
See the link below for what options can be passed
http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-friends_timeline
Return: The friends timeline in dict format
'''
return self.ApiCall("statuses/friends_timeline", "GET", options)
def GetHomeTimeline(self, options={}):
'''Get the home timeline. Unlike friends timeline it also contains retweets
Args:
options:
A dict of options for the statuses/home_timeline call.
See the link below for what options can be passed
http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-home_timeline
Return: The home timeline in dict format
'''
return self.ApiCall("statuses/home_timeline", "GET", options)
def GetUserTimeline(self, options={}):
'''Get the user timeline. These are tweets just by a user, and do not contain retweets
Args:
options:
A dict of options for the statuses/user_timeline call.
See the link below for what options can be passed
http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-user_timeline
Return: The home timeline in dict format
'''
return self.ApiCall("statuses/user_timeline", "GET", options)
def GetPublicTimeline(self):
'''
Get the public timeline, which is the 20 most recent statuses from non-protected
and custom icon users. According to the API docs, this is cached for 60 seconds.
Return: The public timeline in dict format
'''
return self.ApiCall("statuses/public_timeline", "GET", {})
def UpdateStatus(self, status, options = {}):
'''
Args:
status: The status you wish to update to
options:
A dict of options for the statuses/update call.
See the link below for what options can be passed
http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses%C2%A0update
Returns:
Whether or not the status update suceeded
'''
options['status'] = status
self.ApiCall("statuses/update", "POST", options)
def ApiCall(self, call, type="GET", parameters={}):
'''Calls the twitter API
Args:
call: The name of the api call (ie. account/rate_limit_status)
type: One of "GET" or "POST"
parameters: Parameters to pass to the Twitter API call
Returns:
Returns the twitter.User object
'''
json = self._FetchUrl("https://api.twitter.com/1/" + call + ".json", type, parameters)
return simplejson.loads(json)