concrete_plugin.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # plugins/concrete_plugin.py
  2. import time
  3. import random
  4. from vs_plg import IVSPlg, VSError # type: ignore
  5. from vs_types import VSPlgConfig, VSQueryResult, VSBookResult, AvailabilityStatus, QueryWaitMode # type: ignore
  6. from vs_log_macros import VSC_INFO, VSC_ERROR, VSC_DEBUG, VSC_WARN # type: ignore
  7. class ConcretePlugin(IVSPlg):
  8. """
  9. @brief 具体的签证插件实现示例
  10. 实现了 IVSPlg 接口,模拟签证查询和预订逻辑。
  11. """
  12. def __init__(self, group_id: str):
  13. self._group_id = group_id
  14. self._config: Optional[VSPlgConfig] = None
  15. self._is_healthy = True
  16. self._last_error: VSError = VSError(0, "No error")
  17. VSC_INFO("plugin", "[%s] ConcretePlugin initialized.", self._group_id)
  18. def set_config(self, config: VSPlgConfig):
  19. """
  20. @brief 设置 API 的配置信息
  21. @param config 签证 API 配置对象
  22. """
  23. self._config = config
  24. VSC_DEBUG("plugin", "[%s] Config set: Account ID=%d, Proxy IP=%s:%d",
  25. self._group_id, config.account.id, config.proxy.ip, config.proxy.port)
  26. def create_session(self) -> bool:
  27. """
  28. @brief 创建一个新的会话
  29. 模拟登录耗时,有概率失败。
  30. """
  31. VSC_INFO("plugin", "[%s] Creating session...", self._group_id)
  32. time.sleep(0.5) # 模拟耗时
  33. if random.random() < 0.1: # 10% 概率创建失败
  34. self._last_error = VSError(1001, "Session creation failed due to network error.")
  35. VSC_ERROR("plugin", "[%s] Session creation failed.", self._group_id)
  36. return False
  37. VSC_INFO("plugin", "[%s] Session created successfully.", self._group_id)
  38. return True
  39. def query(self) -> VSQueryResult:
  40. """
  41. @brief 查询可用的签证预约信息
  42. 模拟查询,有10%概率找到可预约时段。
  43. """
  44. VSC_DEBUG("plugin", "[%s] Performing query...", self._group_id)
  45. # 模拟查询耗时
  46. time.sleep(random.uniform(0.1, 0.5))
  47. result = VSQueryResult()
  48. if random.random() < 0.1: # 10% 概率找到可预约
  49. result.success = True
  50. result.availability_status = AvailabilityStatus.Available
  51. result.earliest_date = "2025-10-01"
  52. result.visa_type = "Shengen"
  53. result.city = "Paris"
  54. result.country = "France"
  55. # 模拟可用时间
  56. slot1 = VSQueryResult.DateAvailability.TimeSlot(time="09:00", label="Standard")
  57. slot2 = VSQueryResult.DateAvailability.TimeSlot(time="10:30", label="Premium")
  58. date_avail = VSQueryResult.DateAvailability(date="2025-10-01", times=[slot1, slot2])
  59. result.availability.append(date_avail)
  60. VSC_INFO("plugin", "[%s] Query found availability for %s", self._group_id, result.earliest_date)
  61. else:
  62. result.success = False
  63. self._last_error = VSError(2001, "No availability found at this time.")
  64. VSC_DEBUG("plugin", "[%s] Query found no availability.", self._group_id)
  65. return result
  66. def book(self, slot_info: VSQueryResult) -> VSBookResult:
  67. """
  68. @brief 进行预约操作
  69. 模拟预订,有90%概率成功。
  70. """
  71. VSC_INFO("plugin", "[%s] Attempting to book based on query result...", self._group_id)
  72. time.sleep(random.uniform(1.0, 2.0)) # 模拟预订耗时
  73. book_result = VSBookResult()
  74. if random.random() < 0.9: # 90% 概率预订成功
  75. book_result.success = True
  76. book_result.order_id = f"ORD-{self._group_id}-{random.randint(10000, 99999)}"
  77. book_result.session_id = "SESSION-XYZ"
  78. book_result.account = self._config.account.username if self._config else "N/A"
  79. book_result.visa_type = slot_info.visa_type
  80. book_result.city = slot_info.city
  81. book_result.country = slot_info.country
  82. book_result.book_date = slot_info.earliest_date
  83. book_result.book_time = slot_info.availability[0].times[0].time if slot_info.availability else "N/A"
  84. book_result.fee_amount = 10000 # 100 EUR
  85. book_result.fee_currency = "EUR"
  86. book_result.payment_link = "https://example.com/payment/xyz"
  87. VSC_INFO("plugin", "[%s] Booking successful! Order ID: %s", self._group_id, book_result.order_id)
  88. else:
  89. book_result.success = False
  90. self._last_error = VSError(3001, "Booking failed due to concurrent access or payment issue.")
  91. VSC_ERROR("plugin", "[%s] Booking failed.", self._group_id)
  92. return book_result
  93. def get_group_id(self) -> str:
  94. """
  95. @brief 获取当前 API 实例所属的分组 ID
  96. """
  97. return self._group_id
  98. def health_check(self) -> bool:
  99. """
  100. @brief 健康检查,模拟1%的概率变为不健康状态
  101. """
  102. if random.random() < 0.01: # 1% 概率实例变得不健康
  103. self._is_healthy = False
  104. self._last_error = VSError(4001, "Instance became unhealthy unexpectedly.")
  105. VSC_WARN("plugin", "[%s] Instance is now unhealthy.", self._group_id)
  106. return self._is_healthy
  107. def get_last_error(self) -> VSError:
  108. """
  109. @brief 获取最近一次操作的错误信息
  110. """
  111. return self._last_error