Skip to content

Commit

Permalink
b站登录新增2种登录方式
Browse files Browse the repository at this point in the history
  • Loading branch information
Ikaros-521 committed Oct 22, 2023
1 parent ebbd9b0 commit 63e7d69
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 13 deletions.
9 changes: 8 additions & 1 deletion bilibili.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ async def run_trends_copywriting():
)
elif config.get("bilibili", "login_type") == "手机扫码":
credential = login.login_with_qrcode()
elif config.get("bilibili", "login_type") == "手机扫码-终端":
credential = login.login_with_qrcode_term()
elif config.get("bilibili", "login_type") == "账号密码登录":
bilibili_username = config.get("bilibili", "username")
bilibili_password = config.get("bilibili", "password")

credential = login.login_with_password(bilibili_username, bilibili_password)
elif config.get("bilibili", "login_type") == "不登录":
credential = None
else:
Expand Down Expand Up @@ -415,7 +422,7 @@ async def _(event):
except KeyboardInterrupt:
logging.warning('程序被强行退出')
finally:
logging.warning('关闭连接...')
logging.warning('关闭连接...可能是直播间号配置有误或者其他原因导致的')
os._exit(0)


Expand Down
20 changes: 11 additions & 9 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"platform": "twitch",
"room_display_id": "zod0416",
"platform": "talk",
"room_display_id": "278333",
"chat_type": "reread",
"need_lang": "none",
"before_prompt": "请简要回复:",
Expand Down Expand Up @@ -36,7 +36,9 @@
"bilibili": {
"login_type": "手机扫码",
"cookie": "",
"ac_time_value": ""
"ac_time_value": "",
"username": "",
"password": ""
},
"twitch": {
"token": "oauth:xxxx",
Expand Down Expand Up @@ -335,23 +337,23 @@
{
"file_path": "data/copywriting/",
"audio_path": "out/copywriting/",
"continuous_play_num": 2,
"max_play_time": 10.0,
"play_list": [
"test.wav",
"伊卡日语介绍.wav",
"吐槽.wav"
]
],
"continuous_play_num": 2,
"max_play_time": 10.0
},
{
"file_path": "data/copywriting2/",
"audio_path": "out/copywriting2/",
"continuous_play_num": 2,
"max_play_time": 3.0,
"play_list": [
"test.wav",
"test2.wav"
]
],
"continuous_play_num": 2,
"max_play_time": 3.0
}
],
"audio_interval": 5.0,
Expand Down
2 changes: 2 additions & 0 deletions config.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
},
"bilibili": {
"login_type": "手机扫码",
"username": "",
"password": "",
"cookie": "",
"ac_time_value": ""
},
Expand Down
24 changes: 22 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1934,7 +1934,7 @@ def bilibili_gui_create():
"label_text": "登录方式",
"label_tip": "选择登录b站账号的方式,用于获取b站账号相关信息",
"widget_type": "combo_box",
"combo_data_list": ['手机扫码', 'cookie', '不登录'],
"combo_data_list": ['手机扫码', '手机扫码-终端', 'cookie', '账号密码登录', '不登录'],
"data": bilibili_config["login_type"],
"main_obj_name": "bilibili",
"index": 1
Expand All @@ -1959,6 +1959,24 @@ def bilibili_gui_create():
}
data_json.append(tmp_json)

tmp_json = {
"label_text": "用户名",
"label_tip": "b站账号(建议使用小号)",
"data": bilibili_config["username"],
"main_obj_name": "bilibili",
"index": 2
}
data_json.append(tmp_json)

tmp_json = {
"label_text": "密码",
"label_tip": "b站密码(建议使用小号)",
"data": bilibili_config["password"],
"main_obj_name": "bilibili",
"index": 3
}
data_json.append(tmp_json)

widgets = self.create_widgets_from_json(data_json)

# 动态添加widget到对应的gridLayout
Expand Down Expand Up @@ -3275,7 +3293,9 @@ def reorganize_copywriting_config_data(copywriting_config_data):
bilibili_keys_mapping = {
"login_type": 0,
"cookie": 1,
"ac_time_value": 2
"ac_time_value": 2,
"username": 3,
"password": 4
}

# 重组bilibili数据并写回json
Expand Down
76 changes: 76 additions & 0 deletions tests/test_blivedm/2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# -*- coding: utf-8 -*-
import asyncio

import blivedm
import blivedm.models.open_live as open_models
import blivedm.models.web as web_models

