Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

Master #49

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 57 additions & 27 deletions baidupcsapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,10 @@ def _load_cookies(self):
def _get_token(self):
# Token
ret = self.session.get(
'https://passport.baidu.com/v2/api/?getapi&tpl=mn&apiver=v3&class=login&tt=%s&logintype=dialogLogin&callback=0' % int(time.time())).text.replace('\'', '\"')
foo = json.loads(ret)
logging.debug('token %s' % foo['data']['token'])
'https://passport.baidu.com/v2/api/?getapi&tpl=netdisk'
'&apiver=v3&tt={}&class=login&logintype=basicLogin'.format(int(time.time())))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好久没写python了, 第一次见这种一个字符串拆成两个的写法! 👍

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有的,太长的话可以切成两行

foo = json.loads(ret.content.decode().replace('\'', '"'))
logging.info('token %s' % foo['data']['token'])
return foo['data']['token']

def _get_captcha(self, code_string):
Expand Down Expand Up @@ -255,35 +256,65 @@ def _login(self):
password_rsaed = base64.b64encode(rsa.encrypt(self.password.encode('utf-8'), key))
isCheck=False
while True:
login_data = {'staticpage': 'http://www.baidu.com/cache/user/html/v3Jump.html',
'charset': 'UTF-8',
'token': self.user['token'],
'tpl': 'pp',
'subpro': '',
'apiver': 'v3',
'tt': str(int(time.time())),
'codestring': code_string,
'isPhone': 'false',
'safeflg': '0',
'u': 'https://passport.baidu.com/',
'quick_user': '0',
'logLoginType': 'pc_loginBasic',
'loginmerge': 'true',
'logintype': 'basicLogin',
'username': self.username,
'password': password_rsaed,
'verifycode': captcha,
'mem_pass': 'on',
'rsakey': str(rsakey),
'crypttype': 12,
'ppui_logintime': '50918',
'callback': 'parent.bd__pcbs__oa36qm'}

login_data = {
# 'staticpage': 'http://www.baidu.com/cache/user/html/v3Jump.html',
# 'charset': 'UTF-8',
# 'token': self.user['token'],
# 'tpl': 'pp',
# 'subpro': '',
# 'apiver': 'v3',
# 'tt': str(int(time.time())),
# 'codestring': code_string,
# 'isPhone': 'false',
# 'safeflg': '0',
# 'u': 'https://passport.baidu.com/',
# 'quick_user': '0',
# 'logLoginType': 'pc_loginBasic',
# 'loginmerge': 'true',
# 'logintype': 'basicLogin',
# 'username': self.username,
# 'password': password_rsaed,
# 'verifycode': captcha,
# 'mem_pass': 'on',
# 'rsakey': str(rsakey),
# 'crypttype': 12,
# 'ppui_logintime': '50918',
# 'callback': 'parent.bd__pcbs__oa36qm'
"staticpage": "http://pan.baidu.com/res/static/thirdparty/pass_v3_jump.html",
"charset": "utf-8",
"token": self.user['token'],
"tpl": "netdisk",
"subpro": "",
"apiver": "v3",
"tt": int(time.time()),
"codestring": code_string,
"safeflg": "0",
"u": "http://pan.baidu.com/",
"isPhone": "",
"quick_user": "0",
"logintype": "basicLogin",
"logLoginType": "pc_loginBasic",
"idc": "",
"loginmerge": "true",
"username": self.username,
"password": self.password,
"verifycode": captcha,
"mem_pass": "on",
"rsakey": str(rsakey),
"crypttype": "",
"ppui_logintime": "2602",
"callback": "parent.bd__pcbs__ahhlgk",
}
# if captcha=='':
# del login_data['verifycode']
result = self.session.post(
'https://passport.baidu.com/v2/api/?login', data=login_data)

if self.user['token'].find('the fisrt two args should be string')!=-1:
self._initiate()
break

# 是否需要验证码
if 'err_no=257' in result.content.decode() or 'err_no=6' in result.content.decode():
code_string = re.findall('codeString=(.*?)&', result.content.decode())[0]
Expand Down Expand Up @@ -395,7 +426,6 @@ def _request(self, uri, method=None, url=None, extra_params=None,
else:
response = self.session.get(
api, params=params, verify=False, headers=headers, **kwargs)
# print(response.url)
return response


Expand Down