# -*- coding: utf-8 -*-

import os
import json

from services.fin_land_service import fetch_fin_land_article_html
from services.fin_land_parser import parse_fin_land_article_html


BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DEBUG_DIR = os.path.join(BASE_DIR, "debug_outputs")


def main():
    article_no = input("articleNo 입력: ").strip()

    if not article_no:
        article_no = "2624635265"

    os.makedirs(DEBUG_DIR, exist_ok=True)

    html_path = os.path.join(DEBUG_DIR, f"fin_article_{article_no}.html")

    if os.path.exists(html_path):
        print(f"[INFO] 기존 HTML 사용: {html_path}")

        with open(html_path, "r", encoding="utf-8") as f:
            html = f.read()

    else:
        print(f"[INFO] HTML 파일 없음. fin.land에서 새로 가져옵니다: {article_no}")

        html = fetch_fin_land_article_html(article_no)

        with open(html_path, "w", encoding="utf-8") as f:
            f.write(html)

        print(f"[INFO] HTML 저장 완료: {html_path}")

    data = parse_fin_land_article_html(html)

    print("=" * 80)
    print("[PARSE RESULT]")
    print("=" * 80)
    print(json.dumps(data, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()