ita_plugin.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. import time
  2. import json
  3. import random
  4. import uuid
  5. import shutil
  6. import re
  7. import os
  8. import base64
  9. from datetime import datetime
  10. from typing import List, Dict, Optional, Any, Callable
  11. from urllib.parse import urlencode, urlparse
  12. # DrissionPage 核心
  13. from DrissionPage import ChromiumPage, ChromiumOptions
  14. from vs_plg import IVSPlg
  15. from vs_types import VSPlgConfig, AppointmentType, VSQueryResult, VSBookResult, AvailabilityStatus, TimeSlot, DateAvailability, NotFoundError, PermissionDeniedError, RateLimiteddError, SessionExpiredOrInvalidError, BizLogicError
  16. from toolkit.proxy_tunnel import ProxyTunnel
  17. from toolkit.vs_cloud_api import VSCloudApi
  18. from utils.mouse import HumanMouse
  19. from utils.scroll import HumanScroll
  20. class BrowserResponse:
  21. def __init__(self, result_dict):
  22. result_dict = result_dict or {}
  23. self.status_code = result_dict.get('status', 0)
  24. self.text = result_dict.get('body', '')
  25. self.headers = result_dict.get('headers', {})
  26. self.url = result_dict.get('url', '')
  27. self._json = None
  28. def json(self):
  29. if self._json is None:
  30. if not self.text: return {}
  31. try: self._json = json.loads(self.text)
  32. except: self._json = {}
  33. return self._json
  34. # ==========================================
  35. # 2. ItaPlugin 核心逻辑
  36. # ==========================================
  37. class ItaPlugin(IVSPlg):
  38. def __init__(self, group_id: str):
  39. self.group_id = group_id
  40. self.config: Optional[VSPlgConfig] = None
  41. self.free_config: Dict[str, Any] = {}
  42. self.is_healthy = True
  43. self.logger = None
  44. self.page: Optional[ChromiumPage] = None
  45. # Prenotami 特有配置
  46. self._service_id = 0
  47. self._host = 'https://prenotami.esteri.it'
  48. # --- [核心修改] 并发隔离与资源管理 ---
  49. # 生成唯一实例 ID
  50. self.instance_id = uuid.uuid4().hex[:8]
  51. self.root_workspace = os.path.abspath(os.path.join("data/temp_browser_data", f"{self.group_id}.{self.instance_id}"))
  52. # 定义子目录:代理插件目录 & 浏览器用户数据目录
  53. self.user_data_path = os.path.join(self.root_workspace, "user_data")
  54. # 确保根目录存在 (子目录由具体逻辑创建)
  55. if not os.path.exists(self.root_workspace):
  56. os.makedirs(self.root_workspace)
  57. # 持有隧道实例
  58. self.tunnel = None
  59. self.session_create_time: float = 0
  60. def get_group_id(self) -> str:
  61. return self.group_id
  62. def set_log(self, logger: Callable[[str], None]) -> None:
  63. self.logger = logger
  64. def _log(self, message):
  65. if self.logger:
  66. self.logger(f'[ItaPlugin] [{self.group_id}] {message}')
  67. else:
  68. print(f'[ItaPlugin] [{self.group_id}] {message}')
  69. def set_config(self, config: VSPlgConfig):
  70. self.config = config
  71. self.free_config = config.free_config or {}
  72. # Service ID (e.g., 1321 for Ireland, 5059 for Guangzhou)
  73. self._service_id = self.free_config.get('service_id', 0)
  74. def keep_alive(self):
  75. pass
  76. def health_check(self) -> bool:
  77. if not self.is_healthy or not self.page:
  78. return False
  79. try:
  80. if not self.page.run_js("return 1;"):
  81. return False
  82. except:
  83. return False
  84. if self.config.session_max_life > 0:
  85. if time.time() - self.session_create_time > self.config.session_max_life * 60:
  86. self._log("Session expired.")
  87. return False
  88. return True
  89. # -------------------------------------------------------------
  90. # 1. Create Session (Login)
  91. # -------------------------------------------------------------
  92. def create_session(self):
  93. """
  94. 全浏览器登录流程:
  95. 1. 启动浏览器
  96. 2. 解决 ReCaptcha
  97. 3. 登录并维持 Session
  98. """
  99. self._log(f"Initializing Session (ID: {self.instance_id})...")
  100. co = ChromiumOptions()
  101. # -------------------------------------------------------------
  102. # [核心修复] 解决 'not enough values to unpack'
  103. # -------------------------------------------------------------
  104. # 1. 不要用 co.auto_port(),因为它依赖解析 stdout,会被 DBus 报错干扰
  105. # 2. 我们手动随机生成一个端口
  106. import random
  107. import socket
  108. def get_free_port():
  109. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  110. s.bind(('', 0))
  111. return s.getsockname()[1]
  112. debug_port = get_free_port()
  113. self._log(f"Assigned Debug Port: {debug_port}")
  114. # --- [关键配置] 设置独立的用户数据目录 ---
  115. # 这样每个实例的 Cache, Cookies, LocalStorage 都是完全隔离的
  116. # 同时也防止了多进程争抢同一个 Default 文件夹导致的崩溃
  117. co.set_user_data_path(self.user_data_path)
  118. # --- 1. 指定浏览器路径 (适配 Docker) ---
  119. chrome_path = os.getenv("CHROME_BIN")
  120. if chrome_path and os.path.exists(chrome_path):
  121. co.set_paths(browser_path=chrome_path)
  122. # --- [核心修改] 代理配置 ---
  123. if self.config.proxy and self.config.proxy.ip:
  124. p = self.config.proxy
  125. if p.username and p.password:
  126. self._log(f"Starting Proxy Tunnel for {p.ip}...")
  127. # 1. 启动本地隧道
  128. self.tunnel = ProxyTunnel(p.ip, p.port, p.username, p.password)
  129. local_proxy = self.tunnel.start()
  130. self._log(f"Tunnel started at {local_proxy}")
  131. # 2. Chrome 连接本地免密端口
  132. # 必须使用 --proxy-server 强制指定,绝对稳健
  133. co.set_argument(f'--proxy-server={local_proxy}')
  134. else:
  135. # 无密码代理,直接用
  136. proxy_str = f"{p.scheme}://{p.ip}:{p.port}"
  137. co.set_argument(f'--proxy-server={proxy_str}')
  138. else:
  139. self._log("[WARN] No proxy configured!")
  140. co.headless(False)
  141. co.set_argument('--no-sandbox')
  142. co.set_argument('--disable-gpu')
  143. co.set_argument('--disable-dev-shm-usage')
  144. co.set_argument('--window-size=1920,1080')
  145. co.set_argument('--disable-blink-features=AutomationControlled')
  146. try:
  147. self.page = ChromiumPage(co)
  148. login_url = f"{self._host}/Home"
  149. self._log(f"Navigating to {login_url}")
  150. self.page.get(login_url)
  151. # 等待登录框
  152. if not self.page.wait.ele_displayed('#login-email', timeout=20):
  153. raise BizLogicError("Login page not loaded")
  154. # 填充用户名密码
  155. self.page.ele('#login-email').input(self.config.account.username)
  156. self.page.ele('#login-password').input(self.config.account.password)
  157. # 4. [核心修改] 解决 ReCaptcha V3 Enterprise 并注入
  158. # Prenotami 使用的是 Enterprise V3, Action = 'LOGIN'
  159. self._solve_and_inject_prenotami_captcha()
  160. human_mouse = HumanMouse(self.page, debug=True)
  161. human_scroll = HumanScroll(self.page)
  162. # 先定位
  163. self._log("Locating Login button...")
  164. login_btn = self.page.ele('@id=captcha-trigger')
  165. self._log("Scrolling to make button visible...")
  166. human_scroll.scroll_to_element(login_btn, humanize=True)
  167. self._log("Moving mouse to Login button...")
  168. human_mouse.move_to(login_btn, duration=random.uniform(0.6, 1.0))
  169. time.sleep(random.uniform(0.3, 0.5))
  170. self._log("Clicking Login button...")
  171. login_btn.click()
  172. self._log("Login button clicked.")
  173. # 等待 URL 变化或特定元素出现
  174. # 成功通常跳转到 /UserArea, 失败则留在 /Home
  175. end_time = time.time() + 45
  176. login_success = False
  177. while time.time() < end_time:
  178. time.sleep(1)
  179. curr_url = self.page.url
  180. # 成功特征
  181. if "/UserArea" in curr_url or "/Services" in curr_url:
  182. login_success = True
  183. break
  184. # 失败特征
  185. if self.page.ele('.validation-summary-errors') or self.page.ele('.field-validation-error'):
  186. err_text = self.page.ele('.validation-summary-errors').text if self.page.ele('.validation-summary-errors') else "Unknown validation error"
  187. raise PermissionDeniedError(f"Login Failed: {err_text}")
  188. # 检查是否有弹窗错误
  189. if "Home" in curr_url and self.page.ele('#logoutForm'):
  190. # 有时候虽然在 Home 但出现了 Logout 按钮,也算成功
  191. login_success = True
  192. break
  193. if not login_success:
  194. # 截图保留现场
  195. # self.page.get_screenshot(path="login_fail.jpg")
  196. raise BizLogicError("Login Failed: Timeout waiting for redirect (Captcha score too low?)")
  197. self._log("Login Successful.")
  198. self.session_create_time = time.time()
  199. except Exception as e:
  200. self._log(f"Create Session Failed: {e}")
  201. self.cleanup()
  202. raise e
  203. # -------------------------------------------------------------
  204. # 2. Query Availability
  205. # -------------------------------------------------------------
  206. def query(self, apt_type: AppointmentType) -> VSQueryResult:
  207. res = VSQueryResult()
  208. res.success = False
  209. res.availability_status = AvailabilityStatus.NoneAvailable
  210. if not self._service_id:
  211. raise BizLogicError("Service ID not configured")
  212. # 1. 检查 Slot 是否可用 (Check Availability Endpoint)
  213. check_url = f"{self._host}/Services/Booking/{self._service_id}"
  214. # 使用 Fetch 发起检查请求
  215. resp = self._perform_request("GET", check_url, headers={
  216. "Referer": f"{self._host}/Services",
  217. "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
  218. })
  219. # 302 跳转处理逻辑
  220. if resp.status_code == 200:
  221. # 200 表示进入了预约页,有号
  222. self._log("Slot Check: 200 OK (Availability Detected)")
  223. pass
  224. elif "BookingCalendar" in resp.url: # 或者是被重定向到了 Calendar
  225. self._log("Slot Check: Redirected to Calendar (Availability Detected)")
  226. pass
  227. else:
  228. # 被重定向回 Home 或 Service,说明没号或 Session 过期
  229. if "Home" in resp.url or "Login" in resp.url:
  230. self.is_healthy = False
  231. raise SessionExpiredOrInvalidError("Session expired during query")
  232. self._log("Slot Check: No availability (Redirected back)")
  233. return res
  234. # 2. 查询月份 (Query Month)
  235. # 默认查询当月,或者配置的月份
  236. tar_dates = self.free_config.get("target_dates", [])
  237. if not tar_dates:
  238. # 默认查下个月
  239. next_month = datetime.now().replace(day=28) + datetime.timedelta(days=4)
  240. tar_dates = [next_month.strftime("%Y-%m-%d")]
  241. all_slots = []
  242. # Prenotami 需要先 retrieve server info
  243. self._perform_request("GET", f"{self._host}/BookingCalendar/RetrieveServerInfo")
  244. for date_str in tar_dates:
  245. # 构造月份格式 2026-01-05 -> 2026-01-01 (API 需要)
  246. try:
  247. dt = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S.%fZ")
  248. except:
  249. try:
  250. dt = datetime.strptime(date_str, "%Y-%m-%d")
  251. except:
  252. dt = datetime.now()
  253. # API 需要格式: 2025-11-05T... 格式的字符串作为 selectedDay
  254. # 实际上 RetrieveCalendarAvailability 只需要由前端日历控件触发的格式
  255. # 查询日历 API
  256. cal_url = f"{self._host}/BookingCalendar/RetrieveCalendarAvailability"
  257. cal_payload = {
  258. "_Servizio": str(self._service_id),
  259. "selectedDay": date_str # 原样传配置里的 ISO 串
  260. }
  261. resp_cal = self._perform_request("POST", cal_url, json_data=cal_payload)
  262. if resp_cal.status_code != 200: continue
  263. # 解析有效日期
  264. valid_days = self._parse_valid_days(resp_cal.text)
  265. self._log(f"Valid days for {date_str}: {valid_days}")
  266. if valid_dates:
  267. res.success = True
  268. res.availability_status = AvailabilityStatus.Available
  269. earliest_date = valid_dates[0]
  270. earliest_dt = datetime.strptime(earliest_date, "%Y-%m-%d")
  271. res.earliest_date = earliest_dt
  272. for day in valid_days:
  273. # 查询具体 Slot
  274. slot_url = f"{self._host}/BookingCalendar/RetrieveTimeSlots"
  275. slot_payload = {
  276. "selectedDay": day, # YYYY-MM-DD
  277. "idService": str(self._service_id)
  278. }
  279. resp_slot = self._perform_request("POST", slot_url, json_data=slot_payload)
  280. time_slots = self._parse_time_slots(resp_slot.text)
  281. ts_list = []
  282. if time_slots:
  283. # 转换结构
  284. for ts in time_slots:
  285. # ts: {'id': 123, 'start': '10:00', 'end': '10:30', 'remain': 1}
  286. ts_list.append(TimeSlot(
  287. time=f"{ts['start']} - {ts['end']}",
  288. label=str(ts['id']) # 将 ID 存入 label 以便 book 使用
  289. ))
  290. res.availability.append(DateAvailability(date=datetime.strptime(day, "%d-%m-%Y"), times=ts_list))
  291. return res
  292. # -------------------------------------------------------------
  293. # 3. Book
  294. # -------------------------------------------------------------
  295. def book(self, slot_info: VSQueryResult, user_inputs: Dict = None) -> VSBookResult:
  296. res = VSBookResult()
  297. res.success = False
  298. if not slot_info.availability:
  299. raise NotFoundError("No slots to book")
  300. target_dt = slot_info.availability[0].date
  301. target_date = target_dt.strftime("%Y-%m-%d")
  302. # 取第一个时间段
  303. target_slot = slot_info.availability[0].times[0]
  304. slot_id = target_slot.label # 我们在 query 里把 ID 存在了 label
  305. slot_text = target_slot.time # "10:00 - 10:30"
  306. # 1. 获取 OTP (GenerateOTP)
  307. self._log("Requesting OTP...")
  308. otp_url = f"{self._host}/BookingCalendar/GenerateOTP?ServiceID={self._service_id}"
  309. self._perform_request("POST", otp_url)
  310. # 2. 等待并读取邮件
  311. self._log("Waiting for email code...")
  312. time.sleep(10) # 稍微等一下发信
  313. email_account = self.config.account.email
  314. # 使用 CloudAPI 读取 (假设已配置)
  315. otp_code = VSCloudApi.Instance().get_email_verify_code(email_account)
  316. if not otp_code:
  317. raise BizLogicError("Failed to retrieve OTP code")
  318. self._log(f"Got OTP: {otp_code}")
  319. # 3. 提交详细信息 (Fill User Info)
  320. # 这是最复杂的一步,涉及文件上传 (Multipart)
  321. self._log("Submitting User Details & Files...")
  322. # 准备文件 (转 Base64 传给 JS)
  323. passport_pdf_path = user_inputs.get('passport_pdf_path')
  324. irp_pdf_path = user_inputs.get('irp_pdf_path')
  325. def file_to_b64(path):
  326. if not path or not os.path.exists(path): return ""
  327. with open(path, "rb") as f:
  328. return base64.b64encode(f.read()).decode('utf-8')
  329. ppt_b64 = file_to_b64(passport_pdf_path)
  330. irp_b64 = file_to_b64(irp_pdf_path)
  331. # 构造 JS FormData 提交脚本
  332. # 注意:这里需要根据 Service ID (Dublin/Canton) 动态调整字段 ID
  333. # 下面以 Dublin (1321) 的字段为例,如果是 Canton 需要修改 _Id 和 _TipoDatoAddizionale
  334. # 为了通用性,这里演示 Dublin 的结构,请根据实际 Service ID 调整 mapping
  335. # 假设是 Dublin (根据提供的源码分析)
  336. boundary = '----WebKitFormBoundaryRandomString'
  337. submit_url = f"{self._host}/Services/Booking/{self._service_id}"
  338. # 注入 JS 执行
  339. js_submit = f"""
  340. const url = "{submit_url}";
  341. const fd = new FormData();
  342. // 基础字段
  343. fd.append('ServizioDescrizione', 'D Visa Application');
  344. fd.append('MessaggioRassicuranteWaitingList', 'True');
  345. fd.append('isWaitingListEnabled', 'False');
  346. fd.append('IDServizioConsolare', '35');
  347. fd.append('IDServizioErogato', '{self._service_id}');
  348. fd.append('IdTipoPrenotazione', '1'); // Single
  349. fd.append('NumMaxAccompagnatori', '3');
  350. fd.append('NumAccompagnatoriSelected', '0');
  351. // 动态字段 (Dublin 示例)
  352. // [0] Other citizenship -> User Input
  353. fd.append('DatiAddizionaliPrenotante[0]._Descrizione', 'Other citizenship/s');
  354. fd.append('DatiAddizionaliPrenotante[0]._testo', '{user_inputs.get("citizen", "China")}');
  355. fd.append('DatiAddizionaliPrenotante[0]._Obbligatorio', 'False');
  356. fd.append('DatiAddizionaliPrenotante[0]._Id', '61738');
  357. fd.append('DatiAddizionaliPrenotante[0]._TipoDatoAddizionale.IDTipoDatoAddizionale', '26');
  358. fd.append('DatiAddizionaliPrenotante[0]._TipoDatoAddizionale.IDTipoControllo', '2');
  359. // [1] Full address -> User Input
  360. fd.append('DatiAddizionaliPrenotante[1]._Descrizione', 'Full residence address');
  361. fd.append('DatiAddizionaliPrenotante[1]._testo', '{user_inputs.get("address", "")}');
  362. fd.append('DatiAddizionaliPrenotante[1]._Obbligatorio', 'True');
  363. fd.append('DatiAddizionaliPrenotante[1]._Id', '61739');
  364. fd.append('DatiAddizionaliPrenotante[1]._TipoDatoAddizionale.IDTipoDatoAddizionale', '25');
  365. fd.append('DatiAddizionaliPrenotante[1]._TipoDatoAddizionale.IDTipoControllo', '2');
  366. // [2] Passport Num
  367. fd.append('DatiAddizionaliPrenotante[2]._Descrizione', 'Passport number');
  368. fd.append('DatiAddizionaliPrenotante[2]._testo', '{user_inputs.get("passport", "")}');
  369. fd.append('DatiAddizionaliPrenotante[2]._Obbligatorio', 'True');
  370. fd.append('DatiAddizionaliPrenotante[2]._Id', '61740');
  371. fd.append('DatiAddizionaliPrenotante[2]._TipoDatoAddizionale.IDTipoDatoAddizionale', '2');
  372. fd.append('DatiAddizionaliPrenotante[2]._TipoDatoAddizionale.IDTipoControllo', '2');
  373. // [3] Reason (Select)
  374. fd.append('DatiAddizionaliPrenotante[3]._Descrizione', 'Reason for visit');
  375. fd.append('DatiAddizionaliPrenotante[3]._Obbligatorio', 'True');
  376. fd.append('DatiAddizionaliPrenotante[3]._Id', '61741');
  377. fd.append('DatiAddizionaliPrenotante[3]._TipoDatoAddizionale.IDTipoDatoAddizionale', '34');
  378. fd.append('DatiAddizionaliPrenotante[3]._TipoDatoAddizionale.IDTipoControllo', '3');
  379. fd.append('DatiAddizionaliPrenotante[3]._idSelezionato', '42'); // 42 = Tourism? Need verify
  380. // OTP
  381. fd.append('otp-input', '{otp_code}');
  382. fd.append('PrivacyCheck', 'true');
  383. // 文件处理 (Base64 -> Blob -> FormData)
  384. // 注意:这里假设页面上有文件上传的对应 ID,或者我们直接硬编码 FormData
  385. // 原始抓包并未显示文件字段名,通常是 File_0, File_1
  386. // 我们需要将 base64 转 blob
  387. async function addFile(b64, name, filename) {{
  388. if(!b64) return;
  389. const res = await fetch(`data:application/pdf;base64,${{b64}}`);
  390. const blob = await res.blob();
  391. fd.append(name, blob, filename);
  392. }}
  393. // 并行处理文件
  394. await Promise.all([
  395. addFile('{ppt_b64}', 'File_0', 'passport.pdf'), // 假设 File_0 是护照
  396. addFile('{irp_b64}', 'File_1', 'irp.pdf') // 假设 File_1 是 IRP
  397. ]);
  398. // 发送 POST
  399. return fetch(url, {{
  400. method: 'POST',
  401. body: fd
  402. }}).then(async r => {{
  403. return {{ status: r.status, url: r.url, text: await r.text() }};
  404. }}).catch(e => {{ return {{ status: 0, text: e.toString() }}; }});
  405. """
  406. result_dict = self.page.run_js(js_submit)
  407. resp = BrowserResponse(result_dict)
  408. if resp.status_code == 302 or "BookingCalendar" in resp.url:
  409. self._log("User Info Submitted Successfully.")
  410. else:
  411. self._log(f"User Info Submit Failed: {resp.text[:100]}")
  412. # 如果 OTP 错误,页面会返回特定错误信息
  413. if "Codice errato" in resp.text:
  414. raise BizLogicError("Invalid OTP Code")
  415. return res # Fail
  416. # 4. 最终确认预约 (InsertNewBooking)
  417. self._log("Finalizing Booking...")
  418. final_url = f"{self._host}/BookingCalendar/InsertNewBooking"
  419. final_payload = {
  420. "idCalendarioGiornaliero": slot_id,
  421. "selectedDay": target_date,
  422. "selectedHour": slot_text # "10:00 - 10:30(2)"
  423. }
  424. # 这里用 Form-UrlEncoded
  425. resp_final = self._perform_request("POST", final_url, data=final_payload)
  426. if resp_final.status_code == 200:
  427. self._log("Booking Confirmed!")
  428. res.success = True
  429. res.book_date = target_date
  430. res.book_time = slot_text
  431. else:
  432. self._log(f"Final Booking Failed: {resp_final.status_code}")
  433. return res
  434. # -------------------------------------------------------------
  435. # 4. Helpers
  436. # -------------------------------------------------------------
  437. def _get_proxy_url(self):
  438. # 构造代理
  439. proxy_url = ""
  440. if self.config.proxy.ip:
  441. s = self.config.proxy
  442. if s.username:
  443. proxy_url = f"{s.scheme}://{s.username}:{s.password}@{s.ip}:{s.port}"
  444. else:
  445. proxy_url = f"{s.scheme}://{s.ip}:{s.port}"
  446. return proxy_url
  447. def _solve_and_inject_prenotami_captcha(self):
  448. """
  449. 专门处理 Prenotami 的 ReCaptcha Enterprise
  450. """
  451. self._log("Solving ReCaptcha Enterprise (Action: LOGIN)...")
  452. api_token = self.free_config.get("capsolver_key", "")
  453. if not api_token:
  454. raise BizLogicError("Capsolver Key is required for Prenotami")
  455. # 从 HTML 源码中提取的信息
  456. site_key = "6LdkwrIqAAAAAC4NX-g_j7lEx9vh1rg94ZL2cFfY"
  457. page_url = self.page.url
  458. # 注意:Prenotami 的这个 Key 其实是混合模式,
  459. # 虽然它是 V3 (Enterprise),但很多打码平台用 V2 接口也能解,或者必须用 V3 Enterprise 接口
  460. # 建议先尝试 ReCaptchaV3EnterpriseTaskProxyLess
  461. # 修正为最标准的 V3 Enterprise 配置
  462. rc_params = {
  463. "type": "ReCaptchaV3EnterpriseTaskProxyless",
  464. "page": page_url,
  465. "siteKey": site_key,
  466. "action": "LOGIN", # 关键参数
  467. "minScore": 0.7, # 要求高分
  468. "apiToken": api_token,
  469. # "proxy": self._get_proxy_url()
  470. }
  471. g_token = self._solve_recaptcha(rc_params)
  472. self._log(f"Captcha Solved. Token length: {len(g_token)}")
  473. hook_js = f"""
  474. // 1. 填充隐藏域 (双重保险)
  475. var input = document.getElementById('g-recaptcha-response');
  476. if(input) {{
  477. input.value = "{g_token}";
  478. }}
  479. // 2. 劫持 grecaptcha.execute 和 grecaptcha.enterprise.execute
  480. // 无论网页用哪个版本,都拦截下来
  481. var mockExecute = function() {{
  482. console.log("Recaptcha execution intercepted!");
  483. return Promise.resolve("{g_token}");
  484. }};
  485. if (window.grecaptcha) {{
  486. window.grecaptcha.execute = mockExecute;
  487. if (window.grecaptcha.enterprise) {{
  488. window.grecaptcha.enterprise.execute = mockExecute;
  489. }}
  490. }}
  491. """
  492. self._log("Injecting ReCaptcha Hook...")
  493. self.page.run_js(hook_js)
  494. def _perform_request(self, method, url, headers=None, data=None, json_data=None):
  495. """JS Fetch Wrapper"""
  496. if not self.page: raise BizLogicError("Browser not init")
  497. fetch_opts = { "method": method.upper(), "headers": headers or {}, "credentials": "include" }
  498. if json_data:
  499. fetch_opts['body'] = json.dumps(json_data)
  500. fetch_opts['headers']['Content-Type'] = 'application/json; charset=UTF-8'
  501. elif data:
  502. if isinstance(data, dict):
  503. from urllib.parse import urlencode
  504. fetch_opts['body'] = urlencode(data)
  505. fetch_opts['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
  506. else:
  507. fetch_opts['body'] = data
  508. js = f"""
  509. return fetch("{url}", {json.dumps(fetch_opts)})
  510. .then(async r => {{
  511. const h = {{}}; r.headers.forEach((v, k) => h[k] = v);
  512. return {{ status: r.status, body: await r.text(), headers: h, url: r.url }};
  513. }}).catch(e => {{ return {{ status: 0, body: e.toString() }}; }});
  514. """
  515. return BrowserResponse(self.page.run_js(js, timeout=60)) # 文件上传可能较慢,给60s
  516. def _solve_recaptcha(self, params) -> str:
  517. """
  518. 调用 YesCaptcha API 识别
  519. """
  520. client_key = params.get("apiToken")
  521. # 1. 选择任务类型
  522. # 根据文档:RecaptchaV3TaskProxylessM1S7 强制 0.7 分,适合登录
  523. task_type = "RecaptchaV3TaskProxyless" # 默认
  524. if params.get("minScore") == 0.7:
  525. task_type = "RecaptchaV3TaskProxylessM1S7"
  526. elif params.get("minScore") == 0.9:
  527. task_type = "RecaptchaV3TaskProxylessM1S9"
  528. # 2. 构造创建任务请求
  529. create_url = "https://api.yescaptcha.com/createTask"
  530. create_data = {
  531. "clientKey": client_key,
  532. "task": {
  533. "type": task_type,
  534. "websiteURL": params.get("page"),
  535. "websiteKey": params.get("siteKey"),
  536. "pageAction": params.get("action") # YesCaptcha 要求的字段名是 pageAction
  537. }
  538. }
  539. import requests as req
  540. try:
  541. # 发送创建任务请求
  542. r = req.post(create_url, json=create_data, timeout=20)
  543. if r.status_code != 200:
  544. raise BizLogicError(f"YesCaptcha Create Failed: {r.text}")
  545. res_json = r.json()
  546. if res_json.get("errorId") != 0:
  547. raise BizLogicError(f"YesCaptcha Error: {res_json.get('errorDescription')}")
  548. task_id = res_json.get("taskId")
  549. if not task_id:
  550. raise BizLogicError("YesCaptcha returned no taskId")
  551. # 3. 轮询获取结果
  552. result_url = "https://api.yescaptcha.com/getTaskResult"
  553. for _ in range(30): # 最多等 60-90秒
  554. time.sleep(3)
  555. r = req.post(result_url, json={"clientKey": client_key, "taskId": task_id}, timeout=20)
  556. d = r.json()
  557. # 识别中
  558. if d.get("status") == "processing":
  559. continue
  560. # 识别成功
  561. if d.get("status") == "ready":
  562. solution = d.get("solution", {})
  563. token = solution.get("gRecaptchaResponse")
  564. if token:
  565. return token
  566. else:
  567. raise BizLogicError("YesCaptcha ready but no token found")
  568. # 识别失败
  569. if d.get("errorId") != 0:
  570. raise BizLogicError(f"YesCaptcha Task Failed: {d.get('errorDescription')}")
  571. except Exception as e:
  572. raise BizLogicError(f"Captcha Solver Exception: {e}")
  573. raise BizLogicError("YesCaptcha timeout")
  574. def _parse_valid_days(self, text):
  575. # 提取 DateLibere (YYYY-MM-DD)
  576. # 格式: {"DateLibere":"22/10/2024 00:00:00","SlotLiberi":1,"SlotRimanenti":1}
  577. # 原始正则: r'{"DateLibere":"(.*?)","SlotLiberi":\d+,"SlotRimanenti":(-?\d+)}'
  578. days = []
  579. try:
  580. matches = re.findall(r'{"DateLibere":"(.*?)".*?"SlotRimanenti":(-?\d+)}', text)
  581. for d_str, rem in matches:
  582. if int(rem) != -1:
  583. # 22/10/2024 -> 2024-10-22
  584. dt = datetime.strptime(d_str[:10], "%d/%m/%Y")
  585. days.append(dt.strftime("%Y-%m-%d"))
  586. except: pass
  587. return days
  588. def _parse_time_slots(self, text):
  589. # 提取 IDCalendarioServizioGiornaliero, StartTime, EndTime, Remain
  590. slots = []
  591. try:
  592. # 原始逻辑比较复杂,这里简化正则
  593. # 查找 SlotRimanenti > 0 的记录
  594. # 关键是 IDCalendarioServizioGiornaliero
  595. raw_list = json.loads(text)
  596. # Prenotami 返回的是一个 JSON 列表字符串
  597. for item in raw_list:
  598. remain = item.get('SlotRimanenti', -1)
  599. if remain > 0:
  600. start = item['OrarioInizioFascia']
  601. end = item['OrarioFineFascia']
  602. s_time = f"{start['Hours']:02d}:{start['Minutes']:02d}"
  603. e_time = f"{end['Hours']:02d}:{end['Minutes']:02d}"
  604. slots.append({
  605. 'id': item['IDCalendarioServizioGiornaliero'],
  606. 'start': s_time,
  607. 'end': e_time,
  608. 'remain': remain
  609. })
  610. except: pass
  611. return slots
  612. # --- 资源清理核心方法 ---
  613. def cleanup(self):
  614. """
  615. 销毁浏览器并彻底删除临时文件
  616. """
  617. # 1. 关闭浏览器
  618. if self.page:
  619. try:
  620. self.page.quit() # 这会关闭 Chrome 进程
  621. except Exception:
  622. pass # 忽略已关闭的错误
  623. self.page = None
  624. # 2. 删除文件
  625. # 注意:Chrome 关闭后可能需要几百毫秒释放文件锁,稍微等待
  626. if os.path.exists(self.root_workspace):
  627. for _ in range(3):
  628. try:
  629. time.sleep(0.2)
  630. shutil.rmtree(self.root_workspace, ignore_errors=True)
  631. break
  632. except Exception as e:
  633. # 如果删除失败(通常是Windows文件占用),重试
  634. self._log(f"Cleanup retry: {e}")
  635. time.sleep(0.5)
  636. # 如果依然存在,打印警告(虽然 ignore_errors=True 会掩盖报错,但可以 check exists)
  637. if os.path.exists(self.root_workspace):
  638. self._log(f"[WARN] Failed to fully remove workspace: {self.root_workspace}")
  639. # 3. [新增] 关闭代理隧道
  640. if self.tunnel:
  641. try: self.tunnel.stop()
  642. except: pass
  643. self.tunnel = None
  644. def __del__(self):
  645. """
  646. 析构函数:当对象被垃圾回收时自动调用
  647. """
  648. self.cleanup()