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

Commit

Permalink
Change login interface
Browse files Browse the repository at this point in the history
tv 接口好像不行了,换回 v3 (fix #322 )
  • Loading branch information
yawwwwwn committed Jul 25, 2020
1 parent bc93327 commit cdbd720
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 28 deletions.
2 changes: 1 addition & 1 deletion bilibili.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ async def request_fetchmedal(self):
return response

def request_getkey(self):
url = 'https://passport.snm0516.aisee.tv/api/oauth2/getKey'
url = 'https://passport.bilibili.com/api/oauth2/getKey'
temp_params = 'appkey=' + self.dic_bilibili['appkey']
sign = self.calc_sign(temp_params)
params = {'appkey': self.dic_bilibili['appkey'], 'sign': sign}
Expand Down
99 changes: 72 additions & 27 deletions login.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,52 @@
import base64
import configloader
import requests
import time


# temporary app parameter
appkey = '4409e2ce8ffd12b8'
build = '101800'
# appkey = '4409e2ce8ffd12b8'
# build = '101800'
# device = 'android_tv_yst'
mobi_app = 'android_tv_yst'
app_secret = '59b43e04ad6965f34319062b478f83dd'
# mobi_app = 'android_tv_yst'
# app_secret = '59b43e04ad6965f34319062b478f83dd'

app_headers = {
"User-Agent": "Mozilla/5.0 BiliDroid/5.58.0 (bbcallen@gmail.com)",
"Accept-encoding": "gzip",
"Buvid": "XZ11bfe2654a9a42d885520a680b3574582eb3",
"Display-ID": "146771405-1521008435",
"Device-Guid": "2d0bbec5-df49-43c5-8a27-ceba3f74ffd7",
"Device-Id": "469a6aaf431b46f8b58a1d4a91d0d95b202004211125026456adffe85ddcb44818",
"Accept-Language": "zh-CN",
"Accept": "text/html,application/xhtml+xml,*/*;q=0.8",
"Connection": "keep-alive",
'cookie': ''
}

class login():
auto_captcha_times = 3

def normal_login(self, username, password):
# url = 'https://passport.bilibili.com/api/oauth2/login' //旧接口
url = "https://passport.snm0516.aisee.tv/api/tv/login"
temp_params = f"appkey={appkey}&build={build}&captcha=&channel=master&guid=XYEBAA3E54D502E17BD606F0589A356902FCF&mobi_app={mobi_app}&password={password}&platform={bilibili().dic_bilibili['platform']}&token=5598158bcd8511e1&ts=0&username={username}"
data = f"{temp_params}&sign={bilibili().calc_sign(temp_params, app_secret)}"
headers = {"Content-type": "application/x-www-form-urlencoded"}
response = requests.post(url, data=data, headers=headers)
# url = "https://passport.bilibili.com/api/v2/oauth2/login"
url = "https://passport.bilibili.com/api/v3/oauth2/login"
params_dic = {
"actionKey": bilibili().dic_bilibili["actionKey"],
"appkey": bilibili().dic_bilibili["appkey"],
"build": bilibili().dic_bilibili["build"],
"captcha": '',
"device": bilibili().dic_bilibili["device"],
"mobi_app": bilibili().dic_bilibili["mobi_app"],
"password": password,
"platform": bilibili().dic_bilibili["platform"],
"username": username
}
temp_params = '&'.join([f'{key}={value}' for key, value in params_dic.items()])
sign = bilibili().calc_sign(temp_params)
# headers = {"Content-type": "application/x-www-form-urlencoded"}
payload = f'{temp_params}&sign={sign}'
response = requests.post(url, params=payload, headers=app_headers)
return response

def login_with_captcha(self, username, password):
Expand All @@ -30,7 +57,7 @@ def login_with_captcha(self, username, password):
'cookie': "sid=hxt5szbb"
}
s = requests.session()
url = "https://passport.snm0516.aisee.tv/api/captcha?token=5598158bcd8511e1"
url = "https://passport.bilibili.com/captcha"
res = s.get(url, headers=headers)
tmp1 = base64.b64encode(res.content)
for _ in range(login.auto_captcha_times):
Expand All @@ -51,19 +78,31 @@ def login_with_captcha(self, username, password):
Printer().printer("安装 Pillow 库后重启,以弹出验证码图片", "Error", "red")
exit()

