bel_plugin.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import time
  2. import json
  3. import random
  4. import re
  5. import os
  6. import uuid
  7. from datetime import datetime
  8. from typing import List, Dict, Optional, Any, Callable
  9. from DrissionPage import ChromiumPage
  10. from vs_plg import IVSPlg
  11. from vs_types import (
  12. VSPlgConfig,
  13. AppointmentType,
  14. VSQueryResult,
  15. VSBookResult,
  16. AvailabilityStatus,
  17. TimeSlot,
  18. DateAvailability,
  19. NotFoundError,
  20. PermissionDeniedError,
  21. RateLimiteddError,
  22. SessionExpiredOrInvalidError,
  23. BizLogicError,
  24. PluginStatus,
  25. utc_now_str
  26. )
  27. from utils.plugin_utils import (
  28. BrowserResponse,
  29. init_chromium_options,
  30. perform_browser_fetch,
  31. filter_dates_in_range,
  32. solve_capsolver_hcaptcha,
  33. cleanup_browser_workspace
  34. )
  35. class BelPlugin(IVSPlg):
  36. """
  37. Belgium (https://visaonweb.diplomatie.be/en) 签证预约插件 (Browser + Tunnel Mode)
  38. """
  39. name = "bel_plugin"
  40. version = "1.0.0"
  41. description = "比利时签证预约插件 (Visa On Web / 浏览器与隧道模式)"
  42. def __init__(self, group_id: str):
  43. self.group_id = group_id
  44. self.config: Optional[VSPlgConfig] = None
  45. self.free_config: Dict[str, Any] = {}
  46. self.logger = None
  47. # 浏览器实例
  48. self.page: Optional[ChromiumPage] = None
  49. # 资源隔离
  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. self.user_data_path = os.path.join(self.root_workspace, "user_data")
  53. if not os.path.exists(self.root_workspace):
  54. os.makedirs(self.root_workspace)
  55. self.applicants = []
  56. self.tunnel = None # 代理隧道
  57. self.is_healthy = True
  58. self.session_create_time: float = 0
  59. def set_log(self, logger: Callable[[str], None]) -> None:
  60. self.logger = logger
  61. def _log(self, message):
  62. if self.logger:
  63. self.logger(f'[BelPlugin] [{self.group_id}] [{self.instance_id}] {message}')
  64. else:
  65. print(f'[BelPlugin] [{self.group_id}] [{self.instance_id}] {message}')
  66. def set_config(self, config: VSPlgConfig):
  67. self.config = config
  68. self.free_config = config.free_config or {}
  69. def keep_alive(self):
  70. pass
  71. def health_check(self) -> bool:
  72. if not self.is_healthy or not self.page:
  73. return False
  74. try:
  75. if not self.page.run_js("return 1;"):
  76. return False
  77. except Exception:
  78. return False
  79. if self.config.session_max_life > 0:
  80. if time.time() - self.session_create_time > self.config.session_max_life:
  81. self._log("Session expired.")
  82. return False
  83. return True
  84. def create_session(self):
  85. """
  86. 创建会话:启动浏览器 -> 代理隧道 -> 登录 -> 获取申请人列表
  87. """
  88. self._log(f"Initializing Session (ID: {self.instance_id})...")
  89. co, self.tunnel = init_chromium_options(
  90. user_data_path=self.user_data_path,
  91. username_seed=self.config.account.username if self.config and self.config.account else None,
  92. proxy=self.config.proxy if self.config else None,
  93. headless=False,
  94. window_size="1920,1080",
  95. logger=self._log
  96. )
  97. co.set_argument('--ignore-certificate-errors')
  98. try:
  99. self.page = ChromiumPage(co)
  100. url_home = "https://visaonweb.diplomatie.be/Account/Login?ReturnUrl=%2Fen"
  101. self._log(f"Navigating to {url_home}")
  102. self.page.get(url_home)
  103. self.page.wait.doc_loaded()
  104. self.page.wait.ele_displayed('tag:button@@type=submit', timeout=10)
  105. self.page.ele('tag:input@@name=UserName').input(self.config.account.username)
  106. self.page.ele('tag:input@@name=Password').input(self.config.account.password)
  107. self.page.ele('tag:button@@type=submit').click()
  108. self.page.listen.start('/VisaApplication/MyList')
  109. self.page.get('https://visaonweb.diplomatie.be/en/VisaApplication/IndexByUserId')
  110. packet = self.page.listen.wait(timeout=10)
  111. if not packet:
  112. raise BizLogicError(message='Get userList failed')
  113. self.page.listen.stop()
  114. resp_body = packet.response.body
  115. for dat in resp_body.get('data', []):
  116. if dat.get('St') == 'Submitted':
  117. applicant = {
  118. "id": dat.get('Id'),
  119. "first_name": dat.get('FName'),
  120. "last_name": dat.get('LName'),
  121. "vow_id": dat.get('VOWId'),
  122. "os_id": dat.get('OSId'),
  123. "sub_group_id": dat.get('SubGroupId'),
  124. "vac": dat.get('Vac'),
  125. }
  126. self.applicants.append(applicant)
  127. self.session_create_time = time.time()
  128. self._log("Session created successfully.")
  129. except Exception as e:
  130. self._log(f"Session Create Failed: {e}")
  131. self.cleanup()
  132. raise e
  133. def query(self, apt_type: AppointmentType) -> VSQueryResult:
  134. res = VSQueryResult()
  135. res.success = False
  136. if not self.applicants:
  137. self._log("No submitted applicants available to query appointment.")
  138. res.availability_status = AvailabilityStatus.NoneAvailable
  139. return res
  140. applicant = random.choice(self.applicants)
  141. applicant_id = applicant.get('id')
  142. url = 'https://visaonweb.diplomatie.be/Common/GetEAppointmentUrl'
  143. params = {
  144. 'id': applicant_id
  145. }
  146. headers = {
  147. "cache-control": "max-age=0",
  148. "if-modified-since": "0",
  149. "x-requested-with": "XMLHttpRequest",
  150. }
  151. resp = self._perform_request('GET', url, headers=headers, params=params)
  152. appointment_url = resp.json().get('url')
  153. if not appointment_url:
  154. self._log("GetEAppointmentUrl returned empty URL.")
  155. res.availability_status = AvailabilityStatus.NoneAvailable
  156. return res
  157. tab = self.page.new_tab(appointment_url)
  158. try:
  159. tab.wait.doc_loaded()
  160. site_key = '5f64399c-14a8-415e-ad1a-7ebccdc4943a'
  161. capsolver_key = self.free_config.get('capsolver_key') or os.getenv('CAPSOLVER_KEY', '')
  162. token = self.get_captcha_solution(capsolver_key, site_key, appointment_url)
  163. inject_script = f"""
  164. (function() {{
  165. var token = "{token}";
  166. var textareas = [
  167. document.querySelector('textarea[name="h-captcha-response"]'),
  168. document.querySelector('textarea[name="g-recaptcha-response"]'),
  169. document.querySelector('[name="h-captcha-response"]'),
  170. document.querySelector('[name="g-recaptcha-response"]')
  171. ];
  172. textareas.forEach(function(el) {{
  173. if (el) {{
  174. el.value = token;
  175. el.innerHTML = token;
  176. el.dispatchEvent(new Event('input', {{ bubbles: true }}));
  177. el.dispatchEvent(new Event('change', {{ bubbles: true }}));
  178. }}
  179. }});
  180. if (typeof window.hcaptcha !== 'undefined' && typeof window.hcaptcha.setData === 'function') {{
  181. try {{ window.hcaptcha.setData(null, {{ response: token }}); }} catch(e) {{}}
  182. }}
  183. if (typeof window.onHCaptchaSuccess === 'function') {{
  184. window.onHCaptchaSuccess(token);
  185. }}
  186. }})();
  187. """
  188. tab.run_js(inject_script)
  189. time.sleep(2)
  190. btn = tab.ele('tag:button@@type=submit', timeout=2) or tab.ele('tag:input@@type=submit', timeout=2)
  191. if btn:
  192. btn.click()
  193. tab.wait.doc_loaded()
  194. time.sleep(3)
  195. self._log(f"Page title after hCaptcha submit: {tab.title}")
  196. available_dates = []
  197. date_eles = tab.eles('.available-date, .calendar-day-available, [data-date]')
  198. for d_ele in date_eles:
  199. d_str = d_ele.attr('data-date') or d_ele.text
  200. if d_str and re.match(r'^\d{4}-\d{2}-\d{2}$', d_str.strip()):
  201. available_dates.append(d_str.strip())
  202. if available_dates:
  203. res.success = True
  204. res.availability_status = AvailabilityStatus.Available
  205. earliest_date = available_dates[0]
  206. earliest_dt = datetime.strptime(earliest_date, "%Y-%m-%d")
  207. res.earliest_date = earliest_dt
  208. res.availability = [
  209. DateAvailability(
  210. date=datetime.strptime(d, "%Y-%m-%d"),
  211. times=[],
  212. )
  213. for d in available_dates
  214. ]
  215. else:
  216. res.success = False
  217. res.availability_status = AvailabilityStatus.NoneAvailable
  218. res.availability = []
  219. finally:
  220. try:
  221. tab.close()
  222. except Exception:
  223. pass
  224. return res
  225. def get_captcha_solution(self, api_key: str, site_key: str, site_url: str) -> str:
  226. """
  227. 调用 Capsolver 解决 hCaptcha (HCaptchaTaskProxyLess)
  228. """
  229. if not api_key:
  230. api_key = self.free_config.get('capsolver_key') or os.getenv('CAPSOLVER_KEY', '')
  231. return solve_capsolver_hcaptcha(
  232. api_token=api_key,
  233. site_key=site_key,
  234. page_url=site_url,
  235. proxy=self.config.proxy if self.config else None,
  236. logger=self._log
  237. )
  238. def book(self, slot_info: VSQueryResult, user_inputs: Dict = None) -> VSBookResult:
  239. res = VSBookResult()
  240. return res
  241. def _perform_request(self, method, url, headers=None, data=None, json_data=None, params=None, retry_count=0):
  242. """在浏览器上下文中注入执行 Fetch"""
  243. if not self.page:
  244. raise BizLogicError("Browser not initialized")
  245. return perform_browser_fetch(
  246. self.page,
  247. method=method,
  248. url=url,
  249. headers=headers,
  250. data=data,
  251. json_data=json_data,
  252. params=params
  253. )
  254. def cleanup(self):
  255. """销毁浏览器并彻底删除临时文件"""
  256. cleanup_browser_workspace(self.page, self.root_workspace, self.tunnel, self._log)
  257. self.page = None
  258. self.tunnel = None
  259. def get_plugin_status(self) -> PluginStatus:
  260. account_name = self.config.account.username if self.config and self.config.account else ""
  261. proxy_info = f"{self.config.proxy.ip}:{self.config.proxy.port}" if self.config and self.config.proxy and self.config.proxy.ip else ""
  262. current_url = ""
  263. if self.page:
  264. try:
  265. current_url = self.page.url
  266. except Exception:
  267. pass
  268. session_age = int(time.time() - self.session_create_time) if getattr(self, "session_create_time", 0) > 0 else 0
  269. state = "running" if getattr(self, "is_healthy", True) else "unhealthy"
  270. return PluginStatus(
  271. instance_id=self.instance_id,
  272. is_healthy=self.is_healthy,
  273. state=state,
  274. current_url=current_url,
  275. account=account_name,
  276. proxy=proxy_info,
  277. session_age_sec=session_age,
  278. last_active_at=utc_now_str(),
  279. )
  280. def __del__(self):
  281. """
  282. 析构函数:当对象被垃圾回收时自动调用
  283. """
  284. self.cleanup()