Skip to content

Commit

Permalink
Merge pull request #434 from Ikaros-521/owner
Browse files Browse the repository at this point in the history
哔哩哔哩方案2新增开放平台对接,使用官方API进行监听,应该是最稳定方案了;禁用文案的自动播放;gpt新增gpt-4-1106-preview
  • Loading branch information
Ikaros-521 authored Nov 14, 2023
2 parents 130961b + aa51ba2 commit 6e424ce
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 13 deletions.
133 changes: 127 additions & 6 deletions bilibili2.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

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

from utils.common import Common
from utils.config import Config
Expand Down Expand Up @@ -364,19 +365,31 @@ def load_data_list(type):
if config.get("bilibili", "login_type") == "cookie":
bilibili_cookie = config.get("bilibili", "cookie")
SESSDATA = common.parse_cookie_data(bilibili_cookie, "SESSDATA")
elif config.get("bilibili", "login_type") == "open_live":
# 在开放平台申请的开发者密钥 https://open-live.bilibili.com/open-manage
ACCESS_KEY_ID = config.get("bilibili", "open_live", "ACCESS_KEY_ID")
ACCESS_KEY_SECRET = config.get("bilibili", "open_live", "ACCESS_KEY_SECRET")
# 在开放平台创建的项目ID
APP_ID = config.get("bilibili", "open_live", "APP_ID")
# 主播身份码 直播中心获取
ROOM_OWNER_AUTH_CODE = config.get("bilibili", "open_live", "ROOM_OWNER_AUTH_CODE")

except Exception as e:
logging.error(traceback.format_exc())

async def main_func():
global session

init_session()
try:
await run_single_client()
await run_multi_clients()
finally:
await session.close()
if config.get("bilibili", "login_type") == "open_live":
await run_single_client2()
else:
try:
init_session()

await run_single_client()
await run_multi_clients()
finally:
await session.close()


def init_session():
Expand All @@ -386,6 +399,8 @@ def init_session():
cookies['SESSDATA'] = SESSDATA
cookies['SESSDATA']['domain'] = 'bilibili.com'

# logging.info(f"SESSDATA={SESSDATA}")

session = aiohttp.ClientSession()
session.cookie_jar.update_cookies(cookies)

Expand All @@ -411,6 +426,28 @@ async def run_single_client():
finally:
await client.stop_and_close()

async def run_single_client2():
"""
演示监听一个直播间 开放平台
"""
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 = MyHandler2()
client.set_handler(handler)

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

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

