Skip to content

Commit

Permalink
Merge pull request #278 from Ikaros-521/owner
Browse files Browse the repository at this point in the history
更新b站监听库到16.0.0版本,新增b站登录板块,获取cookie等信息解决弹幕监听丢失问题
  • Loading branch information
Ikaros-521 authored Sep 2, 2023
2 parents 1a7e3a7 + 1483de9 commit 38c046d
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ AI Vtuber是一个由 ChatterBot/GPT/Claude/langchain本地or云端/chatglm/text
![思维导图](./docs/xmind.png)




## 🕺🏻目录
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
Expand Down Expand Up @@ -1577,6 +1575,8 @@ cmd运行`npm i docsify-cli -g`
- 入场和礼物感谢部分删除用户名中存在的特殊符号
- 抖音关注事件可以触发关注感谢话术
- 基本实现动态文案功能,待后续使用体验中完善
- 更新b站监听库到16.0.0版本,新增b站登录板块,获取cookie等信息解决弹幕监听丢失问题(强烈建议使用小号登录,有风险)


</details>

Expand Down
13 changes: 11 additions & 2 deletions UI_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def setupUi(self, MainWindow):
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, -2153, 984, 9894))
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 984, 9957))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.verticalLayout = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents)
self.verticalLayout.setContentsMargins(35, 20, 35, 20)
Expand Down Expand Up @@ -695,6 +695,14 @@ def setupUi(self, MainWindow):
self.gridLayout_play_audio.setObjectName("gridLayout_play_audio")
self.gridLayout_68.addLayout(self.gridLayout_play_audio, 0, 0, 1, 1)
self.gridLayout.addWidget(self.groupBox_play_audio, 13, 0, 1, 2)
self.groupBox_bilibili = QtWidgets.QGroupBox(self.formWidget)
self.groupBox_bilibili.setObjectName("groupBox_bilibili")
self.gridLayout_72 = QtWidgets.QGridLayout(self.groupBox_bilibili)
self.gridLayout_72.setObjectName("gridLayout_72")
self.gridLayout_bilibili = QtWidgets.QGridLayout()
self.gridLayout_bilibili.setObjectName("gridLayout_bilibili")
self.gridLayout_72.addLayout(self.gridLayout_bilibili, 0, 0, 1, 1)
self.gridLayout.addWidget(self.groupBox_bilibili, 11, 0, 1, 2)
self.verticalLayout.addWidget(self.formWidget)
self.groupBox_zhipu = QtWidgets.QGroupBox(self.scrollAreaWidgetContents)
self.groupBox_zhipu.setObjectName("groupBox_zhipu")
Expand Down Expand Up @@ -1612,7 +1620,7 @@ def setupUi(self, MainWindow):
self.scrollArea_copywriting_config.setWidgetResizable(True)
self.scrollArea_copywriting_config.setObjectName("scrollArea_copywriting_config")
self.scrollAreaWidgetContents_4 = QtWidgets.QWidget()
self.scrollAreaWidgetContents_4.setGeometry(QtCore.QRect(0, 0, 652, 498))
self.scrollAreaWidgetContents_4.setGeometry(QtCore.QRect(0, 0, 359, 104))
self.scrollAreaWidgetContents_4.setObjectName("scrollAreaWidgetContents_4")
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents_4)
self.verticalLayout_4.setObjectName("verticalLayout_4")
Expand Down Expand Up @@ -2028,6 +2036,7 @@ def retranslateUi(self, MainWindow):
self.label_audio_random_speed_copywriting_speed_max.setText(_translate("MainWindow", "速度上限"))
self.label_before_prompt.setText(_translate("MainWindow", "提示词前缀"))
self.groupBox_play_audio.setTitle(_translate("MainWindow", "音频播放"))
self.groupBox_bilibili.setTitle(_translate("MainWindow", "哔哩哔哩"))
self.groupBox_zhipu.setTitle(_translate("MainWindow", "智谱AI"))
self.groupBox_langchain_chatglm.setTitle(_translate("MainWindow", "Langchain_ChatGLM"))
self.label_langchain_chatglm_api_ip_port.setText(_translate("MainWindow", "api地址"))
Expand Down
32 changes: 29 additions & 3 deletions bilibili.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from functools import partial

