chore(feed): Phase 2 #2 backend cleanup — dead return, dup helpers, shared const
- feed.py _filtered_query: drop the dead 2nd return element (status_expr was
used only internally for WHERE filters; all 3 callers discarded it as _status).
Now returns (query, rank_expr); fixed the stale docstring.
- youtube/client.py: extract _iter_playlist_items() — iter_my_playlist_video_ids
and iter_playlist_items_with_ids were near-identical playlistItems paging loops
(jscpd [173-187]≈[267-281]); both now map over the shared generator.
- sync/videos.py: extract _apply_video_batch() with an on_missing callback —
enrich_pending and refresh_live shared the fetch-map-apply skeleton, differing
only in the query and how they retire rows YouTube omits.
- models.py: add LIVE_OR_UPCOMING = ("live","upcoming"); replace the 4 duplicated
copies (feed.HIDDEN_LIVE, search._LIVE_HIDDEN, videos.py + channels.py inline).
Behavior-neutral. ruff clean on touched files, localdev boots, feed/worker healthy.
This commit is contained in:
parent
477056bfa8
commit
505f0e5650
6 changed files with 75 additions and 71 deletions
|
|
@ -167,9 +167,9 @@ class YouTubeClient:
|
|||
if not page_token:
|
||||
break
|
||||
|
||||
def iter_my_playlist_video_ids(self, playlist_id: str) -> Iterator[str]:
|
||||
"""Yield the video ids of one of the user's own playlists, in playlist order
|
||||
(OAuth, so private/unlisted playlists work)."""
|
||||
def _iter_playlist_items(self, playlist_id: str) -> Iterator[dict]:
|
||||
"""Paginate one of the user's playlists' items (playlistItems, contentDetails part;
|
||||
OAuth so private/unlisted work), yielding each raw item in playlist order."""
|
||||
page_token = None
|
||||
while True:
|
||||
params = {
|
||||
|
|
@ -180,14 +180,19 @@ class YouTubeClient:
|
|||
if page_token:
|
||||
params["pageToken"] = page_token
|
||||
data = self._get("playlistItems", params, cost=1, allow_key=False)
|
||||
for item in data.get("items", []):
|
||||
vid = item.get("contentDetails", {}).get("videoId")
|
||||
if vid:
|
||||
yield vid
|
||||
yield from data.get("items", [])
|
||||
page_token = data.get("nextPageToken")
|
||||
if not page_token:
|
||||
break
|
||||
|
||||
def iter_my_playlist_video_ids(self, playlist_id: str) -> Iterator[str]:
|
||||
"""Yield the video ids of one of the user's own playlists, in playlist order
|
||||
(OAuth, so private/unlisted playlists work)."""
|
||||
for item in self._iter_playlist_items(playlist_id):
|
||||
vid = item.get("contentDetails", {}).get("videoId")
|
||||
if vid:
|
||||
yield vid
|
||||
|
||||
def get_my_channel_id(self) -> str | None:
|
||||
"""The authenticated user's own channel id (channels.list?mine=true). OAuth only;
|
||||
1 quota unit. Returns None if the account has no channel."""
|
||||
|
|
@ -264,23 +269,10 @@ class YouTubeClient:
|
|||
"""Yield {item_id, video_id} for each item of one of the user's playlists, in
|
||||
playlist order. `item_id` is the playlistItem resource id, needed to delete or
|
||||
reposition the item via the write API. OAuth (private playlists work)."""
|
||||
page_token = None
|
||||
while True:
|
||||
params = {
|
||||
"part": "contentDetails",
|
||||
"playlistId": playlist_id,
|
||||
"maxResults": 50,
|
||||
}
|
||||
if page_token:
|
||||
params["pageToken"] = page_token
|
||||
data = self._get("playlistItems", params, cost=1, allow_key=False)
|
||||
for item in data.get("items", []):
|
||||
vid = item.get("contentDetails", {}).get("videoId")
|
||||
if vid:
|
||||
yield {"item_id": item.get("id"), "video_id": vid}
|
||||
page_token = data.get("nextPageToken")
|
||||
if not page_token:
|
||||
break
|
||||
for item in self._iter_playlist_items(playlist_id):
|
||||
vid = item.get("contentDetails", {}).get("videoId")
|
||||
if vid:
|
||||
yield {"item_id": item.get("id"), "video_id": vid}
|
||||
|
||||
# --- write endpoints (OAuth only, never the API key; each costs 50 quota units) ---
|
||||
def _write(self, method: str, path: str, *, params: dict, json: dict | None = None) -> dict:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue