concrete_plugin.py 5.4 KB

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