feat(up): capture follower snapshots at ingest and backfill history
This commit is contained in:
1 parent
4942d1d161
commit
519fc39fca
8 files changed
+401
-14
No files matched your search
@@ -21,6 +21,7 @@ from typing import Any
|
||||
|
||||
from ..config import AppConfig, ConfigStore, FolderConfig
|
||||
from ..repository import Repository
|
||||
from .up_profile import UpProfileService
|
||||
|
||||
CAPACITY_WARN = 950
|
||||
|
||||
@@ -61,6 +62,8 @@ class SyncSummary:
|
||||
finished_at: int = 0
|
||||
folders: list[FolderSyncResult] = field(default_factory=list)
|
||||
blacklist_seeded: int = 0
|
||||
followers_updated: int = 0
|
||||
followers_failed: int = 0
|
||||
|
||||
@property
|
||||
def added(self) -> int:
|
||||
@@ -100,6 +103,8 @@ class SyncSummary:
|
||||
"refreshed": self.refreshed,
|
||||
"missing": self.missing,
|
||||
"blacklist_seeded": self.blacklist_seeded,
|
||||
"followers_updated": self.followers_updated,
|
||||
"followers_failed": self.followers_failed,
|
||||
"folders": [f.as_dict() for f in self.folders],
|
||||
"errors": self.errors,
|
||||
"warnings": self.warnings,
|
||||
@@ -119,6 +124,8 @@ class FolderSyncService:
|
||||
logger=None,
|
||||
capacity_warn: int = CAPACITY_WARN,
|
||||
allow_empty_folders: bool = False,
|
||||
up_profile_service: UpProfileService | None = None,
|
||||
capture_followers: bool = True,
|
||||
) -> None:
|
||||
if client is None and client_provider is None:
|
||||
raise ValueError("必须提供 client 或 client_provider")
|
||||
@@ -130,6 +137,10 @@ class FolderSyncService:
|
||||
self.logger = logger
|
||||
self.capacity_warn = capacity_warn
|
||||
self.allow_empty_folders = allow_empty_folders
|
||||
self.capture_followers = capture_followers
|
||||
self.up_profile = up_profile_service or UpProfileService(
|
||||
repository, self._client, clock=clock, logger=logger
|
||||
)
|
||||
|
||||
def _client(self):
|
||||
if self.client_provider is not None:
|
||||
@@ -189,6 +200,26 @@ class FolderSyncService:
|
||||
)
|
||||
summary.folders.append(result)
|
||||
|
||||
if self.capture_followers:
|
||||
try:
|
||||
missing = self.repo.up_mids_missing_follower()
|
||||
if missing:
|
||||
capture = self.up_profile.capture(
|
||||
[int(row["up_mid"]) for row in missing],
|
||||
names={
|
||||
int(row["up_mid"]): str(row.get("up_name") or "")
|
||||
for row in missing
|
||||
},
|
||||
live_since=summary.started_at - 3600,
|
||||
)
|
||||
summary.followers_updated = capture.updated_videos
|
||||
summary.followers_failed = capture.failed
|
||||
except Exception as exc: # noqa: BLE001 粉丝采集失败不影响收藏夹同步
|
||||
summary.followers_failed += 1
|
||||
self._log(
|
||||
"warning", "folder_sync.followers_failed", error=str(exc)
|
||||
)
|
||||
|
||||
summary.finished_at = self._now()
|
||||
self._log(
|
||||
"info" if summary.ok else "error",
|
||||
@@ -200,7 +231,7 @@ class FolderSyncService:
|
||||
errors=len(summary.errors),
|
||||
elapsed=max(0, summary.finished_at - summary.started_at),
|
||||
)
|
||||
if summary.added or summary.removed or summary.refreshed:
|
||||
if summary.added or summary.removed or summary.refreshed or summary.followers_updated:
|
||||
self.repo.bump_cache_version("videos")
|
||||
return summary
|
||||
|
||||
|
||||
Reference in new issue
Block a user