""" 台湾彩券资讯采集模块 - 列表接口: https://api.taiwanlottery.com/TLCAPIWeB/News/List - 详情页面: https://www.taiwanlottery.com/news/news/{newsId} 写入分类: - la_article_cate.id = 21 (彩票资讯) """ import html import json import re import time from datetime import datetime from urllib.parse import urljoin import pymysql import requests from article_publish_helper import has_publishable_article_content, resolve_crawled_article_is_show ARTICLE_CID = 21 LIST_API = "https://api.taiwanlottery.com/TLCAPIWeB/News/List?keyword&from=2025-06&to=2026-06&type=2&webtag=1&pageSize=20&pageNo={page}" DETAIL_URL = "https://www.taiwanlottery.com/news/news/{news_id}" DETAIL_API = "https://api.taiwanlottery.com/TLCAPIWeB/News/Detail/{news_id}" SOURCE_NAME = "台湾彩券" MAX_PAGES = 3 def clean_text(text: str) -> str: return re.sub(r"\s+", " ", html.unescape(text or "").replace("\u3000", " ")).strip() def parse_announce_date(value: str) -> tuple[int, str]: if not value: now = int(time.time()) return now, datetime.fromtimestamp(now).strftime("%Y-%m-%d %H:%M:%S") try: dt = datetime.fromisoformat(value.replace("Z", "+00:00")) ts = int(dt.timestamp()) return ts, dt.strftime("%Y-%m-%d %H:%M:%S") except Exception: now = int(time.time()) return now, datetime.fromtimestamp(now).strftime("%Y-%m-%d %H:%M:%S") def fetch_list(session: requests.Session, page_no: int) -> list[dict]: resp = session.get(LIST_API.format(page=page_no), timeout=30) resp.raise_for_status() data = resp.json() if data.get("rtCode") != 0: raise RuntimeError(f"台湾彩券列表接口异常: {data.get('rtMsg')}") return data.get("content", {}).get("newsListRes", []) or [] def strip_wrapper_tags(html_text: str) -> str: content = html_text or "" content = re.sub(r"^\ufeff", "", content) content = re.sub(r"?(?:html|body)[^>]*>", "", content, flags=re.I) content = re.sub(r"
", "", content, flags=re.I) content = re.sub(r"