ita_plugin.py 31 KB

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