This commit is contained in:
39
main.py
39
main.py
@@ -215,7 +215,6 @@ def gpt_response_message(content, ist_number=1):
|
|||||||
# else:
|
# else:
|
||||||
# contentGPT = Promts[1].prompt.replace('{content}', content)
|
# contentGPT = Promts[1].prompt.replace('{content}', content)
|
||||||
|
|
||||||
|
|
||||||
if ist_number == 1:
|
if ist_number == 1:
|
||||||
url_ist = "http://epaper.hljnews.cn/hljrb/pc/layout"
|
url_ist = "http://epaper.hljnews.cn/hljrb/pc/layout"
|
||||||
else:
|
else:
|
||||||
@@ -465,11 +464,10 @@ class DownloadRange(BaseModel):
|
|||||||
data_finish: str
|
data_finish: str
|
||||||
|
|
||||||
@app.post("/download_all", summary="Скачать все файлы за период")
|
@app.post("/download_all", summary="Скачать все файлы за период")
|
||||||
async def download_all(dates: DownloadRange):
|
async def download_all(dates: DownloadRange, background_tasks: BackgroundTasks):
|
||||||
|
|
||||||
date_start = dates.data_start
|
date_start = dates.data_start
|
||||||
date_finish = dates.data_finish
|
date_finish = dates.data_finish
|
||||||
# Парсим даты
|
|
||||||
try:
|
try:
|
||||||
start_date = datetime.strptime(date_start, "%Y-%m-%d")
|
start_date = datetime.strptime(date_start, "%Y-%m-%d")
|
||||||
finish_date = datetime.strptime(date_finish, "%Y-%m-%d")
|
finish_date = datetime.strptime(date_finish, "%Y-%m-%d")
|
||||||
@@ -481,7 +479,6 @@ async def download_all(dates: DownloadRange):
|
|||||||
|
|
||||||
all_files = []
|
all_files = []
|
||||||
|
|
||||||
# Собираем файлы за каждый день
|
|
||||||
current_date = start_date
|
current_date = start_date
|
||||||
while current_date <= finish_date:
|
while current_date <= finish_date:
|
||||||
date_path = current_date.strftime("%Y/%m/%d")
|
date_path = current_date.strftime("%Y/%m/%d")
|
||||||
@@ -497,11 +494,10 @@ async def download_all(dates: DownloadRange):
|
|||||||
|
|
||||||
logger.info(f"Найдено файлов: {len(all_files)}")
|
logger.info(f"Найдено файлов: {len(all_files)}")
|
||||||
|
|
||||||
# print(all_files)
|
|
||||||
if not all_files:
|
if not all_files:
|
||||||
return {"error": "Файлы не найдены за указанный период", "date_start": date_start, "date_finish": date_finish}
|
return {"error": "Файлы не найдены за указанный период", "date_start": date_start, "date_finish": date_finish}
|
||||||
|
|
||||||
# Создаём архив в директории DOCUMENTS_DIR
|
# Создаём архив
|
||||||
archive_name = f"documents_{date_start}_{date_finish}.zip"
|
archive_name = f"documents_{date_start}_{date_finish}.zip"
|
||||||
archive_path = os.path.join(DOCUMENTS_DIR, archive_name)
|
archive_path = os.path.join(DOCUMENTS_DIR, archive_name)
|
||||||
|
|
||||||
@@ -515,6 +511,15 @@ async def download_all(dates: DownloadRange):
|
|||||||
|
|
||||||
logger.info(f"Архив создан: {archive_path}")
|
logger.info(f"Архив создан: {archive_path}")
|
||||||
|
|
||||||
|
# Функция для удаления архива после отдачи
|
||||||
|
def cleanup_archive():
|
||||||
|
try:
|
||||||
|
if os.path.exists(archive_path):
|
||||||
|
os.remove(archive_path)
|
||||||
|
logger.info(f"Архив удалён: {archive_path}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Не удалось удалить архив: {e}")
|
||||||
|
|
||||||
# Возвращаем архив
|
# Возвращаем архив
|
||||||
response = FileResponse(
|
response = FileResponse(
|
||||||
path=archive_path,
|
path=archive_path,
|
||||||
@@ -522,23 +527,15 @@ async def download_all(dates: DownloadRange):
|
|||||||
media_type="application/zip"
|
media_type="application/zip"
|
||||||
)
|
)
|
||||||
response.headers["Access-Control-Allow-Origin"] = "*"
|
response.headers["Access-Control-Allow-Origin"] = "*"
|
||||||
response.headers["Access-Control-Allow-Methods"] = "GET, OPTIONS"
|
response.headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS"
|
||||||
response.headers["Access-Control-Allow-Headers"] = "Content-Type"
|
response.headers["Access-Control-Allow-Headers"] = "Content-Type, Authorization"
|
||||||
|
response.headers["Access-Control-Expose-Headers"] = "Content-Disposition"
|
||||||
|
|
||||||
# Удаляем архив после отправки
|
# Удаляем архив после отправки через background task
|
||||||
# Примечание: FileResponse сам отдаёт файл, удаление нужно делать после
|
background_tasks.add_task(cleanup_archive)
|
||||||
@response.streaming_callback
|
|
||||||
async def cleanup():
|
|
||||||
await response.body_send()
|
|
||||||
try:
|
|
||||||
if os.path.exists(archive_path):
|
|
||||||
os.remove(archive_path)
|
|
||||||
logger.info(f"Архив удалён: {archive_path}")
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"Не удалось удалить архив: {e}")
|
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@app.get("/logs")
|
@app.get("/logs")
|
||||||
def get_logs():
|
def get_logs():
|
||||||
with open("app.log", "r") as file:
|
with open("app.log", "r") as file:
|
||||||
|
|||||||
Reference in New Issue
Block a user