feat: 更新配置并优化消息发送逻辑
- 修改配置中的接收者名称和发送时间 - 在scheduler.py中添加周末不执行任务的逻辑 - 在sendmsg.py中支持多种图片格式并延长重试间隔
This commit is contained in:
parent
aaf5f59b59
commit
33aca60f56
@ -3,8 +3,8 @@ CONFIG = {
|
|||||||
"app_secret": "mcK8aTiq0CLtkzGs2aTZpcnom5J4o6yB",
|
"app_secret": "mcK8aTiq0CLtkzGs2aTZpcnom5J4o6yB",
|
||||||
"open_id": ["ou_c6466a45623096cf7a34d94fe30c6c73", "ou_3b94d0caf83dbced8b0e26af4852a281"],
|
"open_id": ["ou_c6466a45623096cf7a34d94fe30c6c73", "ou_3b94d0caf83dbced8b0e26af4852a281"],
|
||||||
"file_path": "F:\\",
|
"file_path": "F:\\",
|
||||||
"messages_reciever": "文件传输助手",
|
"messages_reciever": "宝库学堂:AI全能增长官特训营 2期",
|
||||||
"checking_time": "15:24",
|
"checking_time": "16:55",
|
||||||
"sending_time": "15:26",
|
"sending_time": "16:56",
|
||||||
"message": "新的一天,从华智长盈每日AI新闻开始!让我们一起看看今天AI圈有啥新鲜事!"
|
"message": "新的一天,从华智长盈每日AI新闻开始!让我们一起看看今天AI圈有啥新鲜事!"
|
||||||
}
|
}
|
||||||
12
scheduler.py
12
scheduler.py
@ -4,10 +4,19 @@ import subprocess
|
|||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
import calendar
|
||||||
from config import CONFIG
|
from config import CONFIG
|
||||||
|
|
||||||
def run_sendmsg():
|
def run_sendmsg():
|
||||||
"""执行sendmsg.py脚本"""
|
"""执行sendmsg.py脚本,周末不执行"""
|
||||||
|
# 获取当前日期是星期几(0是星期一,6是星期日)
|
||||||
|
current_weekday = datetime.now().weekday()
|
||||||
|
|
||||||
|
# 如果是周末(周六或周日),不执行任务
|
||||||
|
if current_weekday >= 5: # 5是周六,6是周日
|
||||||
|
print(f"[{datetime.now()}] 今天是周末,不执行任务")
|
||||||
|
return
|
||||||
|
|
||||||
script_path = os.path.join(os.path.dirname(__file__), 'sendmsg.py')
|
script_path = os.path.join(os.path.dirname(__file__), 'sendmsg.py')
|
||||||
try:
|
try:
|
||||||
print(f"[{datetime.now()}] 开始执行每日消息发送任务...")
|
print(f"[{datetime.now()}] 开始执行每日消息发送任务...")
|
||||||
@ -22,7 +31,6 @@ def run_sendmsg():
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""主函数 - 设置定时任务"""
|
"""主函数 - 设置定时任务"""
|
||||||
# 每天07:55执行任务
|
|
||||||
schedule.every().day.at(CONFIG['checking_time']).do(run_sendmsg)
|
schedule.every().day.at(CONFIG['checking_time']).do(run_sendmsg)
|
||||||
|
|
||||||
print(f"[{datetime.now()}] 定时任务已启动,将在每天07:55执行sendmsg.py")
|
print(f"[{datetime.now()}] 定时任务已启动,将在每天07:55执行sendmsg.py")
|
||||||
|
|||||||
26
sendmsg.py
26
sendmsg.py
@ -32,8 +32,13 @@ def send_daily_message(count):
|
|||||||
"""发送每日消息的主函数"""
|
"""发送每日消息的主函数"""
|
||||||
today = date.today()
|
today = date.today()
|
||||||
formatted_date = today.strftime('%Y-%m-%d')
|
formatted_date = today.strftime('%Y-%m-%d')
|
||||||
file_name = formatted_date + ".jpg"
|
|
||||||
file_path = CONFIG['file_path'] + file_name
|
# 支持多种图片格式:jpg, jpeg, png
|
||||||
|
file_formats = [".jpg", ".jpeg", ".png"]
|
||||||
|
|
||||||
|
# 初始化文件路径变量
|
||||||
|
file_path = None
|
||||||
|
|
||||||
num = count
|
num = count
|
||||||
if num >= 4:
|
if num >= 4:
|
||||||
print("已尝试4次,程序将退出")
|
print("已尝试4次,程序将退出")
|
||||||
@ -44,7 +49,7 @@ def send_daily_message(count):
|
|||||||
except:
|
except:
|
||||||
subprocess.run([sys.executable, 'send_openmsg.py'])
|
subprocess.run([sys.executable, 'send_openmsg.py'])
|
||||||
if num<4:
|
if num<4:
|
||||||
time.sleep(60)
|
time.sleep(1800)
|
||||||
num += 1
|
num += 1
|
||||||
send_daily_message(num)
|
send_daily_message(num)
|
||||||
else:
|
else:
|
||||||
@ -54,9 +59,16 @@ def send_daily_message(count):
|
|||||||
msg = CONFIG['message']
|
msg = CONFIG['message']
|
||||||
who = CONFIG['messages_reciever']
|
who = CONFIG['messages_reciever']
|
||||||
|
|
||||||
if os.path.isfile(file_path):
|
# 依次检查各种格式的文件是否存在
|
||||||
print("找到了指定文件!")
|
for format in file_formats:
|
||||||
|
temp_file_name = formatted_date + format
|
||||||
|
temp_file_path = CONFIG['file_path'] + temp_file_name
|
||||||
|
if os.path.isfile(temp_file_path):
|
||||||
|
file_path = temp_file_path
|
||||||
|
print(f"找到了{format}格式的图片文件!")
|
||||||
|
break
|
||||||
|
|
||||||
|
if file_path:
|
||||||
# 等待到配置的时间再执行发送操作
|
# 等待到配置的时间再执行发送操作
|
||||||
wait_until_time()
|
wait_until_time()
|
||||||
|
|
||||||
@ -66,10 +78,10 @@ def send_daily_message(count):
|
|||||||
print("发送完成!")
|
print("发送完成!")
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
print("没找到指定文件")
|
print("没找到任何支持格式的图片文件(.jpg, .jpeg, .png)")
|
||||||
subprocess.run([sys.executable, 'send_filemsg.py'])
|
subprocess.run([sys.executable, 'send_filemsg.py'])
|
||||||
if num<4:
|
if num<4:
|
||||||
time.sleep(60)
|
time.sleep(1800)
|
||||||
num += 1
|
num += 1
|
||||||
send_daily_message(num)
|
send_daily_message(num)
|
||||||
else:
|
else:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user