Repository navigation
Expand file tree
/
Copy pathfeed_fetcher.py
More file actions
816 lines (660 loc) · 30.1 KB
/
Copy pathfeed_fetcher.py
File metadata and controls
816 lines (660 loc) · 30.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
"""Background feed fetcher for subscribed channels."""
import asyncio
import json
import logging
import sqlite3
from datetime import datetime
from typing import Any, Dict, List, Optional
import httpx
import avatar_cache
import database
import innertube
import invidious_proxy
from converters import parse_relative_time, resolve_invidious_url
from security import is_safe_url_strict
from settings import get_settings
from ytdlp_wrapper import YtDlpError, get_channel_info, is_valid_url, run_ytdlp
logger = logging.getLogger(__name__)
_fetch_task: Optional[asyncio.Task] = None
_FEED_FETCH_ERRORS = (
YtDlpError,
invidious_proxy.InvidiousProxyError,
httpx.RequestError,
json.JSONDecodeError,
sqlite3.Error,
OSError,
ValueError,
KeyError,
)
def _build_channel_url(channel_id: str, site: str, channel_url: str) -> str:
"""Build the URL for fetching channel videos.
Args:
channel_id: The channel ID
site: The site/platform name
channel_url: The stored channel URL
Returns:
URL string for fetching
Raises:
ValueError: If channel_url is required but not provided, or if URL is invalid
"""
if site.lower() == "youtube":
if channel_id.startswith("@"):
return f"https://www.youtube.com/{channel_id}/videos"
elif channel_id.startswith("UC"):
return f"https://www.youtube.com/channel/{channel_id}/videos"
# For non-YouTube sites, channel_url is required
if not channel_url:
raise ValueError(f"channel_url is required for {site} channels (channel_id: {channel_id})")
# Validate user-provided URL to prevent command injection
if not is_valid_url(channel_url):
raise ValueError(f"Invalid channel URL format: {channel_url}")
# SSRF prevention - validate URL doesn't target internal resources
# DNS resolution enabled to prevent DNS rebinding attacks
is_safe, reason = is_safe_url_strict(channel_url, resolve_dns=True)
if not is_safe:
raise ValueError(f"Channel URL blocked ({reason}): {channel_url}")
return channel_url
def _process_invidious_video(v: dict, channel_id: str, invidious_base: str) -> dict:
"""Convert a single Invidious video to our cached format.
Args:
v: Invidious video data
channel_id: The channel ID
invidious_base: Invidious instance base URL for resolving thumbnails
Returns:
Video dict in our format
"""
# Resolve relative thumbnail URLs before processing
raw_thumbnails = v.get("videoThumbnails", [])
resolved_thumbnails = []
for thumb in raw_thumbnails:
resolved_thumb = thumb.copy()
resolved_thumb["url"] = resolve_invidious_url(thumb.get("url", ""), invidious_base)
resolved_thumbnails.append(resolved_thumb)
# Extract all thumbnails and best URL
best_thumb_url, all_thumbnails = _get_all_thumbnails(resolved_thumbnails)
return {
"video_id": v.get("videoId", ""),
"title": v.get("title", ""),
"author": v.get("author", ""),
"author_id": v.get("authorId", channel_id),
"length_seconds": v.get("lengthSeconds", 0),
"view_count": v.get("viewCount"),
"published": v.get("published"),
"published_text": v.get("publishedText", ""),
"thumbnail_url": best_thumb_url,
"thumbnails": all_thumbnails,
"video_url": f"https://www.youtube.com/watch?v={v.get('videoId', '')}",
}
def _process_ytdlp_video(info: dict, channel_id: str) -> dict:
"""Convert a single yt-dlp video to our cached format.
Args:
info: yt-dlp video info dict
channel_id: The channel ID
Returns:
Video dict in our format
"""
best_thumb_url, all_thumbnails = _get_all_ytdlp_thumbnails(info)
return {
"video_id": info.get("id", ""),
"title": info.get("title", ""),
"author": info.get("channel") or info.get("uploader") or "",
"author_id": info.get("channel_id") or info.get("uploader_id") or channel_id,
"length_seconds": info.get("duration") or 0,
"view_count": info.get("view_count"),
"published": _parse_timestamp(info.get("timestamp") or info.get("upload_date")),
"published_text": info.get("upload_date") or "",
"thumbnail_url": best_thumb_url,
"thumbnails": all_thumbnails,
"video_url": info.get("url") or info.get("webpage_url") or "",
}
def _parse_relative_time(text: str) -> Optional[int]:
"""Parse relative time text like '2 days ago' into an approximate Unix timestamp.
Returns None if the text can't be parsed.
"""
return parse_relative_time(text)
def _process_innertube_video(v: dict, channel_id: str) -> dict:
"""Convert an InnerTube video (Invidious-compatible format) to our cached format.
Args:
v: InnerTube video data (from video_renderer_to_invidious)
channel_id: The channel ID
Returns:
Video dict in our format
"""
# InnerTube thumbnails are already absolute URLs
raw_thumbnails = v.get("videoThumbnails", [])
best_thumb_url, all_thumbnails = _get_all_thumbnails(raw_thumbnails)
return {
"video_id": v.get("videoId", ""),
"title": v.get("title", ""),
"author": v.get("author", ""),
"author_id": v.get("authorId", channel_id),
"length_seconds": v.get("lengthSeconds", 0),
"view_count": v.get("viewCount"),
"published": _parse_relative_time(v.get("publishedText", "")),
"published_text": v.get("publishedText", ""),
"thumbnail_url": best_thumb_url,
"thumbnails": all_thumbnails,
"video_url": f"https://www.youtube.com/watch?v={v.get('videoId', '')}",
}
async def _fetch_channel_metadata_innertube(channel_id: str) -> Optional[Dict[str, Any]]:
"""Fetch channel metadata from InnerTube.
Args:
channel_id: The channel ID
Returns:
Dict with subscriber_count and is_verified, or None
"""
try:
channel_info = await innertube.get_channel_info(channel_id)
if channel_info:
return {
"subscriber_count": channel_info.get("subCount"),
"is_verified": channel_info.get("authorVerified", False),
}
except innertube.InnerTubeError as e:
logger.debug(f"[Feed] InnerTube channel metadata failed for {channel_id}: {e}")
except (KeyError, TypeError, ValueError) as e:
logger.debug(f"[Feed] InnerTube channel metadata failed for {channel_id}: {e}")
return None
async def _fetch_from_innertube(
channel_id: str, max_videos: int
) -> tuple[Optional[List[dict]], Optional[Dict[str, Any]]]:
"""Fetch videos from InnerTube API.
Args:
channel_id: The YouTube channel ID
max_videos: Maximum number of videos to fetch
Returns:
Tuple of (videos_list or None, channel_metadata or None)
"""
try:
# Fetch channel info first to get channel name (videos on channel tab lack author info)
channel_info = None
channel_name = ""
try:
channel_info = await innertube.get_channel_info(channel_id)
if channel_info:
channel_name = channel_info.get("author", "")
except innertube.InnerTubeError:
pass
all_videos = []
continuation = None
page_count = 0
while len(all_videos) < max_videos:
page_count += 1
data = await innertube.get_channel_videos(channel_id, continuation)
if not data or not data.get("videos"):
break
for v in data["videos"]:
# Fill in author/authorId if missing (channel tab videos don't include them)
if not v.get("author"):
v["author"] = channel_name
if not v.get("authorId"):
v["authorId"] = channel_id
all_videos.append(_process_innertube_video(v, channel_id))
continuation = data.get("continuation")
if not continuation:
break
if not all_videos:
return None, None
all_videos = all_videos[:max_videos]
logger.info(f"[Feed] InnerTube: fetched {len(all_videos)} videos for {channel_id} ({page_count} pages)")
# Build channel metadata from already-fetched channel info
channel_metadata = None
if channel_info:
channel_metadata = {
"subscriber_count": channel_info.get("subCount"),
"is_verified": channel_info.get("authorVerified", False),
}
return all_videos, channel_metadata
except innertube.InnerTubeError as e:
logger.info(f"[Feed] InnerTube failed for {channel_id}: {e}")
return None, None
except (KeyError, TypeError, ValueError) as e:
logger.debug(f"[Feed] InnerTube failed for {channel_id}: {e}")
return None, None
async def _fetch_channel_metadata_invidious(channel_id: str) -> Optional[Dict[str, Any]]:
"""Fetch channel metadata from Invidious.
Args:
channel_id: The channel ID
Returns:
Dict with subscriber_count and is_verified, or None
"""
try:
channel_info = await invidious_proxy.get_channel(channel_id)
if channel_info:
return {
"subscriber_count": channel_info.get("subCount"),
"is_verified": channel_info.get("authorVerified", False),
}
except (KeyError, TypeError, ValueError) as e:
logger.debug(f"Failed to fetch channel info for {channel_id}: {e}")
return None
async def _fetch_channel_metadata_ytdlp(channel_id: str) -> Optional[Dict[str, Any]]:
"""Fetch channel metadata from yt-dlp.
Args:
channel_id: The channel ID
Returns:
Dict with subscriber_count and is_verified, or None
"""
try:
info = await get_channel_info(channel_id)
return {
"subscriber_count": info.get("channel_follower_count"),
"is_verified": info.get("channel_is_verified", False),
}
except (YtDlpError, KeyError, TypeError, ValueError) as e:
logger.debug(f"Failed to fetch channel info for {channel_id}: {e}")
return None
async def _fetch_from_invidious(
channel_id: str, max_videos: int
) -> tuple[Optional[List[dict]], Optional[Dict[str, Any]], bool, Optional[str]]:
"""Fetch videos from Invidious API.
Args:
channel_id: The YouTube channel ID
max_videos: Maximum number of videos to fetch
Returns:
Tuple of (videos_list or None, pagination_info or None, should_fallback_to_ytdlp, fallback_reason or None)
"""
try:
result = await invidious_proxy.get_channel_videos_multi_page(channel_id, max_videos)
if not result or not result.get("videos"):
return None, None, True, "no_videos"
s = get_settings()
# Check if we hit 414 AND fallback is enabled
if (
result.get("pagination_limited")
and result.get("limit_reason") == "414_error"
and s.feed_fallback_ytdlp_on_414
):
logger.info(
f"[Feed] {channel_id}: 414 error with {result.get('total_fetched')} videos, "
f"falling back to yt-dlp for full fetch"
)
return None, None, True, "invidious_error_414"
# Process Invidious results
raw_video_list = result["videos"]
# Filter out entries Invidious can't represent as a video. Two known modes:
# - parse-error placeholders: {"type": "parse-error", "errorMessage": "..."}
# - mis-extracted entries with no videoId (e.g. type="playlist" wrapping
# a single video when Invidious can't decode the channel-videos renderer)
video_list = [v for v in raw_video_list if v.get("type") != "parse-error" and v.get("videoId")]
dropped = len(raw_video_list) - len(video_list)
if dropped > 0:
parse_errors = [v for v in raw_video_list if v.get("type") == "parse-error"]
no_video_id = [v for v in raw_video_list if v.get("type") != "parse-error" and not v.get("videoId")]
sample_type = no_video_id[0].get("type") if no_video_id else None
sample_msg = parse_errors[0].get("errorMessage", "") if parse_errors else ""
logger.warning(
f"[Feed] {channel_id}: Invidious returned {dropped} unusable entr(ies) "
f"out of {len(raw_video_list)} "
f"(parse_errors={len(parse_errors)}, no_video_id={len(no_video_id)}, "
f"sample_type={sample_type!r}, sample_error={sample_msg!r})"
)
# If Invidious produced no usable videos, fall back to yt-dlp.
if not video_list:
return None, None, True, "invidious_unusable_entries"
pagination_info = {
"total_fetched": result.get("total_fetched"),
"pagination_limited": result.get("pagination_limited", False),
"limit_reason": result.get("limit_reason"),
}
invidious_base = invidious_proxy.get_base_url()
videos = [_process_invidious_video(v, channel_id, invidious_base) for v in video_list]
return videos, pagination_info, False, None
except invidious_proxy.InvidiousProxyError as e:
s = get_settings()
# Determine fallback reason from error
if e.status_code:
fallback_reason = f"invidious_error_{e.status_code}"
else:
fallback_reason = "invidious_error_connection"
# If retryable error occurred (after all retries exhausted) and fallback is enabled
if e.is_retryable and s.feed_fallback_ytdlp_on_error:
logger.info(
f"[Feed] {channel_id}: Invidious failed after retries ({e}), "
f"falling back to yt-dlp"
)
return None, None, True, fallback_reason
# Non-retryable error or fallback disabled - re-raise
logger.warning(f"Invidious failed for {channel_id}: {e}")
raise
except (KeyError, TypeError, ValueError, OSError) as e:
logger.warning(f"Invidious failed for {channel_id}, falling back to yt-dlp: {type(e).__name__}: {e}")
return None, None, True, "invidious_error_other"
async def _fetch_from_ytdlp(
channel_id: str, site: str, channel_url: str, max_videos: int, use_flat_playlist: bool
) -> tuple[List[dict], Optional[Dict[str, Any]]]:
"""Fetch videos from yt-dlp.
Args:
channel_id: The channel ID
site: The site/platform name
channel_url: The stored channel URL
max_videos: Maximum number of videos to fetch
use_flat_playlist: Whether to use --flat-playlist flag
Returns:
Tuple of (videos_list, channel_metadata or None)
"""
url = _build_channel_url(channel_id, site, channel_url)
ytdlp_args = ["-j", "--no-warnings", "--playlist-items", f"1:{max_videos}"]
if use_flat_playlist:
ytdlp_args.insert(1, "--flat-playlist")
stdout = await run_ytdlp(*ytdlp_args, url)
videos = []
channel_metadata = None
for line in stdout.strip().split("\n"):
if not line:
continue
try:
info = json.loads(line)
# Extract channel metadata from first video only
if channel_metadata is None:
channel_metadata = {
"subscriber_count": info.get("channel_follower_count"),
"is_verified": info.get("channel_is_verified", False),
}
videos.append(_process_ytdlp_video(info, channel_id))
except json.JSONDecodeError:
continue
return videos, channel_metadata
async def fetch_channel_feed(
channel_id: str, site: str, channel_url: str
) -> tuple[List[Dict[str, Any]], Optional[Dict[str, Any]], Optional[Dict[str, Any]]]:
"""Fetch recent videos for a channel.
Args:
channel_id: The channel ID
site: The site/platform name (e.g., "youtube", "dailymotion")
channel_url: The channel URL for fetching
Returns:
Tuple of (videos list, pagination_info dict or None, channel_metadata dict or None)
pagination_info contains: total_fetched, pagination_limited, limit_reason
channel_metadata contains: subscriber_count, is_verified
"""
s = get_settings()
videos: List[dict] = []
pagination_info: Optional[Dict[str, Any]] = None
channel_metadata: Optional[Dict[str, Any]] = None
fallback_reason: Optional[str] = None
try:
# For YouTube: InnerTube → Invidious → yt-dlp
if site.lower() == "youtube":
# Try InnerTube first (rich data: viewCount, author, publishedText)
innertube_videos, innertube_metadata = await _fetch_from_innertube(channel_id, s.feed_max_videos)
if innertube_videos:
logger.debug(f"Fetched {len(innertube_videos)} videos from InnerTube for {channel_id}")
return innertube_videos, None, innertube_metadata
# Try Invidious second (includes publish timestamps)
if invidious_proxy.is_enabled():
logger.debug(f"Attempting Invidious fetch for {channel_id} - max_videos={s.feed_max_videos}")
videos, pagination_info, should_fallback, fallback_reason = await _fetch_from_invidious(
channel_id, s.feed_max_videos
)
if videos and not should_fallback:
logger.debug(f"Fetched {len(videos)} videos from Invidious for {channel_id}")
channel_metadata = await _fetch_channel_metadata_invidious(channel_id)
return videos, pagination_info, channel_metadata
# Fall back to yt-dlp for all sites (or if InnerTube+Invidious failed/not enabled)
if fallback_reason:
logger.info(f"[Feed] {channel_id}: Using yt-dlp fallback (reason: {fallback_reason})")
logger.debug(f"Using yt-dlp for {channel_id} ({site}) - flat_playlist={s.feed_ytdlp_use_flat_playlist}")
videos, channel_metadata = await _fetch_from_ytdlp(
channel_id, site, channel_url, s.feed_max_videos, s.feed_ytdlp_use_flat_playlist
)
if videos:
logger.debug(f"Fetched {len(videos)} videos from yt-dlp for {channel_id} ({site})")
except YtDlpError as e:
logger.error(f"Failed to fetch channel {channel_id} ({site}): {e}")
raise
except (OSError, ValueError) as e:
logger.error(f"Error fetching channel {channel_id} ({site}): {e}")
raise
# If flat-playlist was used, channel_metadata may be None - fetch it separately
if not channel_metadata and site.lower() == "youtube":
channel_metadata = await _fetch_channel_metadata_ytdlp(channel_id)
# yt-dlp doesn't provide pagination metadata
return videos, pagination_info, channel_metadata
def _get_all_thumbnails(thumbnails: List[dict]) -> tuple[str, List[dict]]:
"""Extract all quality thumbnails and best URL for backwards compatibility.
Returns:
Tuple of (best_url, all_thumbnails_list)
"""
if not thumbnails:
return "", []
# Sort thumbnails by quality score (best first)
quality_scores = {"maxres": 5, "maxresdefault": 5, "sddefault": 4, "high": 3, "medium": 2, "default": 1}
sorted_thumbs = sorted(thumbnails, key=lambda x: quality_scores.get(x.get("quality", ""), 0), reverse=True)
# Best URL for legacy thumbnail_url field
best_url = sorted_thumbs[0].get("url", "") if sorted_thumbs else ""
return best_url, thumbnails
def _get_all_ytdlp_thumbnails(info: dict) -> tuple[str, List[dict]]:
"""Extract all thumbnails from yt-dlp with quality mapping.
Returns:
Tuple of (best_url, all_thumbnails_list)
"""
thumbnails = info.get("thumbnails", [])
if not thumbnails:
fallback_url = info.get("thumbnail", "")
if fallback_url:
# Create a minimal thumbnail entry
return fallback_url, [{"quality": "default", "url": fallback_url, "width": None, "height": None}]
return "", []
# Convert to Invidious-compatible format with quality mapping
result = []
for thumb in thumbnails:
width = thumb.get("width", 0)
height = thumb.get("height", 0)
# Map dimensions to quality names (Invidious-compatible)
if width >= 1280:
quality = "maxres"
elif width >= 640:
quality = "sddefault"
elif width >= 480:
quality = "high"
elif width >= 320:
quality = "medium"
else:
quality = "default"
result.append(
{
"quality": quality,
"url": thumb.get("url", ""),
"width": width if width else None,
"height": height if height else None,
}
)
# Sort by width (largest first) and get best URL
sorted_thumbs = sorted(result, key=lambda x: x.get("width") or 0, reverse=True)
best_url = sorted_thumbs[0].get("url", "") if sorted_thumbs else ""
return best_url, result
def _parse_timestamp(value) -> Optional[int]:
"""Parse timestamp from various formats."""
if value is None:
return None
if isinstance(value, int):
return value
if isinstance(value, str):
# Try YYYYMMDD format
if len(value) == 8 and value.isdigit():
try:
dt = datetime.strptime(value, "%Y%m%d")
return int(dt.timestamp())
except ValueError:
pass
# Try ISO format
try:
dt = datetime.fromisoformat(value.replace("Z", "+00:00"))
return int(dt.timestamp())
except ValueError:
pass
return None
async def fetch_single_channel(channel_id: str, site: str, channel_url: str):
"""Fetch and cache videos for a single channel.
Used when a new subscription is added to immediately populate the feed.
Runs in the background to avoid blocking the API response.
"""
try:
videos, pagination_info, channel_metadata = await fetch_channel_feed(channel_id, site, channel_url)
if videos:
database.upsert_cached_videos(channel_id, site, videos)
database.update_fetch_status(
channel_id,
site,
success=True,
max_videos_fetched=pagination_info.get("total_fetched") if pagination_info else len(videos),
pagination_limited=pagination_info.get("pagination_limited", False) if pagination_info else False,
pagination_limit_reason=pagination_info.get("limit_reason") if pagination_info else None,
)
# Save channel metadata if available (subscriber count, verified status)
if channel_metadata:
database.update_channel_metadata(
channel_id,
site,
subscriber_count=channel_metadata.get("subscriber_count"),
is_verified=channel_metadata.get("is_verified"),
)
# Schedule avatar caching for YouTube channels
if site.lower() == "youtube":
logger.debug(f"Scheduling avatar cache for {channel_id}")
avatar_cache.get_cache().schedule_background_fetch(channel_id)
if pagination_info and pagination_info.get("pagination_limited"):
logger.warning(
f"[Feed] {channel_id} ({site}): Pagination limited - "
f"{pagination_info.get('total_fetched')} videos fetched "
f"(reason: {pagination_info.get('limit_reason')})"
)
logger.info(f"Fetched {len(videos)} videos for new subscription {channel_id} ({site})")
else:
logger.warning(f"No videos found for new subscription {channel_id} ({site})")
database.update_fetch_status(channel_id, site, success=True)
except _FEED_FETCH_ERRORS as e:
error_msg = str(e)[:200]
logger.error(f"Failed to fetch new subscription {channel_id} ({site}): {error_msg}")
database.update_fetch_status(channel_id, site, success=False, error=error_msg)
async def fetch_all_channels():
"""Fetch videos for all watched channels."""
channels = database.get_all_watched_channels()
if not channels:
logger.info("No channels to fetch")
return
logger.info(f"Starting feed fetch for {len(channels)} watched channels")
success_count = 0
error_count = 0
limited_count = 0
ytdlp_fallback_count = 0
for channel in channels:
channel_id = channel["channel_id"]
site = channel["site"]
channel_url = channel["channel_url"]
try:
# Check if this channel had 414 error before (to detect fallback usage)
had_414_before = False
with database.get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
"SELECT pagination_limit_reason FROM feed_fetch_status WHERE channel_id = ? AND site = ?",
(channel_id, site),
)
row = cursor.fetchone()
if row and row[0] == "414_error":
had_414_before = True
videos, pagination_info, channel_metadata = await fetch_channel_feed(channel_id, site, channel_url)
if videos:
database.upsert_cached_videos(channel_id, site, videos)
database.update_fetch_status(
channel_id,
site,
success=True,
max_videos_fetched=pagination_info.get("total_fetched") if pagination_info else len(videos),
pagination_limited=pagination_info.get("pagination_limited", False) if pagination_info else False,
pagination_limit_reason=pagination_info.get("limit_reason") if pagination_info else None,
)
# Save channel metadata if available (subscriber count, verified status)
if channel_metadata:
database.update_channel_metadata(
channel_id,
site,
subscriber_count=channel_metadata.get("subscriber_count"),
is_verified=channel_metadata.get("is_verified"),
)
# Schedule avatar caching for YouTube channels
if site.lower() == "youtube":
avatar_cache.get_cache().schedule_background_fetch(channel_id)
# Track if yt-dlp fallback was used (had 414 before, no pagination_info now means fallback used)
s = get_settings()
ytdlp_fallback_used = (
had_414_before
and not pagination_info
and s.feed_fallback_ytdlp_on_414
and site.lower() == "youtube"
)
if ytdlp_fallback_used:
ytdlp_fallback_count += 1
if pagination_info and pagination_info.get("pagination_limited"):
limited_count += 1
if pagination_info.get("limit_reason") == "414_error":
logger.warning(
f"[Feed] {channel_id} ({site}): 414 error - limited to "
f"{pagination_info.get('total_fetched')} videos"
)
logger.debug(f"Cached {len(videos)} videos for {channel_id} ({site})")
success_count += 1
else:
logger.warning(f"No videos found for {channel_id} ({site})")
database.update_fetch_status(channel_id, site, success=True)
success_count += 1
except _FEED_FETCH_ERRORS as e:
error_msg = str(e)[:200] # Truncate long error messages
logger.error(f"Failed to fetch {channel_id} ({site}): {error_msg}")
database.update_fetch_status(channel_id, site, success=False, error=error_msg)
error_count += 1
# Delay between channel fetches to avoid rate limiting
s = get_settings()
await asyncio.sleep(s.feed_channel_delay)
# Count 414 errors from database for final summary
limited_414_count = 0
with database.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM feed_fetch_status WHERE pagination_limit_reason = '414_error'")
row = cursor.fetchone()
if row:
limited_414_count = row[0]
s = get_settings()
if ytdlp_fallback_count > 0 and s.feed_fallback_ytdlp_on_414:
logger.info(
f"Feed fetch complete: {success_count} succeeded, {error_count} failed, "
f"{limited_count} pagination-limited this cycle ({limited_414_count} total with 414 errors), "
f"{ytdlp_fallback_count} used yt-dlp fallback"
)
else:
logger.info(
f"Feed fetch complete: {success_count} succeeded, {error_count} failed, "
f"{limited_count} pagination-limited this cycle ({limited_414_count} total with 414 errors)"
)
async def feed_fetch_loop():
"""Main loop for periodic feed fetching."""
while True:
try:
await fetch_all_channels()
# Cleanup old cached videos
s = get_settings()
database.cleanup_old_cached_videos(days=s.feed_video_max_age)
# Cleanup stale watched channels (not requested in 14 days)
database.cleanup_stale_watched_channels(days=14)
# Cleanup orphaned cached videos (channels no longer watched or subscribed)
database.cleanup_orphaned_cached_videos()
except Exception as e:
logger.error(f"Feed fetch loop error: {e}", exc_info=True)
# Wait for next fetch cycle
s = get_settings()
await asyncio.sleep(s.feed_fetch_interval)
def start_feed_fetcher():
"""Start the background feed fetcher task."""
global _fetch_task
if _fetch_task is None or _fetch_task.done():
_fetch_task = asyncio.create_task(feed_fetch_loop())
s = get_settings()
logger.info(f"Started feed fetcher (interval: {s.feed_fetch_interval}s)")
def stop_feed_fetcher():
"""Stop the background feed fetcher task."""
global _fetch_task
if _fetch_task and not _fetch_task.done():
_fetch_task.cancel()
logger.info("Stopped feed fetcher")