Spaces:
Sleeping
Sleeping
fix: 連不到法務部時改讀 LawsGraphify 每週鏡像;修正廢止與沿革欄位
Browse files- Hugging Face 連不到 law.moj.gov.tw,原本只能用 repo 裡 2026-02 的舊備份。
比照 LawsGraphify(GitHub Actions 每週下載法務部資料),法務部失敗時改讀它發布的
https://cormort.github.io/LawsGraphify/fund_rules.json(格式與法務部 API 相同)
- 法務部欄位是 LawAbandonNote 不是 LawAbandon:57 部已廢止辦法原本全被標成現行法規
- 沿革欄位是 LawHistories 不是 LawHistory:法規沿革原本一律空白
- 更新時間改顯示法務部資料日期,原本顯示的是伺服器啟動時間
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
22 unmodified lines23242526272876 unmodified lines10510610710810911011111211314 unmodified lines1281291301311321337 unmodified lines14114214314414514614714814915015115215339 unmodified lines1931941951961971985 unmodified lines20420520620720820921021121221321422 unmodified lines"chlaw": "https://law.moj.gov.tw/api/ch/law/json","chorder": "https://law.moj.gov.tw/api/ch/order/json"}# 檔名大小寫要與 repo 一致:Linux 容器區分大小寫(macOS 不分,本機測不出來)LOCAL_FILES = {"chlaw": "ChLaw.json", "chorder": "ChOrder.json"}76 unmodified linesif target_keyword in law_name:fund_name = law_name.replace("收支保管及運用辦法", "").replace("收支保管運用辦法", "")modified_date = parse_taiwan_date(law.get('LawModifiedDate'))law_history = clean_text(law.get('LawHistory', ''))is_abolished = (law.get('LawAbandon') == '廢')law_status = "廢止法規" if is_abolished else "現行法規"sort_weight = mapping.get(fund_name, 999999)14 unmodified lines# --- 資料載入與 API 更新 ---def load_local_data():print(">>> [啟動] 正在檢查本地 JSON 檔案...")all_local_data = []7 unmodified lineslaws = raw_json.get('Laws', [])all_local_data.extend(process_raw_data(laws))has_file = Trueexcept Exception as e:print(f" - 讀取 {filename} 失敗: {e}")if has_file and all_local_data:all_local_data.sort(key=lambda x: (x['法規狀態'] == '廢止法規', x['sort_weight'], x['基金名稱'], x['條次']))CACHE["data"] = all_local_dataCACHE["last_update"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")CACHE["source"] = "本地備份 (快速啟動)"print(f">>> [啟動] 本地資料載入完成,共 {len(all_local_data)} 筆。")else:39 unmodified linesraw_json = json.load(f)processed = process_raw_data(raw_json.get('Laws', []))new_api_data.extend(processed)# 更新本地備份with open(LOCAL_FILES[key], 'w', encoding='utf-8') as local_f:5 unmodified linesif tmp_path and os.path.exists(tmp_path):os.remove(tmp_path)if download_success and new_api_data:new_api_data.sort(key=lambda x: (x['法規狀態'] == '廢止法規', x['sort_weight'], x['基金名稱'], x['條次']))CACHE["data"] = new_api_dataCACHE["last_update"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")CACHE["source"] = "全國法規資料庫 API (最新)"print(f">>> [背景] API 更新完成,共 {len(new_api_data)} 筆。")CACHE["is_updating"] = False22 unmodified lines23242526272829303176 unmodified lines10810911011111211311411511614 unmodified lines1311321331341351361371381391401417 unmodified lines14915015115215315415515615715815916016116239 unmodified lines2022032042052062072085 unmodified lines21421521621721821922022122222322422522622722822923023123223323423523623723823922 unmodified lines"chlaw": "https://law.moj.gov.tw/api/ch/law/json","chorder": "https://law.moj.gov.tw/api/ch/order/json"}# Hugging Face 連不到 law.moj.gov.tw;LawsGraphify 每週由 GitHub Actions 下載法務部資料,# 把全部收支保管及運用辦法的原始紀錄發布在這裡(格式與法務部 API 相同)MIRROR_URL = "https://cormort.github.io/LawsGraphify/fund_rules.json"# 檔名大小寫要與 repo 一致:Linux 容器區分大小寫(macOS 不分,本機測不出來)LOCAL_FILES = {"chlaw": "ChLaw.json", "chorder": "ChOrder.json"}76 unmodified linesif target_keyword in law_name:fund_name = law_name.replace("收支保管及運用辦法", "").replace("收支保管運用辦法", "")modified_date = parse_taiwan_date(law.get('LawModifiedDate'))law_history = clean_text(law.get('LawHistories', ''))is_abolished = bool(law.get('LawAbandonNote'))law_status = "廢止法規" if is_abolished else "現行法規"sort_weight = mapping.get(fund_name, 999999)14 unmodified lines# --- 資料載入與 API 更新 ---def data_date(raw_json):"""法務部 UpdateDate「2026/10/16 上午 12:00:00」→「2026/10/16(法務部資料日期)」"""d = str(raw_json.get('UpdateDate', '')).split(' ')[0]return f"{d}(法務部資料日期)" if d else datetime.now().strftime("%Y-%m-%d %H:%M:%S")def load_local_data():print(">>> [啟動] 正在檢查本地 JSON 檔案...")all_local_data = []7 unmodified lineslaws = raw_json.get('Laws', [])all_local_data.extend(process_raw_data(laws))has_file = Truelocal_date = data_date(raw_json)except Exception as e:print(f" - 讀取 {filename} 失敗: {e}")if has_file and all_local_data:all_local_data.sort(key=lambda x: (x['法規狀態'] == '廢止法規', x['sort_weight'], x['基金名稱'], x['條次']))CACHE["data"] = all_local_dataCACHE["last_update"] = local_dateCACHE["source"] = "本地備份 (快速啟動)"print(f">>> [啟動] 本地資料載入完成,共 {len(all_local_data)} 筆。")else:39 unmodified linesraw_json = json.load(f)processed = process_raw_data(raw_json.get('Laws', []))new_api_data.extend(processed)new_date = data_date(raw_json)# 更新本地備份with open(LOCAL_FILES[key], 'w', encoding='utf-8') as local_f:5 unmodified linesif tmp_path and os.path.exists(tmp_path):os.remove(tmp_path)source = "全國法規資料庫 API (最新)"if not (download_success and new_api_data):# 連不到法務部(Hugging Face 上的常態):改讀 LawsGraphify 每週發布的鏡像try:r = await asyncio.to_thread(requests.get, MIRROR_URL, timeout=60)r.raise_for_status()raw_json = r.json()new_api_data = process_raw_data(raw_json.get('Laws', []))new_date = data_date(raw_json)download_success = Truesource = "LawsGraphify 每週鏡像(法務部資料)"print(f">>> [背景] 改用 LawsGraphify 鏡像,共 {len(new_api_data)} 筆。")except Exception as e:print(f" - 鏡像下載失敗: {e}")if download_success and new_api_data:new_api_data.sort(key=lambda x: (x['法規狀態'] == '廢止法規', x['sort_weight'], x['基金名稱'], x['條次']))CACHE["data"] = new_api_dataCACHE["last_update"] = new_dateCACHE["source"] = sourceprint(f">>> [背景] API 更新完成,共 {len(new_api_data)} 筆。")CACHE["is_updating"] = False
app.py+30-5