LearnPython/python_weibo.py

153 lines
5.3 KiB
Python
Raw Normal View History

2016-10-19 17:03:28 +08:00
# _*_ coding: utf-8 _*_
2016-10-21 15:47:10 +08:00
"""
python_weibo.py by xianhu
"""
2016-10-19 17:03:28 +08:00
import re
import rsa
import time
import json
import base64
import logging
import binascii
2016-11-07 10:24:36 +08:00
import requests
2016-10-19 17:03:28 +08:00
import urllib.parse
class WeiBoLogin(object):
"""
class of WeiBoLogin, to login weibo.com
"""
def __init__(self):
"""
constructor
"""
2016-11-07 10:24:36 +08:00
self.user_name = None
self.pass_word = None
self.user_uniqueid = None
self.user_nick = None
2016-10-19 17:03:28 +08:00
2016-11-07 10:24:36 +08:00
self.session = requests.Session()
self.session.headers.update({"User-Agent": "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0"})
self.session.get("http://weibo.com/login.php")
2016-10-19 17:03:28 +08:00
return
2016-11-07 10:24:36 +08:00
def login(self, user_name, pass_word):
2016-10-19 17:03:28 +08:00
"""
login weibo.com, return True or False
"""
self.user_name = user_name
self.pass_word = pass_word
self.user_uniqueid = None
self.user_nick = None
2016-11-07 10:24:36 +08:00
# get json data
2016-10-19 17:03:28 +08:00
s_user_name = self.get_username()
json_data = self.get_json_data(su_value=s_user_name)
if not json_data:
return False
s_pass_word = self.get_password(json_data["servertime"], json_data["nonce"], json_data["pubkey"])
2016-11-07 10:24:36 +08:00
# make post_data
post_data = {
2016-10-19 17:03:28 +08:00
"entry": "weibo",
"gateway": "1",
"from": "",
"savestate": "7",
"userticket": "1",
"vsnf": "1",
"service": "miniblog",
"encoding": "UTF-8",
"pwencode": "rsa2",
"sr": "1280*800",
"prelt": "529",
"url": "http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack",
"rsakv": json_data["rsakv"],
"servertime": json_data["servertime"],
"nonce": json_data["nonce"],
"su": s_user_name,
"sp": s_pass_word,
"returntype": "TEXT",
}
2016-11-07 10:24:36 +08:00
# get captcha code
if json_data["showpin"] == 1:
2016-10-19 17:03:28 +08:00
url = "http://login.sina.com.cn/cgi/pin.php?r=%d&s=0&p=%s" % (int(time.time()), json_data["pcid"])
with open("captcha.jpeg", "wb") as file_out:
2016-11-07 10:24:36 +08:00
file_out.write(self.session.get(url).content)
2016-10-19 17:03:28 +08:00
code = input("请输入验证码:")
2016-11-07 10:24:36 +08:00
post_data["pcid"] = json_data["pcid"]
post_data["door"] = code
2016-10-19 17:03:28 +08:00
2016-11-07 10:24:36 +08:00
# login weibo.com
2016-10-19 17:03:28 +08:00
login_url_1 = "http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.18)&_=%d" % int(time.time())
2016-11-07 10:24:36 +08:00
json_data_1 = self.session.post(login_url_1, data=post_data).json()
2016-10-19 17:03:28 +08:00
if json_data_1["retcode"] == "0":
2016-11-07 10:24:36 +08:00
params = {
2016-10-19 17:03:28 +08:00
"callback": "sinaSSOController.callbackLoginStatus",
2016-11-07 10:24:36 +08:00
"client": "ssologin.js(v1.4.18)",
2016-10-19 17:03:28 +08:00
"ticket": json_data_1["ticket"],
"ssosavestate": int(time.time()),
"_": int(time.time()*1000),
}
2016-11-07 10:24:36 +08:00
response = self.session.get("https://passport.weibo.com/wbsso/login", params=params)
json_data_2 = json.loads(re.search(r"\((?P<result>.*)\)", response.text).group("result"))
2016-10-19 17:03:28 +08:00
if json_data_2["result"] is True:
self.user_uniqueid = json_data_2["userinfo"]["uniqueid"]
self.user_nick = json_data_2["userinfo"]["displayname"]
logging.warning("WeiBoLogin succeed: %s", json_data_2)
else:
logging.warning("WeiBoLogin failed: %s", json_data_2)
else:
logging.warning("WeiBoLogin failed: %s", json_data_1)
return True if self.user_uniqueid and self.user_nick else False
def get_username(self):
"""
2016-11-07 10:24:36 +08:00
get legal username
2016-10-19 17:03:28 +08:00
"""
username_quote = urllib.parse.quote_plus(self.user_name)
username_base64 = base64.b64encode(username_quote.encode("utf-8"))
return username_base64.decode("utf-8")
def get_json_data(self, su_value):
"""
get the value of "servertime", "nonce", "pubkey", "rsakv" and "showpin", etc
"""
2016-11-07 10:24:36 +08:00
params = {
2016-10-19 17:03:28 +08:00
"entry": "weibo",
"callback": "sinaSSOController.preloginCallBack",
"rsakt": "mod",
"checkpin": "1",
"client": "ssologin.js(v1.4.18)",
"su": su_value,
"_": int(time.time()*1000),
2016-11-07 10:24:36 +08:00
}
2016-10-19 17:03:28 +08:00
try:
2016-11-07 10:24:36 +08:00
response = self.session.get("http://login.sina.com.cn/sso/prelogin.php", params=params)
json_data = json.loads(re.search(r"\((?P<data>.*)\)", response.text).group("data"))
2016-10-19 17:03:28 +08:00
except Exception as excep:
json_data = {}
logging.error("WeiBoLogin get_json_data error: %s", excep)
logging.debug("WeiBoLogin get_json_data: %s", json_data)
return json_data
def get_password(self, servertime, nonce, pubkey):
"""
2016-11-07 10:24:36 +08:00
get legal password
2016-10-19 17:03:28 +08:00
"""
2016-11-07 10:24:36 +08:00
string = (str(servertime) + "\t" + str(nonce) + "\n" + str(self.pass_word)).encode("utf-8")
2016-10-19 17:03:28 +08:00
public_key = rsa.PublicKey(int(pubkey, 16), int("10001", 16))
password = rsa.encrypt(string, public_key)
password = binascii.b2a_hex(password)
return password.decode()
2016-11-07 10:24:36 +08:00
if __name__ == "__main__":
2016-10-19 17:03:28 +08:00
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s\t%(levelname)s\t%(message)s")
weibo = WeiBoLogin()
weibo.login("username", "password")