# 导入所需的库
from bilibili_api import live, sync
from bilibili_api import Credential, live, sync, login

from utils.common import Common
from utils.config import Config
Expand Down Expand Up @@ -188,9 +188,35 @@ async def run_trends_copywriting():
# 创建动态文案子线程并启动
threading.Thread(target=lambda: asyncio.run(run_trends_copywriting())).start()

try:
if config.get("bilibili", "login_type") == "cookie":
bilibili_cookie = config.get("bilibili", "cookie")
bilibili_ac_time_value = config.get("bilibili", "ac_time_value")
if bilibili_ac_time_value == "":
bilibili_ac_time_value = None

# print(f'SESSDATA={common.parse_cookie_data(bilibili_cookie, "SESSDATA")}')
# print(f'bili_jct={common.parse_cookie_data(bilibili_cookie, "bili_jct")}')
# print(f'buvid3={common.parse_cookie_data(bilibili_cookie, "buvid3")}')
# print(f'DedeUserID={common.parse_cookie_data(bilibili_cookie, "DedeUserID")}')

# 生成一个 Credential 对象
credential = Credential(
sessdata=common.parse_cookie_data(bilibili_cookie, "SESSDATA"),
bili_jct=common.parse_cookie_data(bilibili_cookie, "bili_jct"),
buvid3=common.parse_cookie_data(bilibili_cookie, "buvid3"),
dedeuserid=common.parse_cookie_data(bilibili_cookie, "DedeUserID"),
ac_time_value=bilibili_ac_time_value
)
elif config.get("bilibili", "login_type") == "手机扫码":
credential = login.login_with_qrcode()
else:
credential = login.login_with_qrcode()

# 初始化 Bilibili 直播间
room = live.LiveDanmaku(my_handle.get_room_id())
# 初始化 Bilibili 直播间
room = live.LiveDanmaku(my_handle.get_room_id(), credential=credential)
except Exception as e:
logging.error(traceback.format_exc())

"""
DANMU_MSG: 用户发送弹幕
Expand Down
9 changes: 7 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
"play_audio": true,
"web_captions_printer": true
},
"bilibili": {
"login_type": "手机扫码",
"cookie": "",
"ac_time_value": ""
},
"read_user_name": {
"enable": true,
"voice_change": true,
Expand Down Expand Up @@ -76,7 +81,7 @@
"openai": {
"api": "https://api.openai.com/v1",
"api_key": [
"修改为你的api key"
"修改为你的api_key"
],
"model": "gpt-3.5-turbo-0301"
},
Expand Down Expand Up @@ -224,7 +229,7 @@
"db_path": "db.sqlite3"
},
"chatgpt": {
"model": "gpt-3.5-turbo",
"model": "gpt-3.5-turbo-0613",
"temperature": 0.9,
"max_tokens": 2048,
"top_p": 1.0,
Expand Down
9 changes: 7 additions & 2 deletions config.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
"play_audio": true,
"web_captions_printer": true
},
"bilibili": {
"login_type": "手机扫码",
"cookie": "",
"ac_time_value": ""
},
"read_user_name": {
"enable": true,
"voice_change": true,
Expand Down Expand Up @@ -76,7 +81,7 @@
"openai": {
"api": "https://api.openai.com/v1",
"api_key": [
"修改为你的api key"
"修改为你的api_key"
],
"model": "gpt-3.5-turbo-0301"
},
Expand Down Expand Up @@ -224,7 +229,7 @@
"db_path": "db.sqlite3"
},
"chatgpt": {
"model": "gpt-3.5-turbo",
"model": "gpt-3.5-turbo-0613",
"temperature": 0.9,
"max_tokens": 2048,
"top_p": 1.0,
Expand Down
64 changes: 64 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1763,6 +1763,52 @@ def vall_e_x_gui_create():

vall_e_x_gui_create()

# 哔哩哔哩
def bilibili_gui_create():
data_json = []
bilibili_config = config.get("bilibili")

tmp_json = {
"label_text": "登录方式",
"label_tip": "选择登录b站账号的方式,用于获取b站账号相关信息",
"widget_type": "combo_box",
"combo_data_list": ['手机扫码', 'cookie'],
"data": bilibili_config["login_type"],
"main_obj_name": "bilibili",
"index": 1
}
data_json.append(tmp_json)

tmp_json = {
"label_text": "API地址",
"label_tip": "b站登录后F12抓网络包获取cookie,强烈建议使用小号!有封号风险",
"data": bilibili_config["cookie"],
"main_obj_name": "bilibili",
"index": 1
}
data_json.append(tmp_json)

tmp_json = {
"label_text": "voice preset",
"label_tip": "b站登录后,F12控制台,输入window.localStorage.ac_time_value获取(如果没有,请重新登录)",
"data": bilibili_config["ac_time_value"],
"main_obj_name": "bilibili",
"index": 1
}
data_json.append(tmp_json)

widgets = self.create_widgets_from_json(data_json)

# 动态添加widget到对应的gridLayout
row = 0
# 分2列,左边就是label说明,右边就是输入框等
for i in range(0, len(widgets), 2):
self.ui.gridLayout_bilibili.addWidget(widgets[i], row, 0)
self.ui.gridLayout_bilibili.addWidget(widgets[i + 1], row, 1)
row += 1

bilibili_gui_create()


"""
↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
Expand Down Expand Up @@ -2521,6 +2567,24 @@ def reorganize_vall_e_x_data(vall_e_x_data):
# 写回json
config_data["vall_e_x"] = reorganize_vall_e_x_data(vall_e_x_data)

