Skip to content

Commit

Permalink
新增闲时任务功能(UI未适配)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ikaros-521 committed Nov 7, 2023
1 parent 72ca603 commit af87d3e
Show file tree
Hide file tree
Showing 35 changed files with 1,465 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,6 @@ cookie/*.json
# 数据库
data/data.db

webui2.py
webui2.py

data/本地问答库-ikaros.json
153 changes: 153 additions & 0 deletions bilibili.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import random
import asyncio
import traceback
import copy

from functools import partial

Expand All @@ -29,6 +30,8 @@
my_handle = None
# last_liveroom_data = None
last_username_list = None
# 空闲时间计数器
global_idle_time = 0

# 点火起飞
def start_server():
Expand Down Expand Up @@ -194,6 +197,151 @@ async def run_trends_copywriting():
# 创建动态文案子线程并启动
threading.Thread(target=lambda: asyncio.run(run_trends_copywriting())).start()

# 闲时任务
async def idle_time_task():
global config, global_idle_time

try:
if False == config.get("idle_time_task", "enable"):
return

logging.info(f"闲时任务线程运行中...")

# 记录上一次触发的任务类型
last_mode = 0
comment_copy_list = None
local_audio_path_list = None

overflow_time = config.get("idle_time_task", "idle_time")
# 是否开启了随机闲时时间
if config.get("idle_time_task", "random_time"):
overflow_time = random.randint(0, overflow_time)

logging.info(f"overflow_time={overflow_time}")

def load_data_list(type):
if type == "comment":
tmp = config.get("idle_time_task", "comment", "copy")
elif type == "local_audio":
tmp = config.get("idle_time_task", "local_audio", "path")
tmp2 = copy.copy(tmp)
return tmp2

comment_copy_list = load_data_list("comment")
local_audio_path_list = load_data_list("local_audio")

logging.info(f"comment_copy_list={comment_copy_list}")
logging.info(f"local_audio_path_list={local_audio_path_list}")

while True:
# 每隔一秒的睡眠进行闲时计数
await asyncio.sleep(1)
global_idle_time = global_idle_time + 1

# 闲时计数达到指定值,进行闲时任务处理
if global_idle_time >= overflow_time:
# 闲时计数清零
global_idle_time = 0

# 闲时任务处理
if config.get("idle_time_task", "comment", "enable"):
if last_mode == 0 or not config.get("idle_time_task", "local_audio", "enable"):
# 是否开启了随机触发
if config.get("idle_time_task", "comment", "random"):
if comment_copy_list != []:
# 随机打乱列表中的元素
random.shuffle(comment_copy_list)
comment_copy = comment_copy_list.pop(0)
else:
# 刷新list数据
comment_copy_list = load_data_list("comment")
# 随机打乱列表中的元素
random.shuffle(comment_copy_list)
comment_copy = comment_copy_list.pop(0)
else:
if comment_copy_list != []:
comment_copy = comment_copy_list.pop(0)
else:
# 刷新list数据
comment_copy_list = load_data_list("comment")
# 随机打乱列表中的元素
random.shuffle(comment_copy_list)
comment_copy = comment_copy_list.pop(0)

# 发送给处理函数
data = {
"platform": "哔哩哔哩",
"username": "闲时任务",
"type": "comment",
"content": comment_copy
}

my_handle.process_data(data, "idle_time_task")

# 模式切换
last_mode = 1

overflow_time = config.get("idle_time_task", "idle_time")
# 是否开启了随机闲时时间
if config.get("idle_time_task", "random_time"):
overflow_time = random.randint(0, overflow_time)
logging.info(f"overflow_time={overflow_time}")

continue

if config.get("idle_time_task", "local_audio", "enable"):
if last_mode == 1 or not config.get("idle_time_task", "comment", "enable"):
# 是否开启了随机触发
if config.get("idle_time_task", "local_audio", "random"):
if local_audio_path_list != []:
# 随机打乱列表中的元素
random.shuffle(local_audio_path_list)
local_audio_path = local_audio_path_list.pop(0)
else:
# 刷新list数据
local_audio_path_list = load_data_list("local_audio")
# 随机打乱列表中的元素
random.shuffle(local_audio_path_list)
local_audio_path = local_audio_path_list.pop(0)
else:
if local_audio_path_list != []:
local_audio_path = local_audio_path_list.pop(0)
else:
# 刷新list数据
local_audio_path_list = load_data_list("local_audio")
# 随机打乱列表中的元素
random.shuffle(local_audio_path_list)
local_audio_path = local_audio_path_list.pop(0)

# 发送给处理函数
data = {
"platform": "哔哩哔哩",
"username": "闲时任务",
"type": "local_audio",
"content": local_audio_path,
"file_path": local_audio_path
}

my_handle.process_data(data, "idle_time_task")

# 模式切换
last_mode = 0

overflow_time = config.get("idle_time_task", "idle_time")
# 是否开启了随机闲时时间
if config.get("idle_time_task", "random_time"):
overflow_time = random.randint(0, overflow_time)
logging.info(f"overflow_time={overflow_time}")

continue

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

# 创建闲时任务子线程并启动
threading.Thread(target=lambda: asyncio.run(idle_time_task())).start()


try:
if config.get("bilibili", "login_type") == "cookie":
logging.info("b站登录后F12抓网络包获取cookie,强烈建议使用小号!有封号风险")
Expand Down Expand Up @@ -267,6 +415,11 @@ async def _(event):
处理直播间弹幕事件
:param event: 弹幕事件数据
"""
global global_idle_time

# 闲时计数清零
global_idle_time = 0

content = event["data"]["info"][1] # 获取弹幕内容
user_name = event["data"]["info"][2][1] # 获取发送弹幕的用户昵称

Expand Down
Loading

0 comments on commit af87d3e

Please sign in to comment.