diff --git a/1.1/整合.py b/1.1/整合.py index d50d322..87d5f85 100644 --- a/1.1/整合.py +++ b/1.1/整合.py @@ -19,7 +19,7 @@ CONFIG = { "106": { "base_url": "http://106.75.72.40:7500/api/proxy/tcp", "primary_auth": "Basic YWRtaW46bGljYWhr", - "x_auth": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjoxLCJsb2NhbGUiOiJ6aC1jbiIsInZpZXdNb2RlIjoibGlzdCIsInNpbmdsZUNsaWNrIjpmYWxzZSwicGVybSI6eyJhZG1pbiI6dHJ1ZSwiZXhlY3V0ZSI6dHJ1ZSwiY3JlYXRlIjp0cnVlLCJyZW5hbWUiOnRydWUsIm1vZGlmeSI6dHJ1ZSwiZGVsZXRlIjp0cnVlLCJzaGFyZSI6dHJ1ZSwiZG93bmxvYWQiOnRydWV9LCJjb21tYW5kcyI6W10sImxvY2tQYXNzd29yZCI6ZmFsc2UsImhpZGVEb3RmaWxlcyI6ZmFsc2V9LCJleHAiOjE3Njc2Njg3NzgsImlhdCI6MTc2NzY2MTU3OCwiaXNzIjoiRmlsZSBCcm93c2VyIn0.z9zycFSf3XpUDRhGjziUJ-PUeHIsRba23AI6itqXM-w" + "login_payload": {"username": "admin", "password": "licahk", "recaptcha": ""} }, "82": { "base_url": "http://82.156.1.111/weather/php", @@ -37,7 +37,6 @@ def add_error(source, name, reason, latest_time="N/A"): days_diff = "N/A" if latest_time and latest_time != "N/A": try: - # 兼容 2026_01_06 和 2026-01-06 格式 clean_date_str = str(latest_time).split()[0].replace('_', '-') target_date = datetime.strptime(clean_date_str, "%Y-%m-%d").date() today_date = datetime.now().date() @@ -63,12 +62,12 @@ def find_closest_item(items, is_date_level=True): scored_items = [] for item in items: if not isinstance(item, dict): continue - path = item.get('path', '') - if not path: continue + name_val = item.get('name', '') + path_val = item.get('path', '') + target_str = name_val if name_val else path_val.split('/')[-1] try: if is_date_level: - date_str = path.split('/')[-1] - current_date = datetime.strptime(date_str, "%Y_%m_%d") + current_date = datetime.strptime(target_str, "%Y_%m_%d") else: mod_str = item.get('modified', '') if mod_str: @@ -76,13 +75,12 @@ def find_closest_item(items, is_date_level=True): else: continue diff = abs((today - current_date.replace(tzinfo=None)).total_seconds()) - scored_items.append((diff, item)) + scored_items.append((diff, item, target_str)) except: continue - if not scored_items: return None scored_items.sort(key=lambda x: x[0]) - return scored_items[0][1] + return scored_items[0] def process_text_content(raw_content): @@ -105,16 +103,28 @@ def save_json(folder, name, data): json.dump(data, f, indent=4, ensure_ascii=False) -# --- 106 网站逻辑 (强化防御版) --- +# --- 106 网站逻辑 --- + +def get_106_dynamic_token(port): + """为106特定端口站点执行登录获取 Token""" + url = f"http://106.75.72.40:{port}/api/login" + try: + resp = requests.post(url, json=CONFIG["106"]["login_payload"], timeout=10) + if resp.status_code == 200: + return resp.text.strip().replace('"', '') + except: + pass + return None + def run_106_logic(): - print("\n>>> 开始处理 106 网站 (FRPS)...") + print("\n>>> 开始处理 106 网站 (FRPS - 修正逻辑)...") c = CONFIG["106"] - headers = {"Authorization": c["primary_auth"], "x-auth": c["x_auth"], "User-Agent": "Mozilla/5.0"} today_str = datetime.now().strftime("%Y_%m_%d") try: - resp = requests.get(c["base_url"], headers=headers, timeout=15) + main_headers = {"Authorization": c["primary_auth"], "User-Agent": "Mozilla/5.0"} + resp = requests.get(c["base_url"], headers=main_headers, timeout=15) if resp.status_code != 200: add_error("106网站", "主入口API", f"访问失败: HTTP {resp.status_code}") return @@ -123,81 +133,97 @@ def run_106_logic(): for item in proxies: if not isinstance(item, dict): continue name = item.get('name', 'Unknown') - if not name.endswith('_data'): continue + if not name.lower().endswith('_data'): continue + + # 1. 状态预检 - 离线拦截逻辑 + status_raw = item.get('status', '') + status = str(status_raw).lower().strip() if status_raw else "unknown" + + # 如果离线,直接保存并记录错误,不再进行后续 API 访问 + if status != 'online': + add_error("106网站", name, f"设备离线 (当前状态: {status})") + save_json(FRPS_DIR, name, item) + continue + + # 2. 识别站点类型并进行在线处理 + name_up = name.upper() + is_tower_underscore = "TOWER_" in name_up + is_tower_i = "TOWER" in name_up and not is_tower_underscore + if not (is_tower_underscore or is_tower_i): continue try: - # 1. 状态预检 - 彻底解决 NoneType 问题 - status_raw = item.get('status', '') - status = str(status_raw).lower().strip() if status_raw else "unknown" - conf = item.get('conf') or {} port = conf.get('remote_port') - - # 离线直接判定,严禁继续访问二级接口 - if status != 'online': - add_error("106网站", name, f"设备离线 (当前状态: {status})") - save_json(FRPS_DIR, name, item) - continue - if not port: add_error("106网站", name, "配置错误: 缺少 remote_port") continue - # 2. 只有 Online 才进行二级访问 - res2 = requests.get(f"http://106.75.72.40:{port}/api/resources/Data/", headers=headers, timeout=10) + # 只有 Online 才获取子站点 Token + token = get_106_dynamic_token(port) + if not token: + add_error("106网站", name, "Token获取失败(登录异常)") + continue + + headers = {"Authorization": c["primary_auth"], "x-auth": token, "User-Agent": "Mozilla/5.0"} + + # 查找 Data 根目录 (根据类型区分大小写) + api_root = "/api/resources/Data/" if is_tower_underscore else "/api/resources/data/" + res2 = requests.get(f"http://106.75.72.40:{port}{api_root}", headers=headers, timeout=10) if res2.status_code != 200: add_error("106网站", name, f"无法打开Data目录 (HTTP {res2.status_code})") continue it2 = res2.json().get('items', []) - closest_date = find_closest_item(it2, True) - if not closest_date: - add_error("106网站", name, "Data目录为空") - continue + best_date = find_closest_item(it2, is_date_level=True) + if not best_date or best_date[2] != today_str: + add_error("106网站", name, "未找到今日文件夹", best_date[2] if best_date else "N/A") + if not best_date: continue - path_date = closest_date.get('path', '') - date_val = path_date.split('/')[-1] + date_path = f"{api_root}{best_date[2]}/" - # 记录日期不对的情况,但尝试继续抓取 - if date_val != today_str: - add_error("106网站", name, "日期非当天", date_val) - - # 3. 访问日期内文件列表 - res3 = requests.get(f"http://106.75.72.40:{port}/api/resources{path_date}/", headers=headers, - timeout=10) + # 查找文件夹内最新文件 + res3 = requests.get(f"http://106.75.72.40:{port}{date_path}", headers=headers, timeout=10) it3 = res3.json().get('items', []) - closest_file = find_closest_item(it3, False) - if not closest_file: - add_error("106网站", name, "文件夹内无文件", date_val) + best_file = find_closest_item(it3, is_date_level=False) + if not best_file: + add_error("106网站", name, "文件夹内无文件", best_date[2]) continue - # 4. 读取内容并进行 NoneType 防御 - path_csv = closest_file.get('path', '') - res4 = requests.get(f"http://106.75.72.40:{port}/api/resources{path_csv}", headers=headers, timeout=10) - file_json = res4.json() + file_item = best_file[1] + full_path = file_item.get('path') or f"{date_path}{file_item.get('name')}" - if file_json is None: - add_error("106网站", name, "内容接口返回 Null", date_val) - continue - - raw_content = file_json.get('content', '') - if not raw_content: - add_error("106网站", name, "content字段为空", date_val) - - save_json(FRPS_DIR, name, { - "status": status, - "latest_path": path_csv, - "content": process_text_content(raw_content) - }) + # 3. 根据类型下载内容 + if is_tower_i: + # TowerI 模式:使用 raw 接口获取二进制 + raw_url = f"http://106.75.72.40:{port}/api/raw{full_path}" + res4 = requests.get(raw_url, headers=headers, timeout=20) + if res4.status_code == 200: + save_path = os.path.join(FRPS_DIR, f"{name}_{today_str}.bin") + with open(save_path, 'wb') as f: + f.write(res4.content) + print(f" ✅ {name} 二进制数据保存成功") + else: + # Tower_ 模式:使用 resources 接口获取 JSON + file_api_url = f"http://106.75.72.40:{port}/api/resources{full_path}" + res4 = requests.get(file_api_url, headers=headers, timeout=20) + file_json = res4.json() + raw_content = file_json.get('content', '') if file_json else None + if raw_content: + save_path = os.path.join(FRPS_DIR, f"{name}_{today_str}.json") + with open(save_path, 'w', encoding='utf-8') as f: + f.write(process_text_content(raw_content)) + print(f" ✅ {name} JSON数据保存成功") + else: + add_error("106网站", name, "文件内容为空", best_date[2]) except Exception as e: - add_error("106网站", name, f"站点级崩溃: {str(e)}") + add_error("106网站", name, f"站点处理崩溃: {str(e)}") except Exception as e: add_error("106网站", "全局逻辑", f"主进程崩溃: {str(e)}") -# --- 82 网站逻辑 --- +# --- 82 网站逻辑 (保持原样) --- def run_82_logic(): print("\n>>> 开始处理 82 网站 (Weather)...") @@ -253,7 +279,6 @@ def export_to_excel(): df = pd.DataFrame(error_logs) cols = ["数据来源", "站点/代理名称", "错误原因", "日期偏移量", "最新数据时间", "检查时间"] - # 过滤掉 dataframe 中不存在的列,防止报错 df = df[[c for c in cols if c in df.columns]] df.to_excel(EXCEL_PATH, index=False) print(f"\n[!] 错误报表已生成至: {EXCEL_PATH} (共 {len(error_logs)} 条)")