# 在开放平台申请的开发者密钥
ACCESS_KEY_ID = ''
ACCESS_KEY_SECRET = ''
# 在开放平台创建的项目ID
APP_ID = 0
# 主播身份码
ROOM_OWNER_AUTH_CODE = ''


async def main():
await run_single_client()


async def run_single_client():
"""
演示监听一个直播间
"""
client = blivedm.OpenLiveClient(
access_key_id=ACCESS_KEY_ID,
access_key_secret=ACCESS_KEY_SECRET,
app_id=APP_ID,
room_owner_auth_code=ROOM_OWNER_AUTH_CODE,
)
handler = MyHandler()
client.set_handler(handler)

client.start()
try:
# 演示70秒后停止
# await asyncio.sleep(70)
# client.stop()

await client.join()
finally:
await client.stop_and_close()


class MyHandler(blivedm.BaseHandler):
def _on_heartbeat(self, client: blivedm.BLiveClient, message: web_models.HeartbeatMessage):
print(f'[{client.room_id}] 心跳')

def _on_open_live_danmaku(self, client: blivedm.OpenLiveClient, message: open_models.DanmakuMessage):
print(f'[{message.room_id}] {message.uname}{message.msg}')

def _on_open_live_gift(self, client: blivedm.OpenLiveClient, message: open_models.GiftMessage):
coin_type = '金瓜子' if message.paid else '银瓜子'
total_coin = message.price * message.gift_num
print(f'[{message.room_id}] {message.uname} 赠送{message.gift_name}x{message.gift_num}'
f' ({coin_type}x{total_coin})')

def _on_open_live_buy_guard(self, client: blivedm.OpenLiveClient, message: open_models.GuardBuyMessage):
print(f'[{message.room_id}] {message.user_info.uname} 购买 大航海等级={message.guard_level}')

def _on_open_live_super_chat(
self, client: blivedm.OpenLiveClient, message: open_models.SuperChatMessage
):
print(f'[{message.room_id}] 醒目留言 ¥{message.rmb} {message.uname}{message.message}')

def _on_open_live_super_chat_delete(
self, client: blivedm.OpenLiveClient, message: open_models.SuperChatDeleteMessage
):
print(f'[{message.room_id}] 删除醒目留言 message_ids={message.message_ids}')

def _on_open_live_like(self, client: blivedm.OpenLiveClient, message: open_models.LikeMessage):
print(f'[{message.room_id}] {message.uname} 点赞')


if __name__ == '__main__':
asyncio.run(main())
8 changes: 7 additions & 1 deletion webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ def common_textarea_handle(content):
config_data["bilibili"]["login_type"] = select_bilibili_login_type.value
config_data["bilibili"]["cookie"] = input_bilibili_cookie.value
config_data["bilibili"]["ac_time_value"] = input_bilibili_ac_time_value.value
config_data["bilibili"]["username"] = input_bilibili_username.value
config_data["bilibili"]["password"] = input_bilibili_password.value


# twitch
config_data["twitch"]["token"] = input_twitch_token.value
Expand Down Expand Up @@ -704,11 +707,14 @@ def textarea_data_change(data):
with ui.row():
select_bilibili_login_type = ui.select(
label='登录方式',
options={'手机扫码': '手机扫码', 'cookie': 'cookie', '不登录': '不登录'},
options={'手机扫码': '手机扫码', '手机扫码-终端': '手机扫码-终端', 'cookie': 'cookie', '账号密码登录': '账号密码登录', '不登录': '不登录'},
value=config.get("bilibili", "login_type")
)
input_bilibili_cookie = ui.input(label='cookie', placeholder='b站登录后F12抓网络包获取cookie,强烈建议使用小号!有封号风险', value=config.get("bilibili", "cookie")).style("width:500px;")
input_bilibili_ac_time_value = ui.input(label='ac_time_value', placeholder='b站登录后,F12控制台,输入window.localStorage.ac_time_value获取(如果没有,请重新登录)', value=config.get("bilibili", "ac_time_value")).style("width:500px;")
input_bilibili_username = ui.input(label='账号', value=config.get("bilibili", "username"), placeholder='b站账号(建议使用小号)').style("width:500px;")
input_bilibili_password = ui.input(label='密码', value=config.get("bilibili", "password"), placeholder='b站密码(建议使用小号)').style("width:500px;")

with ui.card().style("margin:10px 0px"):
ui.label('twitch')
with ui.row():
Expand Down

0 comments on commit 63e7d69

Please sign in to comment.