# 哔哩哔哩
def reorganize_bilibili_data(bilibili_data):
keys = list(bilibili_data.keys())

tmp_json = {
"login_type": bilibili_data[keys[0]],
"cookie": bilibili_data[keys[1]],
"ac_time_value": bilibili_data[keys[2]]
}

logging.debug(f"tmp_json={tmp_json}")

return tmp_json

bilibili_data = self.update_data_from_gridLayout(self.ui.gridLayout_bilibili)
# 写回json
config_data["bilibili"] = reorganize_bilibili_data(bilibili_data)

"""
↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
-------------------------------------------------------------------------------------------------------------
Expand Down
20 changes: 16 additions & 4 deletions ui/main.ui
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ background-color: rgba(255, 255, 255, 50);
<property name="geometry">
<rect>
<x>0</x>
<y>-2153</y>
<y>0</y>
<width>984</width>
<height>9894</height>
<height>9957</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
Expand Down Expand Up @@ -1349,6 +1349,18 @@ background-color: rgba(255, 255, 255, 50);
</layout>
</widget>
</item>
<item row="11" column="0" colspan="2">
<widget class="QGroupBox" name="groupBox_bilibili">
<property name="title">
<string>哔哩哔哩</string>
</property>
<layout class="QGridLayout" name="gridLayout_72">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_bilibili"/>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down Expand Up @@ -2999,8 +3011,8 @@ border-radius: 3px;</string>
<rect>
<x>0</x>
<y>0</y>
<width>652</width>
<height>498</height>
<width>359</width>
<height>104</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
Expand Down
26 changes: 26 additions & 0 deletions utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,3 +590,29 @@ def replace_special_characters(self, input_string, special_characters):
input_string = input_string.replace(char, "")

return input_string


# 将cookie数据字符串分割成键值对列表
def parse_cookie_data(self, data_str, field_name):
"""将cookie数据字符串分割成键值对列表
Args:
data_str (str): 待提取数据的cookie字符串
field_name (str): 要提取的键名
Returns:
str: 键所对应的值
"""
# 将数据字符串分割成键值对列表
key_value_pairs = data_str.split(';')

# print(key_value_pairs)

# 遍历键值对列表,查找指定字段名
for pair in key_value_pairs:
key, value = pair.strip().split('=')
if key == field_name:
return value

# 如果未找到指定字段,返回空字符串
return ""

0 comments on commit 38c046d

Please sign in to comment.