Compare commits
3 Commits
96e67e8b04
...
546ca9dfe2
| Author | SHA1 | Date | |
|---|---|---|---|
| 546ca9dfe2 | |||
| 84288fc66b | |||
| 699bac8008 |
@ -2,5 +2,6 @@ CONFIG = {
|
||||
"app_id": "cli_a8de57f7e95f100c",
|
||||
"app_secret": "jm2nkFnnSNVS3n076VOPfh8k7q0hEKVp",
|
||||
"open_id": "ou_bd33a5f75dd2ccb005a21e4dd01f930c",
|
||||
"file_path": "C:\\Users\\22251\\OneDrive\\桌面\\picture\\"
|
||||
"file_path": "C:\\Users\\22251\\OneDrive\\桌面\\picture\\",
|
||||
"messages_reciever": "File Transfer"
|
||||
}
|
||||
31
scheduler.py
31
scheduler.py
@ -3,17 +3,36 @@ import time
|
||||
import subprocess
|
||||
import sys
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
def run_sendmsg():
|
||||
"""执行sendmsg.py脚本"""
|
||||
script_path = os.path.join(os.path.dirname(__file__), 'sendmsg.py')
|
||||
subprocess.run([sys.executable, script_path])
|
||||
try:
|
||||
print(f"[{datetime.now()}] 开始执行每日消息发送任务...")
|
||||
result = subprocess.run([sys.executable, script_path],
|
||||
capture_output=True, text=True, cwd=os.path.dirname(__file__))
|
||||
if result.returncode == 0:
|
||||
print(f"[{datetime.now()}] 任务执行成功")
|
||||
else:
|
||||
print(f"[{datetime.now()}] 任务执行失败: {result.stderr}")
|
||||
except Exception as e:
|
||||
print(f"[{datetime.now()}] 执行出错: {str(e)}")
|
||||
|
||||
# 设置每天12点运行
|
||||
schedule.every().day.at("12:00").do(run_sendmsg)
|
||||
def main():
|
||||
"""主函数 - 设置定时任务"""
|
||||
# 每天17:30执行任务
|
||||
schedule.every().day.at("17:40").do(run_sendmsg)
|
||||
|
||||
print("定时任务已启动,将在每天12:00运行sendmsg.py")
|
||||
print(f"[{datetime.now()}] 定时任务已启动,将在每天17:30执行sendmsg.py")
|
||||
print("按Ctrl+C停止程序")
|
||||
|
||||
# 保持程序运行
|
||||
try:
|
||||
while True:
|
||||
schedule.run_pending()
|
||||
time.sleep(1)
|
||||
time.sleep(60) # 每分钟检查一次
|
||||
except KeyboardInterrupt:
|
||||
print(f"\n[{datetime.now()}] 定时任务已停止")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
26
sendmsg.py
26
sendmsg.py
@ -7,30 +7,32 @@ import json
|
||||
from config import CONFIG
|
||||
import time
|
||||
|
||||
|
||||
def send_daily_message():
|
||||
"""发送每日消息的主函数"""
|
||||
today = date.today()
|
||||
formatted_date = today.strftime('%Y-%m-%d')
|
||||
file_name = formatted_date + ".png"
|
||||
file_path = CONFIG['file_path'] + file_name
|
||||
|
||||
try:
|
||||
wx = WeChat()
|
||||
except:
|
||||
#运行send_openmsg.py
|
||||
subprocess.run([sys.executable, 'send_openmsg.py'])
|
||||
sys.exit(1)
|
||||
|
||||
return False
|
||||
|
||||
msg = 'hello, wxauto!'
|
||||
who = 'File Transfer'
|
||||
who = CONFIG['messages_reciever']
|
||||
|
||||
if os.path.isfile(file_path):
|
||||
error_msg = ""
|
||||
else:
|
||||
error_msg = "没找到指定文件"
|
||||
#运行send_filemsg.py
|
||||
subprocess.run([sys.executable, 'send_filemsg.py'])
|
||||
sys.exit(1)
|
||||
|
||||
print("找到了指定文件!")
|
||||
wx.SendFiles(filepath=file_path, who="File Transfer")
|
||||
wx.SendMsg(msg=msg, who=who)
|
||||
return True
|
||||
else:
|
||||
print("没找到指定文件")
|
||||
subprocess.run([sys.executable, 'send_filemsg.py'])
|
||||
return False
|
||||
|
||||
# 如果直接运行此脚本,执行发送消息功能
|
||||
if __name__ == "__main__":
|
||||
send_daily_message()
|
||||
|
||||
20
service_runner.py
Normal file
20
service_runner.py
Normal file
@ -0,0 +1,20 @@
|
||||
import subprocess
|
||||
import sys
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
def run_in_background():
|
||||
"""在后台运行定时任务"""
|
||||
script_path = Path(__file__).parent / "scheduler.py"
|
||||
|
||||
# 使用pythonw.exe在后台运行(无窗口)
|
||||
pythonw_path = sys.executable.replace('python.exe', 'pythonw.exe')
|
||||
|
||||
subprocess.Popen([pythonw_path, str(script_path)],
|
||||
cwd=str(Path(__file__).parent),
|
||||
creationflags=subprocess.CREATE_NO_WINDOW)
|
||||
|
||||
print("定时任务已在后台启动")
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_in_background()
|
||||
25
stop_scheduler.py
Normal file
25
stop_scheduler.py
Normal file
@ -0,0 +1,25 @@
|
||||
import subprocess
|
||||
import sys
|
||||
import os
|
||||
|
||||
def stop_scheduler():
|
||||
"""停止定时任务进程"""
|
||||
try:
|
||||
# 查找并结束scheduler.py进程
|
||||
result = subprocess.run(['tasklist', '/FI', 'IMAGENAME eq pythonw.exe', '/FO', 'CSV'],
|
||||
capture_output=True, text=True)
|
||||
|
||||
if 'scheduler.py' in result.stdout:
|
||||
# 结束包含scheduler.py的pythonw进程
|
||||
subprocess.run(['taskkill', '/F', '/IM', 'pythonw.exe'], check=True)
|
||||
print("定时任务已停止")
|
||||
else:
|
||||
print("未找到运行中的定时任务")
|
||||
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"停止进程时出错: {e}")
|
||||
except Exception as e:
|
||||
print(f"发生错误: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
stop_scheduler()
|
||||
Loading…
x
Reference in New Issue
Block a user