base_platform.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """Platform base types for zhuce6."""
  2. from __future__ import annotations
  3. from abc import ABC, abstractmethod
  4. from dataclasses import dataclass, field
  5. from enum import Enum
  6. from pathlib import Path
  7. from typing import Any
  8. import time
  9. class AccountStatus(str, Enum):
  10. REGISTERED = "registered"
  11. TRIAL = "trial"
  12. SUBSCRIBED = "subscribed"
  13. EXPIRED = "expired"
  14. INVALID = "invalid"
  15. @dataclass
  16. class Account:
  17. platform: str
  18. email: str
  19. password: str
  20. user_id: str = ""
  21. region: str = ""
  22. token: str = ""
  23. status: AccountStatus = AccountStatus.REGISTERED
  24. trial_end_time: int = 0
  25. extra: dict[str, Any] = field(default_factory=dict)
  26. created_at: int = field(default_factory=lambda: int(time.time()))
  27. @dataclass
  28. class RegisterConfig:
  29. executor_type: str = "protocol"
  30. captcha_solver: str = "manual"
  31. proxy: str | None = None
  32. extra: dict[str, Any] = field(default_factory=dict)
  33. class BasePlatform(ABC):
  34. name: str = ""
  35. display_name: str = ""
  36. version: str = "1.0.0"
  37. def __init__(self, config: RegisterConfig | None = None) -> None:
  38. self.config = config or RegisterConfig()
  39. @abstractmethod
  40. def register(self, email: str | None = None, password: str | None = None) -> Account:
  41. """Execute the platform registration flow."""
  42. @abstractmethod
  43. def check_valid(self, account: Account) -> bool:
  44. """Check whether the account is currently valid."""
  45. def run_preflight(self, email: str | None = None, password: str | None = None) -> dict[str, Any]:
  46. del email, password
  47. raise NotImplementedError(f"Platform {self.name} does not expose a preflight flow")
  48. def exchange_callback(
  49. self,
  50. callback_url: str,
  51. expected_state: str,
  52. code_verifier: str,
  53. *,
  54. write_pool: bool = True,
  55. pool_dir: Path | None = None,
  56. ) -> dict[str, Any]:
  57. del callback_url, expected_state, code_verifier, write_pool, pool_dir
  58. raise NotImplementedError(f"Platform {self.name} does not expose a callback exchange flow")
  59. def get_platform_actions(self) -> list[dict[str, Any]]:
  60. return []
  61. def execute_action(self, action_id: str, account: Account, params: dict[str, Any]) -> dict[str, Any]:
  62. raise NotImplementedError(f"Platform {self.name} does not support action: {action_id}")