Skip to content

Commit

Permalink
Merge pull request #721 from Ikaros-521/owner
Browse files Browse the repository at this point in the history
定时任务、闲时任务、动态文案,支持新语法[1|2|3],括号中随机一个结果做拼接;闲时任务加了time动态变量
  • Loading branch information
Ikaros-521 committed Mar 24, 2024
2 parents b11054d + 674f0d7 commit 2602ceb
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
43 changes: 41 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,6 @@ def key_listener():
thread.start()



# 定时任务
def schedule_task(index):
global config, common, my_handle, last_liveroom_data, last_username_list
Expand Down Expand Up @@ -541,6 +540,8 @@ def schedule_task(index):
else:
content = random_copy

content = common.brackets_text_randomize(content)

data = {
"platform": platform,
"username": None,
Expand Down Expand Up @@ -630,6 +631,8 @@ async def run_trends_copywriting():
# 调用函数进行LLM处理,以及生成回复内容,进行音频合成,需要好好考虑考虑实现
data_json["content"] = my_handle.llm_handle(config.get("trends_copywriting", "llm_type"), data_json)
else:
copywriting_file_content = common.brackets_text_randomize(copywriting_file_content)

data_json = {
"username": "trends_copywriting",
"content": copywriting_file_content
Expand All @@ -652,7 +655,7 @@ async def run_trends_copywriting():

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

try:
if False == config.get("idle_time_task", "enable"):
Expand Down Expand Up @@ -720,6 +723,42 @@ def load_data_list(type):
comment_copy_list = load_data_list("comment")
comment_copy = comment_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 comment_copy for var in variables):
comment_copy = comment_copy.format(**{var: value for var, value in variables.items() if var in comment_copy})

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

# 发送给处理函数
data = {
"platform": platform,
Expand Down
23 changes: 23 additions & 0 deletions utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,29 @@ def dynamic_variable_replacement(self, template, data_json):
return template


# [1|2]括号语法随机获取一个值,返回取值完成后的字符串
def brackets_text_randomize(self, text: str):
"""
[1|2]括号语法随机获取一个值,返回取值完成后的字符串
Args:
text (str): 原始字符串
Returns:
str: 最终字符串
"""
# 查找所有括号内的内容
brackets_content = re.findall(r'\[([^\]]*)\]', text)

for content in brackets_content:
# 分割每个括号内的选项
choices = content.split('|')
# 从选项中随机选择一个
random_choice = random.choice(choices)
# 替换文本中的括号内容
text = text.replace(f'[{content}]', random_choice, 1)

return text

"""
.@@@ @@@ @@^ =@@@@@@@@ /@@ /@@ =@@@@@*,@@\]]]] ,@@@@@@@@@@@@* .@@@ @@/.\]`@@@ =@@\]]]]]]] =@@..@@@@@@@@@ =@@\ /@@^
Expand Down

0 comments on commit 2602ceb

Please sign in to comment.