- 在service_runner.py中添加进程ID记录功能 - 增强stop_scheduler.py以同时处理scheduler.py和sendmsg.py进程 - 修改sendmsg.py增加重试机制 - 更新配置文件路径和执行时间
83 lines
2.5 KiB
Python
83 lines
2.5 KiB
Python
from wxauto import WeChat
|
||
from datetime import date, datetime, time as dt_time
|
||
import sys
|
||
import os
|
||
import subprocess
|
||
import json
|
||
from config import CONFIG
|
||
import time
|
||
|
||
def wait_until_time():
|
||
"""等待到配置的发送时间"""
|
||
now = datetime.now()
|
||
|
||
# 从配置文件中读取发送时间
|
||
send_time_str = CONFIG['sending_time'] # "17:10"
|
||
hour, minute = map(int, send_time_str.split(':'))
|
||
|
||
target_time = now.replace(hour=hour, minute=minute, second=0, microsecond=0)
|
||
|
||
# 如果当前时间已经过了今天的发送时间,则设置为明天的发送时间
|
||
if now >= target_time:
|
||
target_time = target_time.replace(day=target_time.day + 1)
|
||
|
||
wait_seconds = (target_time - now).total_seconds()
|
||
print(f"当前时间: {now.strftime('%Y-%m-%d %H:%M:%S')}")
|
||
print(f"将在 {target_time.strftime('%Y-%m-%d %H:%M:%S')} 执行发送操作")
|
||
print(f"等待时间: {wait_seconds/3600:.2f} 小时")
|
||
|
||
time.sleep(wait_seconds)
|
||
|
||
def send_daily_message(count):
|
||
"""发送每日消息的主函数"""
|
||
today = date.today()
|
||
formatted_date = today.strftime('%Y-%m-%d')
|
||
file_name = formatted_date + ".jpg"
|
||
file_path = CONFIG['file_path'] + file_name
|
||
num = count
|
||
if num >= 4:
|
||
print("已尝试4次,程序将退出")
|
||
sys.exit(0) # 直接终止程序
|
||
|
||
try:
|
||
wx = WeChat()
|
||
except:
|
||
subprocess.run([sys.executable, 'send_openmsg.py'])
|
||
if num<4:
|
||
time.sleep(60)
|
||
num += 1
|
||
send_daily_message(num)
|
||
else:
|
||
print("已尝试4次,程序将退出")
|
||
sys.exit(0) # 直接终止程序
|
||
|
||
msg = CONFIG['message']
|
||
who = CONFIG['messages_reciever']
|
||
|
||
if os.path.isfile(file_path):
|
||
print("找到了指定文件!")
|
||
|
||
# 等待到配置的时间再执行发送操作
|
||
wait_until_time()
|
||
|
||
print("开始发送文件和消息...")
|
||
wx.SendFiles(filepath=file_path, who=who)
|
||
wx.SendMsg(msg=msg, who=who)
|
||
print("发送完成!")
|
||
return True
|
||
else:
|
||
print("没找到指定文件")
|
||
subprocess.run([sys.executable, 'send_filemsg.py'])
|
||
if num<4:
|
||
time.sleep(60)
|
||
num += 1
|
||
send_daily_message(num)
|
||
else:
|
||
print("已尝试4次,程序将退出")
|
||
sys.exit(0) # 直接终止程序
|
||
|
||
# 如果直接运行此脚本,执行发送消息功能
|
||
if __name__ == "__main__":
|
||
count = 0
|
||
send_daily_message(count)
|