|
|
|
@ -8,7 +8,7 @@ import httpx
|
|
|
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
|
|
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
|
|
|
|
|
|
|
|
|
|
|
from util.config import DataLoader
|
|
|
|
from util.config import DataLoader
|
|
|
|
from util.database import get_latest_update_time
|
|
|
|
from util.database import get_latest_update_time, insert_many_messages, update_sent_status
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__) # 获取当前模块的logger
|
|
|
|
logger = logging.getLogger(__name__) # 获取当前模块的logger
|
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
@ -56,9 +56,14 @@ async def poll_external_api():
|
|
|
|
logger.info("接口返回错误,请检查响应。")
|
|
|
|
logger.info("接口返回错误,请检查响应。")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
for report in reports_list:
|
|
|
|
for report in reports_list:
|
|
|
|
|
|
|
|
id = report["id"]
|
|
|
|
file_id = report["file_id"]
|
|
|
|
file_id = report["file_id"]
|
|
|
|
download_dir_file_id = download_dir_all+"/"+file_id
|
|
|
|
download_dir_file_id = download_dir_all+"/"+file_id
|
|
|
|
file_path = download_file(download_report_url,download_dir_file_id)
|
|
|
|
file_path = download_file(download_report_url,download_dir_file_id)
|
|
|
|
|
|
|
|
if file_path is not None:
|
|
|
|
|
|
|
|
messages_to_insert =[(id,file_id, current_time, 0)]
|
|
|
|
|
|
|
|
error_message = insert_many_messages(messages_to_insert)
|
|
|
|
|
|
|
|
if error_message is None:
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
# 1,获取需要推送的文档ID 2,根据ID获取文档,3,文档写成文件,4,发送文件,5,删除文件
|
|
|
|
# 1,获取需要推送的文档ID 2,根据ID获取文档,3,文档写成文件,4,发送文件,5,删除文件
|
|
|
|
files = []
|
|
|
|
files = []
|
|
|
|
@ -83,6 +88,8 @@ async def poll_external_api():
|
|
|
|
response.raise_for_status()
|
|
|
|
response.raise_for_status()
|
|
|
|
logger.info("文件发送成功!")
|
|
|
|
logger.info("文件发送成功!")
|
|
|
|
# 都发送成功,则删除文件,回填到数据库
|
|
|
|
# 都发送成功,则删除文件,回填到数据库
|
|
|
|
|
|
|
|
update_sent_status(id)
|
|
|
|
|
|
|
|
os.remove(file_path)
|
|
|
|
except httpx.HTTPStatusError as e:
|
|
|
|
except httpx.HTTPStatusError as e:
|
|
|
|
logger.info(f"调用外部接口 {push_message_url} 时发生 HTTP 状态码错误: {e.response.status_code} - {e.response.text}")
|
|
|
|
logger.info(f"调用外部接口 {push_message_url} 时发生 HTTP 状态码错误: {e.response.status_code} - {e.response.text}")
|
|
|
|
except httpx.RequestError as e:
|
|
|
|
except httpx.RequestError as e:
|
|
|
|
@ -124,9 +131,9 @@ def download_file(url, download_dir_all):
|
|
|
|
if not os.path.exists(download_dir_all):
|
|
|
|
if not os.path.exists(download_dir_all):
|
|
|
|
os.makedirs(download_dir_all)
|
|
|
|
os.makedirs(download_dir_all)
|
|
|
|
print(f"开始从 {url} 下载文件...")
|
|
|
|
print(f"开始从 {url} 下载文件...")
|
|
|
|
|
|
|
|
filename = None
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
# 使用流式方式(stream=True)下载
|
|
|
|
# 使用流式方式(stream=True)下载
|
|
|
|
filename = ""
|
|
|
|
|
|
|
|
with httpx.stream("GET", url, follow_redirects=True) as response:
|
|
|
|
with httpx.stream("GET", url, follow_redirects=True) as response:
|
|
|
|
# 检查响应状态码,如果不是2xx则抛出异常
|
|
|
|
# 检查响应状态码,如果不是2xx则抛出异常
|
|
|
|
response.raise_for_status()
|
|
|
|
response.raise_for_status()
|
|
|
|
|