Compare commits
No commits in common. "546ca9dfe2d8e13a233b2d36bcfc344ac7fce978" and "96e67e8b045e6fb40339d17119b613497448339b" have entirely different histories.
546ca9dfe2
...
96e67e8b04
@ -2,6 +2,5 @@ CONFIG = {
|
|||||||
"app_id": "cli_a8de57f7e95f100c",
|
"app_id": "cli_a8de57f7e95f100c",
|
||||||
"app_secret": "jm2nkFnnSNVS3n076VOPfh8k7q0hEKVp",
|
"app_secret": "jm2nkFnnSNVS3n076VOPfh8k7q0hEKVp",
|
||||||
"open_id": "ou_bd33a5f75dd2ccb005a21e4dd01f930c",
|
"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,36 +3,17 @@ import time
|
|||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
def run_sendmsg():
|
def run_sendmsg():
|
||||||
"""执行sendmsg.py脚本"""
|
|
||||||
script_path = os.path.join(os.path.dirname(__file__), 'sendmsg.py')
|
script_path = os.path.join(os.path.dirname(__file__), 'sendmsg.py')
|
||||||
try:
|
subprocess.run([sys.executable, script_path])
|
||||||
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)}")
|
|
||||||
|
|
||||||
def main():
|
# 设置每天12点运行
|
||||||
"""主函数 - 设置定时任务"""
|
schedule.every().day.at("12:00").do(run_sendmsg)
|
||||||
# 每天17:30执行任务
|
|
||||||
schedule.every().day.at("17:40").do(run_sendmsg)
|
|
||||||
|
|
||||||
print(f"[{datetime.now()}] 定时任务已启动,将在每天17:30执行sendmsg.py")
|
print("定时任务已启动,将在每天12:00运行sendmsg.py")
|
||||||
print("按Ctrl+C停止程序")
|
|
||||||
|
|
||||||
try:
|
# 保持程序运行
|
||||||
while True:
|
while True:
|
||||||
schedule.run_pending()
|
schedule.run_pending()
|
||||||
time.sleep(60) # 每分钟检查一次
|
time.sleep(1)
|
||||||
except KeyboardInterrupt:
|
|
||||||
print(f"\n[{datetime.now()}] 定时任务已停止")
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
26
sendmsg.py
26
sendmsg.py
@ -7,32 +7,30 @@ import json
|
|||||||
from config import CONFIG
|
from config import CONFIG
|
||||||
import time
|
import time
|
||||||
|
|
||||||
def send_daily_message():
|
|
||||||
"""发送每日消息的主函数"""
|
|
||||||
today = date.today()
|
today = date.today()
|
||||||
formatted_date = today.strftime('%Y-%m-%d')
|
formatted_date = today.strftime('%Y-%m-%d')
|
||||||
file_name = formatted_date+".png"
|
file_name = formatted_date+".png"
|
||||||
file_path = CONFIG['file_path']+file_name
|
file_path = CONFIG['file_path']+file_name
|
||||||
|
|
||||||
try:
|
try:
|
||||||
wx = WeChat()
|
wx = WeChat()
|
||||||
except:
|
except:
|
||||||
|
#运行send_openmsg.py
|
||||||
subprocess.run([sys.executable, 'send_openmsg.py'])
|
subprocess.run([sys.executable, 'send_openmsg.py'])
|
||||||
return False
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
msg = 'hello, wxauto!'
|
msg = 'hello, wxauto!'
|
||||||
who = CONFIG['messages_reciever']
|
who = 'File Transfer'
|
||||||
|
|
||||||
if os.path.isfile(file_path):
|
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("找到了指定文件!")
|
print("找到了指定文件!")
|
||||||
wx.SendFiles(filepath=file_path, who="File Transfer")
|
wx.SendFiles(filepath=file_path, who="File Transfer")
|
||||||
wx.SendMsg(msg=msg, who=who)
|
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()
|
|
||||||
|
|||||||
@ -1,20 +0,0 @@
|
|||||||
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()
|
|
||||||
@ -1,25 +0,0 @@
|
|||||||
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