gendesign/tradein-mvp/backend/app/tasks/fns_opendata_load.py
lekss361 e0bef636e6
All checks were successful
Deploy Trade-In / changes (push) Successful in 12s
Deploy Trade-In / build-frontend (push) Has been skipped
Deploy Trade-In / build-browser (push) Has been skipped
Deploy Trade-In / test (push) Successful in 4m17s
Deploy Trade-In / build-backend (push) Successful in 1m3s
Deploy Trade-In / deploy (push) Successful in 7m11s
Deploy Trade-In / deploy-status (push) Successful in 1s
Deploy Trade-In / perimeter-smoke (push) Successful in 11s
feat(tradein): bulk-дампы открытых данных ФНС по юрлицам + lookup по ИНН (#3429)
2026-09-08 22:29:10 +00:00

71 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""CLI: ФНС opendata (revexp/sshr2019/debtam/snr) → fns_legal_entity_facts.
Только загрузка + lookup (`app/services/fns_lookup.py`) — потребителя у данных пока
нет, см. docstring `app/services/fns_opendata_loader.py`.
Запуск из контейнера tradein-backend/tradein-scraper (нужен интернет к nalog.gov.ru):
python -m app.tasks.fns_opendata_load # все 4 набора
python -m app.tasks.fns_opendata_load --datasets revexp # один набор
python -m app.tasks.fns_opendata_load --dry-run # без записи, только резолв ссылки
python -m app.tasks.fns_opendata_load --force # перекачать, даже если версия та же
В режиме --dry-run скачивание/парс/запись не происходят — только резолв актуальной
ссылки со страницы каталога (проверка доступности + логирование того, что было бы
скачано).
"""
from __future__ import annotations
import argparse
import logging
from app.core.db import SessionLocal
from app.services.fns_opendata_loader import DATASET_SLUGS, load_dataset
logger = logging.getLogger(__name__)
def build_parser() -> argparse.ArgumentParser:
"""Парсер CLI (вынесен для тестируемости флагов без запуска main)."""
parser = argparse.ArgumentParser(
description="ФНС opendata loader: revexp/sshr2019/debtam/snr → fns_legal_entity_facts"
)
parser.add_argument(
"--datasets",
nargs="+",
choices=list(DATASET_SLUGS),
default=list(DATASET_SLUGS),
help="список наборов (по умолчанию — все 4)",
)
parser.add_argument(
"--dry-run", action="store_true", help="без записи в БД — только резолв ссылки/подсчёт"
)
parser.add_argument(
"--force", action="store_true", help="перекачать, даже если версия совпадает с загруженной"
)
return parser
def main() -> None:
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
)
args = build_parser().parse_args()
db = SessionLocal()
try:
results: dict[str, object] = {}
for slug in args.datasets:
result = load_dataset(db, slug, dry_run=args.dry_run, force=args.force)
if not args.dry_run:
db.commit()
results[slug] = result
logger.info("fns_opendata_load DONE: dry_run=%s %s", args.dry_run, results)
finally:
db.close()
if __name__ == "__main__":
main()