temp_params = f"appkey={appkey}&build={build}&captcha={captcha}&channel=master&guid=XYEBAA3E54D502E17BD606F0589A356902FCF&mobi_app={mobi_app}&password={password}&platform={bilibili().dic_bilibili['platform']}&token=5598158bcd8511e1&ts=0&username={username}"
data = f"{temp_params}&sign={bilibili().calc_sign(temp_params, app_secret)}"
headers['Content-type'] = "application/x-www-form-urlencoded"
params_dic = {
"actionKey": bilibili().dic_bilibili["actionKey"],
"appkey": bilibili().dic_bilibili["appkey"],
"build": bilibili().dic_bilibili["build"],
"captcha": captcha,
"device": bilibili().dic_bilibili["device"],
"mobi_app": bilibili().dic_bilibili["mobi_app"],
"password": password,
"platform": bilibili().dic_bilibili["platform"],
"username": username
}
temp_params = '&'.join([f'{key}={value}' for key, value in params_dic.items()])
sign = bilibili().calc_sign(temp_params)
payload = f'{temp_params}&sign={sign}'
headers = app_headers.copy()
headers['cookie'] = "sid=hxt5szbb"
url = "https://passport.snm0516.aisee.tv/api/tv/login"
response = s.post(url, data=data, headers=headers)
url = "https://passport.bilibili.com/api/v3/oauth2/login"
response = s.post(url, params=payload, headers=headers)
return response

def access_token_2_cookies(self, access_token):
params = f"access_key={access_token}&appkey={appkey}&gourl=https%3A%2F%2Faccount.bilibili.com%2Faccount%2Fhome"
url = f"https://passport.bilibili.com/api/login/sso?{params}&sign={bilibili().calc_sign(params, app_secret)}"
response = requests.get(url, allow_redirects=False)
return response.cookies.get_dict(domain=".bilibili.com")
# def access_token_2_cookies(self, access_token):
# params = f"access_key={access_token}&appkey={appkey}&gourl=https%3A%2F%2Faccount.bilibili.com%2Faccount%2Fhome"
# url = f"https://passport.bilibili.com/api/login/sso?{params}&sign={bilibili().calc_sign(params, app_secret)}"
# response = requests.get(url, allow_redirects=False)
# return response.cookies.get_dict(domain=".bilibili.com")

def login(self):
username = str(bilibili().dic_bilibili['account']['username'])
Expand All @@ -81,24 +120,30 @@ def login(self):
if response.json()['code'] == -662: # "can't decrypt rsa password~"
Printer().printer("打码时间太长key失效,重试", "Error", "red")
continue
if response.json()['code'] == -449:
# {'code': -449, 'message': '服务繁忙, 请稍后再试', 'ts': 1593853665}
Printer().printer("服务繁忙,10分钟后重试", "Error", "red")
Printer().printer(f"疑似登录接口失效,请联系开发者 {response.json()}", "Warning", "red")
time.sleep(600)
continue
break
try:
access_key = response.json()['data']['token_info']['access_token']
cookie_info = self.access_token_2_cookies(access_key)
cookie = response.json()['data']['cookie_info']['cookies']
cookie_format = ""
for key, value in cookie_info.items():
cookie_format = cookie_format + key + "=" + value + ";"
bilibili().dic_bilibili['csrf'] = cookie_info['bili_jct']
for i in range(len(cookie)):
cookie_format = cookie_format + cookie[i]['name'] + "=" + cookie[i]['value'] + ";"
bilibili().dic_bilibili['csrf'] = cookie[0]['value']
bilibili().dic_bilibili['access_key'] = access_key
bilibili().dic_bilibili['cookie'] = cookie_format
bilibili().dic_bilibili['uid'] = int(cookie_info['DedeUserID'])
bilibili().dic_bilibili['uid'] = cookie[1]['value']
bilibili().dic_bilibili['pcheaders']['cookie'] = cookie_format
bilibili().dic_bilibili['appheaders']['cookie'] = cookie_format
dic_saved_session = {
'csrf': cookie_info['bili_jct'],
'csrf': cookie[0]['value'],
'access_key': access_key,
'cookie': cookie_format,
'uid': cookie_info['DedeUserID']
'uid': cookie[1]['value']
}
configloader.write2bilibili(dic_saved_session)
Printer().printer(f"登录成功", "Info", "green")
Expand Down

0 comments on commit cdbd720

Please sign in to comment.