async def run_multi_clients():
"""
Expand Down Expand Up @@ -535,6 +572,90 @@ def _on_super_chat(self, client: blivedm.BLiveClient, message: web_models.SuperC

my_handle.process_data(data, "comment")

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

def _on_open_live_danmaku(self, client: blivedm.OpenLiveClient, message: open_models.DanmakuMessage):
global global_idle_time

# 闲时计数清零
global_idle_time = 0

# logging.info(f'[{client.room_id}] {message.uname}:{message.msg}')
content = message.msg # 获取弹幕内容
user_name = message.uname # 获取发送弹幕的用户昵称

logging.info(f"[{user_name}]: {content}")

data = {
"platform": "哔哩哔哩2",
"username": user_name,
"content": content
}

my_handle.process_data(data, "comment")

def _on_open_live_gift(self, client: blivedm.OpenLiveClient, message: open_models.GiftMessage):
gift_name = message.gift_name
user_name = message.uname
# 礼物数量
combo_num = message.gift_num
# 总金额
combo_total_coin = message.price * message.gift_num

logging.info(f"用户:{user_name} 赠送 {combo_num}{gift_name},总计 {combo_total_coin}电池")

data = {
"platform": "哔哩哔哩2",
"gift_name": gift_name,
"username": user_name,
"num": combo_num,
"unit_price": combo_total_coin / combo_num / 1000,
"total_price": combo_total_coin / 1000
}

my_handle.process_data(data, "gift")


def _on_open_live_buy_guard(self, client: blivedm.OpenLiveClient, message: open_models.GuardBuyMessage):
logging.info(f'[{client.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}')

message = message.message
uname = message.uname
price = message.rmb

logging.info(f"用户:{uname} 发送 {price}元 SC:{message}")

data = {
"platform": "哔哩哔哩2",
"gift_name": "SC",
"username": uname,
"num": 1,
"unit_price": price,
"total_price": price,
"content": message
}

my_handle.process_data(data, "gift")

my_handle.process_data(data, "comment")

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

def _on_open_live_like(self, client: blivedm.OpenLiveClient, message: open_models.LikeMessage):
logging.info(f'用户:{message.uname} 点了个赞')



asyncio.run(main_func())


Expand Down
10 changes: 8 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@
"username": "",
"password": "",
"cookie": "",
"ac_time_value": ""
"ac_time_value": "",
"open_live": {
"ACCESS_KEY_ID": "",
"ACCESS_KEY_SECRET": "",
"APP_ID": 0,
"ROOM_OWNER_AUTH_CODE": ""
}
},
"twitch": {
"token": "oauth:xxxx",
Expand Down Expand Up @@ -392,7 +398,7 @@
"audio_interval": 5.0,
"switching_interval": 5.0,
"random_play": true,
"auto_play": true
"auto_play": false
},
"header": {
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.42"
Expand Down
10 changes: 8 additions & 2 deletions config.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@
"username": "",
"password": "",
"cookie": "",
"ac_time_value": ""
"ac_time_value": "",
"open_live": {
"ACCESS_KEY_ID": "",
"ACCESS_KEY_SECRET": "",
"APP_ID": 0,
"ROOM_OWNER_AUTH_CODE": ""
}
},
"twitch": {
"token": "oauth:xxxx",
Expand Down Expand Up @@ -392,7 +398,7 @@
"audio_interval": 5.0,
"switching_interval": 5.0,
"random_play": true,
"auto_play": true
"auto_play": false
},
"header": {
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.42"
Expand Down
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,7 @@ def init_config(self):
"gpt-4-32k",
"gpt-4-32k-0314",
"gpt-4-32k-0613",
"gpt-4-1106-preview",
"text-embedding-ada-002",
"text-davinci-003",
"text-davinci-002",
Expand Down
16 changes: 13 additions & 3 deletions webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,10 @@ def common_textarea_handle(content):
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

config_data["bilibili"]["open_live"]["ACCESS_KEY_ID"] = input_bilibili_open_live_ACCESS_KEY_ID.value
config_data["bilibili"]["open_live"]["ACCESS_KEY_SECRET"] = input_bilibili_open_live_ACCESS_KEY_SECRET.value
config_data["bilibili"]["open_live"]["APP_ID"] = input_bilibili_open_live_APP_ID.value
config_data["bilibili"]["open_live"]["ROOM_OWNER_AUTH_CODE"] = input_bilibili_open_live_ROOM_OWNER_AUTH_CODE.value

# twitch
config_data["twitch"]["token"] = input_twitch_token.value
Expand Down Expand Up @@ -828,15 +831,21 @@ def common_textarea_handle(content):
with ui.row():
select_bilibili_login_type = ui.select(
label='登录方式',
options={'手机扫码': '手机扫码', '手机扫码-终端': '手机扫码-终端', 'cookie': 'cookie', '账号密码登录': '账号密码登录', '不登录': '不登录'},
options={'手机扫码': '手机扫码', '手机扫码-终端': '手机扫码-终端', 'cookie': 'cookie', '账号密码登录': '账号密码登录', 'open_live': '开放平台', '不登录': '不登录'},
value=config.get("bilibili", "login_type")
).style("width:100px")
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;")
with ui.row():
input_bilibili_username = ui.input(label='账号', value=config.get("bilibili", "username"), placeholder='b站账号(建议使用小号)').style("width:300px;")
input_bilibili_password = ui.input(label='密码', value=config.get("bilibili", "password"), placeholder='b站密码(建议使用小号)').style("width:300px;")

with ui.row():
with ui.card().style(card_css):
with ui.row():
input_bilibili_open_live_ACCESS_KEY_ID = ui.input(label='ACCESS_KEY_ID', value=config.get("bilibili", "open_live", "ACCESS_KEY_ID"), placeholder='开放平台ACCESS_KEY_ID').style("width:300px;")
input_bilibili_open_live_ACCESS_KEY_SECRET = ui.input(label='ACCESS_KEY_SECRET', value=config.get("bilibili", "open_live", "ACCESS_KEY_SECRET"), placeholder='开放平台ACCESS_KEY_SECRET').style("width:300px;")
input_bilibili_open_live_APP_ID = ui.input(label='APP_ID', value=config.get("bilibili", "open_live", "APP_ID"), placeholder='开放平台 创作者服务中心 项目ID').style("width:200px;")
input_bilibili_open_live_ROOM_OWNER_AUTH_CODE = ui.input(label='身份码', value=config.get("bilibili", "open_live", "ROOM_OWNER_AUTH_CODE"), placeholder='直播中心用户 身份码').style("width:200px;")
with ui.card().style(card_css):
ui.label('twitch')
with ui.row():
Expand Down Expand Up @@ -1077,6 +1086,7 @@ def common_textarea_handle(content):
"gpt-4-32k",
"gpt-4-32k-0314",
"gpt-4-32k-0613",
"gpt-4-1106-preview",
"text-embedding-ada-002",
"text-davinci-003",
"text-davinci-002",
Expand Down

0 comments on commit 6e424ce

Please sign in to comment.