This repository was archived by the owner on Apr 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrpweibo.py
More file actions
497 lines (388 loc) · 15.5 KB
/
Copy pathrpweibo.py
File metadata and controls
497 lines (388 loc) · 15.5 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import curl
import pycurl
from io import BytesIO
import urllib.parse
import base64
import rsa
import json
import itertools
import time
__version__ = "0.02.2"
g_retry = -1
def set_retry(times):
global g_retry
g_retry = int(times)
class _Curl(curl.Curl):
"""Returns a pycurl.Curl() with serveral settings."""
def __init__(self, base_url="", fakeheaders=()):
super().__init__(base_url, fakeheaders)
self.set_option(pycurl.SSL_VERIFYPEER, True)
self.set_option(pycurl.ENCODING, "") # accept all encodings
# workaround buggy pycurl versions before Dec 2013
self.payload = None
self.payload_io = BytesIO()
self.set_option(pycurl.WRITEFUNCTION, self.payload_io.write)
def header_callback(x):
if isinstance(x, str):
# workaround buggy pycurl versions
self.hdr += x
else:
self.hdr += x.decode("ascii")
self.set_option(pycurl.HEADERFUNCTION, header_callback)
ssl_library = pycurl.version_info()[5]
# use the only one secure cipher that Sina supports
if "OpenSSL" in ssl_library or "LibreSSL" in ssl_library:
self.set_option(pycurl.SSL_CIPHER_LIST, "ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384")
elif "GnuTLS".lower() in ssl_library.lower(): # not sure about the capitalization, use lower case
self.set_option(pycurl.SSL_CIPHER_LIST, "PFS")
else:
raise NotImplemented("Unsupported SSL/TLS library (%s)!" % ssl_library)
def __request(self, relative_url=None):
super().__request(relative_url)
self.payload = self.payload_io.getvalue().decode("UTF-8")
return self.payload
def get(self, url="", params=None):
"Ship a GET request for a specified URL, capture the response."
if params:
url += "?" + urllib.parse.urlencode(params, doseq=True)
self.set_option(pycurl.HTTPGET, 1)
return self.__request(url)
def post_binary(self, cgi, params):
"Ship a POST request, treats bytes in params as the binary data."
postdata = []
for param, value in params.items():
if isinstance(value, bytes):
postdata.append((param, (pycurl.FORM_BUFFER, param, pycurl.FORM_BUFFERPTR, value)))
else:
postdata.append((param, (pycurl.FORM_CONTENTS, urllib.parse.quote(value))))
self.set_option(pycurl.HTTPPOST, postdata)
return self.__request(cgi)
class WeiboError(Exception):
pass
class RequestError(WeiboError):
pass
class NetworkError(RequestError):
pass
class APIError(RequestError):
def __init__(self, error_code, error_message):
self.error_code = int(error_code)
self.error_message = str(error_message).strip()
def __str__(self):
return "%d: %s" % (self.error_code, self.error_message)
def __repr__(self):
return self.__str__()
class RemoteError(APIError):
pass
class CallerError(APIError):
pass
class ResultCorrupted(RequestError):
pass
class AuthorizeError(WeiboError):
pass
class AuthorizeFailed(AuthorizeError):
pass
class NotAuthorized(AuthorizeError):
pass
class Application():
def __init__(self, app_key, app_secret, redirect_uri):
self.app_key = app_key
self.app_secret = app_secret
self.redirect_uri = redirect_uri
class getable_dict(dict):
def __init__(self, dic):
super().__init__(dic)
def __getattr__(self, attr):
return self[attr]
class Weibo():
API = "https://api.weibo.com/2/%s.json"
HTTP_GET = 1
HTTP_POST = 2
HTTP_UPLOAD = 3
# if you find out more, add the error code to the tuple
UNREASONABLE_ERRORS = (
10003, # Remote service error
10011, # RPC error
21321, # Applications over the unaudited use restrictions
)
PRIVILEGED_APIS = {
"statuses/user_timeline": {"identifier_required": True},
"users/show": {"identifier_required": True},
"users/domain_show": {"identifier_required": False},
"users/counts": {"identifier_required": True},
}
def __init__(self, application):
self.application = application
self._access_token = ""
self._authorize_code = ""
def auth(self, authenticator):
access_token = authenticator.auth(self.application)
if access_token:
self._access_token = access_token
try:
self._authorize_code = authenticator.authorize_code
except AttributeError:
pass
else:
return False
def __request(self, action, api, kwargs, privileged=True):
if not self._access_token:
raise NotAuthorized
# hack for https://github.com/WeCase/WeCase/issues/119
if (privileged and api in self.PRIVILEGED_APIS and self._authorize_code and
(not self.PRIVILEGED_APIS[api]["identifier_required"] or
(self.PRIVILEGED_APIS[api]["identifier_required"] and (("uid" in kwargs) or ("screen_name" in kwargs))))):
if "uid" in kwargs and "screen_name" not in kwargs:
screen_name = self.__request(self.HTTP_GET, "users/show", {"uid": kwargs["uid"]}, privileged=False).get("screen_name")
kwargs["screen_name"] = screen_name
del kwargs["uid"]
kwargs["source"] = self.application.app_key
kwargs["access_token"] = self._authorize_code
else:
kwargs["access_token"] = self._access_token
request_url = self.API % api
curl = _Curl()
if action == self.HTTP_GET:
result = curl.get(request_url, kwargs)
elif action == self.HTTP_POST:
result = curl.post(request_url, kwargs)
elif action == self.HTTP_UPLOAD:
image = kwargs.pop("pic")
kwargs["pic"] = image.read()
image.close()
result = curl.post_binary(request_url, kwargs)
status_code = curl.get_info(pycurl.RESPONSE_CODE)
curl.close()
try:
result_json = json.loads(result, object_hook=getable_dict)
if not isinstance(result_json, dict):
return result_json
if "error_code" in result_json.keys():
raise APIError(result_json["error_code"], result_json["error"])
return getable_dict(result_json)
except (TypeError, ValueError):
if status_code != 200:
raise APIError(status_code, "Unknown Error")
raise ResultCorrupted
def _request(self, action, api, kwargs):
exception = None
delay = 1
for retry in itertools.count():
if retry == g_retry:
break
if retry != 0:
time.sleep(delay)
if retry > 3:
delay = 3
elif retry > 5:
delay = 5
try:
return self.__request(action, api, kwargs)
except APIError as e:
exception = e
if e.error_code in self.UNREASONABLE_ERRORS or exception.error_code <= 10014:
pass
else:
raise CallerError(exception.error_code, exception.error_message)
except ResultCorrupted:
pass
except pycurl.error:
pass
if isinstance(exception, pycurl.error):
raise NetworkError
else:
raise exception
def get(self, api, **kwargs):
return self._request(self.HTTP_GET, api, kwargs)
def post(self, api, **kwargs):
if "pic" in kwargs:
return self._request(self.HTTP_UPLOAD, api, kwargs)
else:
return self._request(self.HTTP_POST, api, kwargs)
def api(self, api):
return WeiboAPI(self, api)
class WeiboAPI():
def __init__(self, weibo, api):
self._weibo = weibo
self._api = api
def get(self, **kwargs):
return self._weibo.get(self._api, **kwargs)
def post(self, **kwargs):
return self._weibo.post(self._api, **kwargs)
class AccessTokenAuthenticator():
def __init__(self, access_token):
self._access_token = access_token
def auth(self, application):
return self._access_token
class UserPassAutheticator():
PRELOGIN_PARAMETER = {
'entry': 'openapi',
'callback': 'sinaSSOController.preloginCallBack',
'rsakt': 'mod',
'client': 'ssologin.js(v1.4.15)',
'su': '',
}
LOGIN_PARAMETER = {
'entry': 'openapi',
'gateway': '1',
'from': '',
'savestate': '0',
'useticket': '1',
'vsnf': '1',
'vsnval': '',
'door': '',
'scope': '', # scope of the application
'su': '',
'service': 'miniblog',
'servertime': '',
'nonce': '',
'pwencode': 'rsa2',
'rsakv': '',
'sp': '',
'encoding': 'UTF-8',
'cdult': '2',
'domain': 'weibo.com',
'prelt': '1609',
'returntype': 'TEXT',
}
OAUTH2_PARAMETER = {
'response_type': 'code',
'action': 'login',
'isLoginSina': 0,
'from': '',
'regCallback': '',
'state': '',
'ticket': '',
'withOfficalFlag': 0
}
PRELOGIN_URL = "https://login.sina.com.cn/sso/prelogin.php"
LOGIN_URL = "https://login.sina.com.cn/sso/login.php?client=%s"
AUTHORIZE_URL = "https://api.weibo.com/oauth2/authorize"
ACCESS_TOKEN_URL = "https://api.weibo.com/oauth2/access_token"
def __init__(self, username, password):
self._username = username
self._password = password
self.authorize_code = ""
def _request_authorize_code(self, application):
# Encode the username to a URL-encoded string.
# Then, calculate its base64, we need it later
username_encoded = urllib.parse.quote(self._username)
username_encoded = username_encoded.encode("UTF-8") # convert to UTF-8-encoded byte string
username_encoded = base64.b64encode(username_encoded)
# First, we need to request prelogin.php for some necessary parameters.
prelogin = self.PRELOGIN_PARAMETER
prelogin['su'] = username_encoded
curl = _Curl()
try:
prelogin_result = curl.get(self.PRELOGIN_URL, prelogin)
except pycurl.error:
raise NetworkError
finally:
curl.close()
# The result is a piece of JavaScript code, in the format of
# sinaSSOController.preloginCallBack({json here})
prelogin_json = prelogin_result.replace("sinaSSOController.preloginCallBack(", "")[0:-1]
prelogin_json = json.loads(prelogin_json)
# Second, we request login.php to request for a authenticate ticket
login = self.LOGIN_PARAMETER
login['su'] = username_encoded
login['servertime'] = prelogin_json['servertime']
login['nonce'] = prelogin_json['nonce']
login['rsakv'] = prelogin_json['rsakv']
# One more thing, we need to encrypt the password with extra token
# using RSA-1024 public key which the server has sent us.
rsa_pubkey_bignum = int(prelogin_json['pubkey'], 16) # the public key is a big number in Hex
rsa_pubkey = rsa.PublicKey(rsa_pubkey_bignum, 65537) # RFC requires e == 65537 for RSA algorithm
plain_msg = "%s\t%s\n%s" % (prelogin_json['servertime'], prelogin_json['nonce'], self._password)
plain_msg = plain_msg.encode('UTF-8') # to byte string
cipher_msg = rsa.encrypt(plain_msg, rsa_pubkey)
cipher_msg = base64.b16encode(cipher_msg) # to Hex
login['sp'] = cipher_msg
curl = _Curl()
try:
login_result = curl.post(self.LOGIN_URL % "ssologin.js(v1.4.15)", login)
except pycurl.error:
raise NetworkError
finally:
curl.close()
# the result is a JSON string
# if success, Sina will give us a ticket for this authorized session
login_json = json.loads(login_result)
if "ticket" not in login_json:
raise AuthorizeFailed(str(login_json))
oauth2 = self.OAUTH2_PARAMETER
oauth2['ticket'] = login_json['ticket'] # it's what all we need
oauth2['client_id'] = application.app_key
oauth2['redirect_uri'] = application.redirect_uri
curl = _Curl()
curl.set_option(pycurl.FOLLOWLOCATION, False) # don't follow redirect
curl.set_option(pycurl.REFERER, self.AUTHORIZE_URL) # required for auth
try:
# After post the OAuth2 information, if success,
# Sina will return "302 Moved Temporarily", the target is "http://redirect_uri/?code=xxxxxx",
# xxxxxx is the authorize code.
curl.post(self.AUTHORIZE_URL, oauth2)
redirect_url = curl.get_info(pycurl.REDIRECT_URL)
except pycurl.error:
raise NetworkError
finally:
curl.close()
if not redirect_url:
raise AuthorizeFailed("Invalid Application() or wrong username/password.")
authorize_code = redirect_url.split("=")[1]
self.authorize_code = authorize_code
return authorize_code
def _request_access_token(self, application, authorize_code):
access_token_parameter = {
'client_id': application.app_key,
'client_secret': application.app_secret,
'grant_type': 'authorization_code',
'code': authorize_code,
'redirect_uri': application.redirect_uri
}
curl = _Curl()
try:
result = curl.post(self.ACCESS_TOKEN_URL, access_token_parameter)
except pycurl.error:
raise NetworkError
finally:
curl.close()
try:
return json.loads(result)["access_token"]
except KeyError:
raise AuthorizeError
def auth(self, application):
authorize_code = self._request_authorize_code(application)
return self._request_access_token(application, authorize_code)
class ManualAutheticator():
WEIBO_DOMAIN = "api.weibo.com"
AUTHORIZE_URL = "https://%s/oauth2/authorize" % WEIBO_DOMAIN
ACCESS_TOKEN_URL = "https://%s/oauth2/access_token" % WEIBO_DOMAIN
def __init__(self):
pass
def _request_authorize_code(self, application):
print("Please open %s?client_id=%s&redirect_uri=%s in the web browser" % (self.AUTHORIZE_URL, application.app_key, application.redirect_uri))
authorize_code = input("Authorize Code: ").strip()
return authorize_code
def _request_access_token(self, application, authorize_code):
access_token_parameter = {
'client_id': application.app_key,
'client_secret': application.app_secret,
'grant_type': 'authorization_code',
'code': authorize_code,
'redirect_uri': application.redirect_uri
}
curl = _Curl()
try:
result = curl.post(self.ACCESS_TOKEN_URL, access_token_parameter)
status_code = curl.get_info(pycurl.RESPONSE_CODE)
if status_code != 200:
result_json = json.loads(result, object_hook=getable_dict)
raise AuthorizeFailed(result_json)
except pycurl.error:
raise NetworkError
finally:
curl.close()
return json.loads(result)["access_token"]
def auth(self, application):
authorize_code = self._request_authorize_code(application)
return self._request_access_token(application, authorize_code)