| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- # plugins/concrete_plugin.py
- import time
- import random
- from typing import Dict, List, Optional, Any
- from vs_plg import IVSPlg, VSError # type: ignore
- from vs_types import VSPlgConfig, VSQueryResult, VSBookResult, AvailabilityStatus, QueryWaitMode # type: ignore
- from vs_log_macros import VSC_INFO, VSC_ERROR, VSC_DEBUG, VSC_WARN # type: ignore
- class ConcretePlugin(IVSPlg):
- """
- @brief 具体的签证插件实现示例
- 实现了 IVSPlg 接口,模拟签证查询和预订逻辑。
- """
- def __init__(self, group_id: str):
- self._group_id = group_id
- self._config: Optional[VSPlgConfig] = None
- self._is_healthy = True
- self._last_error: VSError = VSError(0, "No error")
- VSC_INFO("plugin", "[%s] ConcretePlugin initialized.", self._group_id)
- def set_config(self, config: VSPlgConfig):
- """
- @brief 设置 API 的配置信息
- @param config 签证 API 配置对象
- """
- self._config = config
- VSC_DEBUG("plugin", "[%s] Config set: Account ID=%d, Proxy IP=%s:%d",
- self._group_id, config.account.id, config.proxy.ip, config.proxy.port)
- def create_session(self) -> bool:
- """
- @brief 创建一个新的会话
- 模拟登录耗时,有概率失败。
- """
- VSC_INFO("plugin", "[%s] Creating session...", self._group_id)
- time.sleep(0.5) # 模拟耗时
- if random.random() < 0.1: # 10% 概率创建失败
- self._last_error = VSError(1001, "Session creation failed due to network error.")
- VSC_ERROR("plugin", "[%s] Session creation failed.", self._group_id)
- return False
-
- VSC_INFO("plugin", "[%s] Session created successfully.", self._group_id)
- return True
- def query(self) -> VSQueryResult:
- """
- @brief 查询可用的签证预约信息
- 模拟查询,有10%概率找到可预约时段。
- """
- VSC_DEBUG("plugin", "[%s] Performing query...", self._group_id)
- # 模拟查询耗时
- time.sleep(random.uniform(0.1, 0.5))
- result = VSQueryResult()
- if random.random() < 0.1: # 10% 概率找到可预约
- result.success = True
- result.availability_status = AvailabilityStatus.Available
- result.earliest_date = "2025-10-01"
- result.visa_type = "Shengen"
- result.city = "Paris"
- result.country = "France"
-
- # 模拟可用时间
- slot1 = VSQueryResult.DateAvailability.TimeSlot(time="09:00", label="Standard")
- slot2 = VSQueryResult.DateAvailability.TimeSlot(time="10:30", label="Premium")
- date_avail = VSQueryResult.DateAvailability(date="2025-10-01", times=[slot1, slot2])
- result.availability.append(date_avail)
-
- VSC_INFO("plugin", "[%s] Query found availability for %s", self._group_id, result.earliest_date)
- else:
- result.success = False
- self._last_error = VSError(2001, "No availability found at this time.")
- VSC_DEBUG("plugin", "[%s] Query found no availability.", self._group_id)
-
- return result
- def book(self, slot_info: VSQueryResult) -> VSBookResult:
- """
- @brief 进行预约操作
- 模拟预订,有90%概率成功。
- """
- VSC_INFO("plugin", "[%s] Attempting to book based on query result...", self._group_id)
- time.sleep(random.uniform(1.0, 2.0)) # 模拟预订耗时
- book_result = VSBookResult()
- if random.random() < 0.9: # 90% 概率预订成功
- book_result.success = True
- book_result.order_id = f"ORD-{self._group_id}-{random.randint(10000, 99999)}"
- book_result.session_id = "SESSION-XYZ"
- book_result.account = self._config.account.username if self._config else "N/A"
- book_result.visa_type = slot_info.visa_type
- book_result.city = slot_info.city
- book_result.country = slot_info.country
- book_result.book_date = slot_info.earliest_date
- book_result.book_time = slot_info.availability[0].times[0].time if slot_info.availability else "N/A"
- book_result.fee_amount = 10000 # 100 EUR
- book_result.fee_currency = "EUR"
- book_result.payment_link = "https://example.com/payment/xyz"
- VSC_INFO("plugin", "[%s] Booking successful! Order ID: %s", self._group_id, book_result.order_id)
- else:
- book_result.success = False
- self._last_error = VSError(3001, "Booking failed due to concurrent access or payment issue.")
- VSC_ERROR("plugin", "[%s] Booking failed.", self._group_id)
-
- return book_result
- def get_group_id(self) -> str:
- """
- @brief 获取当前 API 实例所属的分组 ID
- """
- return self._group_id
- def health_check(self) -> bool:
- """
- @brief 健康检查,模拟1%的概率变为不健康状态
- """
- if random.random() < 0.01: # 1% 概率实例变得不健康
- self._is_healthy = False
- self._last_error = VSError(4001, "Instance became unhealthy unexpectedly.")
- VSC_WARN("plugin", "[%s] Instance is now unhealthy.", self._group_id)
- return self._is_healthy
- def get_last_error(self) -> VSError:
- """
- @brief 获取最近一次操作的错误信息
- """
- return self._last_error
|