base.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """Formal platform adapter contracts for acquisition."""
  2. from __future__ import annotations
  3. from typing import Any, Iterable, Protocol
  4. from pydantic import BaseModel, ConfigDict, Field
  5. from core.config import Settings
  6. class PlatformCandidate(BaseModel):
  7. model_config = ConfigDict(extra="forbid")
  8. rank: int
  9. platform: str
  10. provider: str = ""
  11. source_id: str = ""
  12. url: str = ""
  13. title: str = ""
  14. author: str = ""
  15. cover_url: str = ""
  16. raw: dict[str, Any] = Field(default_factory=dict)
  17. class PlatformItem(BaseModel):
  18. model_config = ConfigDict(extra="forbid")
  19. platform: str
  20. provider: str = ""
  21. source_id: str = ""
  22. url: str = ""
  23. content_type: str = ""
  24. content_mode: str = ""
  25. title: str = ""
  26. author: str = ""
  27. body_text: str = ""
  28. image_urls: list[str] = Field(default_factory=list)
  29. video_urls: list[str] = Field(default_factory=list)
  30. raw: dict[str, Any] = Field(default_factory=dict)
  31. class PlatformSearchPage(BaseModel):
  32. model_config = ConfigDict(extra="forbid")
  33. page_index: int
  34. candidates: list[PlatformCandidate] = Field(default_factory=list)
  35. raw_count: int = 0
  36. cursor: str = ""
  37. next_cursor: str = ""
  38. has_more: bool = False
  39. provider: str = ""
  40. request_payload: dict[str, Any] = Field(default_factory=dict)
  41. raw_metadata: dict[str, Any] = Field(default_factory=dict)
  42. class PlatformAdapter(Protocol):
  43. platform: str
  44. def search_pages(
  45. self,
  46. query: str,
  47. *,
  48. settings: Settings,
  49. limit: int,
  50. rate_limiter: Any,
  51. max_pages: int = 2,
  52. ) -> Iterable[PlatformSearchPage]:
  53. ...
  54. def search(
  55. self,
  56. query: str,
  57. *,
  58. settings: Settings,
  59. limit: int,
  60. rate_limiter: Any,
  61. ) -> list[PlatformCandidate]:
  62. ...
  63. def fetch_detail(
  64. self,
  65. candidate: PlatformCandidate,
  66. *,
  67. settings: Settings,
  68. rate_limiter: Any,
  69. ) -> PlatformItem:
  70. ...