From aa007d885e59f2c8e6214cb44110cc9148df2a79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=A0=87?= Date: Wed, 6 Aug 2025 15:12:37 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=F0=9F=90=9B:1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scheduler/scheduler_module.py | 33 ++++++++++++++++++--------------- util/database.py | 7 ++++++- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/scheduler/scheduler_module.py b/scheduler/scheduler_module.py index 92e1fd2..7912511 100644 --- a/scheduler/scheduler_module.py +++ b/scheduler/scheduler_module.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- import asyncio import datetime import json @@ -108,14 +110,15 @@ async def poll_external_api(): logger.info(f"状态码: {response.status_code}") logger.info(f"响应文本: {response.text}") response.raise_for_status() - logger.info("文件发送成功!") + logger.info(f"文件发送成功!{name}") update_sent_status(id,name,1) + await asyncio.sleep(15) except httpx.HTTPStatusError as e: - logger.info(f"调用外部接口 {push_message_url} 时发生 HTTP 状态码错误: {e.response.status_code} - {e.response.text}") + logger.error(f"调用外部接口 {push_message_url} 时发生 HTTP 状态码错误: {e.response.status_code} - {e.response.text}") except httpx.RequestError as e: - logger.info(f"调用外部接口 {push_message_url} 时发生请求错误: {e}") + logger.error(f"调用外部接口 {push_message_url} 时发生请求错误: {e}") except Exception as e: - logger.info(f"调用外部接口 {push_message_url} 时发生未知错误: {e}") + logger.error(f"调用外部接口 {push_message_url} 时发生未知错误: {e}") logger.info(f"--- 定时任务外部接口调用结束 ---") @@ -132,15 +135,15 @@ def request_report(base_url,params): response.raise_for_status() # 检查响应的 Content-Type,确保它是 JSON data = response.json() - print("\n请求成功,返回数据:") - print(json.dumps(data, indent=4, ensure_ascii=False)) + logger.info("\n请求成功,返回数据:") + logger.info(json.dumps(data, indent=4, ensure_ascii=False)) return data except httpx.RequestError as exc: - print(f"\n请求失败,发生错误: {exc}") + logger.error(f"\n请求失败,发生错误: {exc}") return None except httpx.HTTPStatusError as exc: - print(f"\n请求失败,HTTP 错误状态码: {exc.response.status_code}") - print(f"响应内容: {exc.response.text}") + logger.error(f"\n请求失败,HTTP 错误状态码: {exc.response.status_code}") + logger.error(f"响应内容: {exc.response.text}") return None def download_file(url, download_dir_all,filename,file_id): @@ -150,7 +153,7 @@ def download_file(url, download_dir_all,filename,file_id): # 确保保存目录存在,如果不存在则创建 if not os.path.exists(download_dir_all): os.makedirs(download_dir_all) - print(f"开始从 {url} 下载文件...") + logger.info(f"开始从 {url} 下载文件...") params = {"id": file_id} try: # 使用流式方式(stream=True)下载 @@ -159,17 +162,17 @@ def download_file(url, download_dir_all,filename,file_id): response.raise_for_status() # 构造完整的文件路径 file_path = os.path.join(download_dir_all, filename+".docx") - print(f"文件名从响应头中获取为: {filename}") + logger.info(f"文件名从响应头中获取为: {filename}") # 将文件内容写入本地文件 with open(file_path, "wb") as f: for chunk in response.iter_bytes(): f.write(chunk) - print(f"文件成功下载并保存到: {file_path}") + logger.info(f"文件成功下载并保存到: {file_path}") return file_path except httpx.HTTPStatusError as e: - print(f"下载文件时发生HTTP错误: {e}") + logger.error(f"下载文件时发生HTTP错误: {e}") except httpx.RequestError as e: - print(f"下载文件时发生请求错误: {e}") + logger.error(f"下载文件时发生请求错误: {e}") def start_scheduler(): @@ -178,7 +181,7 @@ def start_scheduler(): """ logger.info("调度器启动中...") # 添加定时任务:每隔1分钟执行一次 poll_push_message_endpoint 函数 - scheduler.add_job(poll_external_api, 'interval', minutes=1) + scheduler.add_job(poll_external_api, 'interval', minutes=3) scheduler.start() logger.info("调度器已启动。") diff --git a/util/database.py b/util/database.py index b41f4c4..575ceeb 100644 --- a/util/database.py +++ b/util/database.py @@ -69,7 +69,12 @@ def get_latest_update_time(): return result else: # 如果没有数据,返回当前时间 - return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + # 插入一条数据 + messages_to_insert = [] + current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + messages_to_insert.append((0,"0", current_time, 0,"","0")) + insert_many_messages(messages_to_insert) + return def insert_many_messages(messages_to_insert):