ready to deploy
Some checks failed
Build and Deploy / build_and_deploy (push) Failing after 10s

This commit is contained in:
2025-07-24 11:28:34 +03:00
parent 1c14f9bcc1
commit da6d345609
3 changed files with 79 additions and 5 deletions

View File

@@ -1,9 +1,10 @@
# ftp_parser.py
import os
import json
import logging
from typing import List, Dict
from datetime import datetime, timedelta
from dateutil import parser
log = logging.getLogger(__name__)
@@ -11,6 +12,7 @@ def process_json_files(directory: str) -> List[Dict]:
"""
Сканирует директорию, читает все .json файлы и извлекает из них данные
о фискальных регистраторах в стандартизированном формате.
Файлы, старше 21 дня (по полю 'current_time'), игнорируются.
:param directory: Путь к директории с JSON файлами.
:return: Список словарей, где каждый словарь представляет один ФР.
@@ -22,6 +24,9 @@ def process_json_files(directory: str) -> List[Dict]:
all_fr_data = []
log.info(f"Начинаю обработку JSON файлов из директории: {directory}")
# Пороговая дата: всё что старше, считаем неактуальным
freshness_threshold = datetime.now() - timedelta(days=21)
for filename in os.listdir(directory):
if filename.lower().endswith('.json'):
file_path = os.path.join(directory, filename)
@@ -29,6 +34,26 @@ def process_json_files(directory: str) -> List[Dict]:
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
current_time_str = data.get('current_time')
if not current_time_str:
log.warning(f"Пропущен файл {filename}: отсутствует поле 'current_time' для проверки актуальности.")
continue
try:
file_datetime = parser.parse(current_time_str)
# Приводим к aware-объекту с локальной таймзоной, если он naive, для корректного сравнения
if file_datetime.tzinfo is None:
file_datetime = file_datetime.astimezone()
# Сравниваем с пороговым значением (пороговое значение тоже делаем aware)
if file_datetime < freshness_threshold.astimezone(file_datetime.tzinfo):
log.warning(f"Пропущен файл {filename}: данные неактуальны (старше 21 дня). "
f"Дата файла: {file_datetime.strftime('%Y-%m-%d')}")
continue
except parser.ParserError:
log.error(f"Не удалось распознать дату в поле 'current_time' в файле {filename}: '{current_time_str}'")
continue
if 'serialNumber' not in data or not data['serialNumber']:
log.warning(f"Пропущен файл {filename}: отсутствует serialNumber.")
continue