Skip to content

Commit

Permalink
Merge pull request #722 from Ikaros-521/owner
Browse files Browse the repository at this point in the history
闲时任务 时间改成最大最小值内随机,新增了文案模式,纯进行闲时的文案内容复读
  • Loading branch information
Ikaros-521 committed Mar 24, 2024
2 parents 2602ceb + 4b4d713 commit 62e9e6f
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 45 deletions.
12 changes: 10 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -771,8 +771,16 @@
],
"idle_time_task": {
"enable": false,
"idle_time": 120,
"random_time": false,
"idle_time_min": 60,
"idle_time_max": 120,
"copywriting": {
"enable": false,
"random": true,
"copy": [
"当前时间是{time},这里是新的语句[1|2|3],可以在括号内随机一个数据[4|5|6]",
"请给我简单地讲一个笑话,让我在忙碌的生活中找到轻松和快乐。控制在100字以内"
]
},
"comment": {
"enable": false,
"random": true,
Expand Down
12 changes: 10 additions & 2 deletions config.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -771,8 +771,16 @@
],
"idle_time_task": {
"enable": false,
"idle_time": 120,
"random_time": false,
"idle_time_min": 60,
"idle_time_max": 120,
"copywriting": {
"enable": false,
"random": true,
"copy": [
"当前时间是{time},这里是新的语句[1|2|3],可以在括号内随机一个数据[4|5|6]",
"请给我简单地讲一个笑话,让我在忙碌的生活中找到轻松和快乐。控制在100字以内"
]
},
"comment": {
"enable": false,
"random": true,
Expand Down
131 changes: 106 additions & 25 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def send():
data_json = request.get_json()
logging.info(f"API收到数据:{data_json}")

if data_json["type"] == "reread":
if data_json["type"] in ["reread", "reread_top_priority"]:
my_handle.reread_handle(data_json)
elif data_json["type"] == "comment":
my_handle.process_data(data_json, "comment")
Expand Down Expand Up @@ -665,27 +665,32 @@ async def idle_time_task():

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

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

logging.info(f"闲时时间={overflow_time}")
logging.info(f"下一个闲时任务将在{overflow_time}秒后执行")

def load_data_list(type):
if type == "comment":
if type == "copywriting":
tmp = config.get("idle_time_task", "copywriting", "copy")
elif 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

# 加载数据到list
copywriting_copy_list = load_data_list("copywriting")
comment_copy_list = load_data_list("comment")
local_audio_path_list = load_data_list("local_audio")

logging.debug(f"copywriting_copy_list={copywriting_copy_list}")
logging.debug(f"comment_copy_list={comment_copy_list}")
logging.debug(f"local_audio_path_list={local_audio_path_list}")

Expand All @@ -700,11 +705,90 @@ def load_data_list(type):
global_idle_time = 0

# 闲时任务处理
if config.get("idle_time_task", "copywriting", "enable"):
if last_mode == 0:
# 是否开启了随机触发
if config.get("idle_time_task", "copywriting", "random"):
logging.debug("切换到文案触发模式")
if copywriting_copy_list != []:
# 随机打乱列表中的元素
random.shuffle(copywriting_copy_list)
copywriting_copy = copywriting_copy_list.pop(0)
else:
# 刷新list数据
copywriting_copy_list = load_data_list("copywriting")
# 随机打乱列表中的元素
random.shuffle(copywriting_copy_list)
copywriting_copy = copywriting_copy_list.pop(0)
else:
if copywriting_copy_list != []:
copywriting_copy = copywriting_copy_list.pop(0)
else:
# 刷新list数据
copywriting_copy_list = load_data_list("copywriting")
copywriting_copy = copywriting_copy_list.pop(0)

hour, min = common.get_bj_time(6)

if 0 <= hour and hour < 6:
time = f"凌晨{hour}{min}分"
elif 6 <= hour and hour < 9:
time = f"早晨{hour}{min}分"
elif 9 <= hour and hour < 12:
time = f"上午{hour}{min}分"
elif hour == 12:
time = f"中午{hour}{min}分"
elif 13 <= hour and hour < 18:
time = f"下午{hour - 12}{min}分"
elif 18 <= hour and hour < 20:
time = f"傍晚{hour - 12}{min}分"
elif 20 <= hour and hour < 24:
time = f"晚上{hour - 12}{min}分"

# 动态变量替换
# 假设有多个未知变量,用户可以在此处定义动态变量
variables = {
'time': time,
'user_num': "N",
'last_username': last_username_list[-1],
}

# 有用户数据情况的平台特殊处理
if platform in ["dy", "tiktok"]:
variables['user_num'] = last_liveroom_data["OnlineUserCount"]

# 使用字典进行字符串替换
if any(var in copywriting_copy for var in variables):
copywriting_copy = copywriting_copy.format(**{var: value for var, value in variables.items() if var in copywriting_copy})

# [1|2]括号语法随机获取一个值,返回取值完成后的字符串
copywriting_copy = common.brackets_text_randomize(copywriting_copy)

# 发送给处理函数
data = {
"platform": platform,
"username": "闲时任务-文案模式",
"type": "reread",
"content": copywriting_copy
}

my_handle.process_data(data, "idle_time_task")

# 模式切换
last_mode = 1

overflow_time = random.randint(overflow_time_min, overflow_time_max)
logging.info(f"下一个闲时任务将在{overflow_time}秒后执行")

continue
else:
last_mode = 1

if config.get("idle_time_task", "comment", "enable"):
if last_mode == 0 or not config.get("idle_time_task", "local_audio", "enable"):
if last_mode == 1:
# 是否开启了随机触发
if config.get("idle_time_task", "comment", "random"):
logging.debug("切换到文案触发模式")
logging.debug("切换到弹幕触发LLM模式")
if comment_copy_list != []:
# 随机打乱列表中的元素
random.shuffle(comment_copy_list)
Expand Down Expand Up @@ -762,26 +846,25 @@ def load_data_list(type):
# 发送给处理函数
data = {
"platform": platform,
"username": "闲时任务",
"username": "闲时任务-弹幕触发LLM模式",
"type": "comment",
"content": comment_copy
}

my_handle.process_data(data, "idle_time_task")

# 模式切换
last_mode = 1
last_mode = 2

overflow_time = int(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 = random.randint(overflow_time_min, overflow_time_max)
logging.info(f"下一个闲时任务将在{overflow_time}秒后执行")

continue

else:
last_mode = 2

if config.get("idle_time_task", "local_audio", "enable"):
if last_mode == 1 or (not config.get("idle_time_task", "comment", "enable")):
if last_mode == 2:
logging.debug("切换到本地音频模式")

# 是否开启了随机触发
Expand Down Expand Up @@ -809,7 +892,7 @@ def load_data_list(type):
# 发送给处理函数
data = {
"platform": platform,
"username": "闲时任务",
"username": "闲时任务-本地音频模式",
"type": "local_audio",
"content": common.extract_filename(local_audio_path, False),
"file_path": local_audio_path
Expand All @@ -820,14 +903,12 @@ def load_data_list(type):
# 模式切换
last_mode = 0

overflow_time = int(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 = random.randint(overflow_time_min, overflow_time_max)
logging.info(f"下一个闲时任务将在{overflow_time}秒后执行")

continue

else:
last_mode = 0
except Exception as e:
logging.error(traceback.format_exc())

Expand Down
3 changes: 2 additions & 1 deletion utils/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ def audio_synthesis(self, message):
Audio.message_queue.put(tmp_message)
# 闲时任务
elif message['type'] == "idle_time_task":
if message['content_type'] == "comment":
if message['content_type'] in ["comment", "reread"]:
pass
elif message['content_type'] == "local_audio":
# 拼接json数据,存入队列
Expand Down Expand Up @@ -1004,6 +1004,7 @@ async def only_play_audio(self):
elif self.config.get("visual_body") == "digital_human_video_player":
await self.digital_human_video_player_api(voice_tmp_path)
else:
# 根据播放器类型进行区分
if self.config.get("play_audio", "player") in ["audio_player", "audio_player_v2"]:
if "insert_index" in data_json:
data_json = {
Expand Down
40 changes: 37 additions & 3 deletions utils/my_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ def audio_synthesis_handle(self, data_json):
核心参数:
type目前有
reread_top_priority 最高优先级-复读
comment 弹幕
local_qa_audio 本地问答音频
song 歌曲
Expand Down Expand Up @@ -2373,7 +2374,40 @@ def idle_time_task_handle(self, data):
content = data["content"]
username = data["username"]

if type == "comment":
if type == "reread":
# 输出当前用户发送的弹幕消息
logging.info(f"[{username}]: {content}")

# 弹幕格式检查和特殊字符替换
content = self.comment_check_and_replace(content)
if content is None:
return

# 判断按键映射触发类型
if My_handle.config.get("key_mapping", "type") == "弹幕" or My_handle.config.get("key_mapping", "type") == "弹幕+回复":
# 按键映射 触发后不执行后面的其他功能
if self.key_mapping_handle("弹幕", data):
return

# 判断自定义命令触发类型
if My_handle.config.get("custom_cmd", "type") == "弹幕" or My_handle.config.get("custom_cmd", "type") == "弹幕+回复":
# 自定义命令 触发后不执行后面的其他功能
if self.custom_cmd_handle("弹幕", data):
return

# 音频合成时需要用到的重要数据
message = {
"type": "idle_time_task",
"tts_type": My_handle.config.get("audio_synthesis_type"),
"data": My_handle.config.get(My_handle.config.get("audio_synthesis_type")),
"config": My_handle.config.get("filter"),
"username": username,
"content": content,
"content_type": type
}

self.audio_synthesis_handle(message)
elif type == "comment":
# 记录数据库
if My_handle.config.get("database", "comment_enable"):
insert_data_sql = '''
Expand Down Expand Up @@ -2497,7 +2531,7 @@ def idle_time_task_handle(self, data):
"config": My_handle.config.get("filter"),
"username": username,
"content": resp_content,
"content_type": "comment"
"content_type": type
}


Expand All @@ -2512,7 +2546,7 @@ def idle_time_task_handle(self, data):
"config": My_handle.config.get("filter"),
"username": username,
"content": content,
"content_type": "local_audio",
"content_type": type,
"file_path": os.path.abspath(data["file_path"])
}

Expand Down
Loading

0 comments on commit 62e9e6f

Please sign in to comment.