Open on the Feed, and refresh it without hammering yt-dlp
The tab order was Watch, Search, Feed, Browse, Downloads, and the app opened on Watch -- which is where the other tabs send you, not somewhere you start. It is now Feed, Search, Browse, Downloads, Watch, with icons. Routing is unaffected: _tab_index_of resolves by widget identity, not position. Refresh-on-open needed care rather than a call in showEvent. A local refresh is one yt-dlp subprocess per subscribed channel, serially, so it is braked four ways: only channels not seen for feed.auto_refresh_on_open_minutes (30, 0 to disable), at most eight per visit, once per interval per session, and after a delay so it does not race the tab transition or first-run setup. The cached feed is already on screen throughout. That is a new config key rather than the dead auto_refresh_minutes, because existing configs store that as 0 and would have read as opting out of a feature that did not exist yet. SmoothTabWidget gained the currentChanged signal, icons and a corner slot it never had. Its set_current_index needed a re-entrancy flag, not just an index check: setting the tab bar's index emits its currentChanged straight back into the same method, and at that point the stack has not moved, so every switch fired the activation hook twice. The grid was cleared and rebuilt after every channel finished -- flicker, lost scroll position and every thumbnail re-read from disk each time. merge_entries keeps existing cards. While there, _relayout was re-adding cards the layout already owned, so layout items accumulated on every Load more, and the resize check compared against columnCount(), which never shrinks, so it relaid out on every resize event. Smaller things this exposed: feed errors were written into the label the next success overwrote, so failures were invisible; switching to account mode left the local videos on screen; cancel() had no callers, so a refresh outlived the tab and the window; Browse's Subscribe never said Unsubscribe though it toggles; "Play all" queued one page while claiming otherwise; and the feed sorted publish times against wall-clock fetch times in one COALESCE, so the last-refreshed channel floated to the top. Feed is the first thing seen now, so an empty one says what to do about it instead of showing a bare grid. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
+195
-17
@@ -16,7 +16,7 @@ Browse page's Subscribe button routes here via the main window.
|
||||
import time
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from PySide6.QtCore import QThread, Qt, Signal
|
||||
from PySide6.QtCore import QThread, QTimer, Qt, Signal
|
||||
from PySide6.QtWidgets import (
|
||||
QComboBox,
|
||||
QHBoxLayout,
|
||||
@@ -38,6 +38,18 @@ from ..utils.ytsage_localization import _
|
||||
from ..utils.ytsage_logger import logger
|
||||
|
||||
|
||||
#: Only refresh a channel automatically if it is at least this stale.
|
||||
AUTO_REFRESH_DEFAULT_MINUTES = 30
|
||||
#: Channels touched per automatic refresh. A manual Refresh still does all of
|
||||
#: them; this only bounds what opening the tab can set off.
|
||||
AUTO_REFRESH_BATCH = 8
|
||||
#: Let the tab transition finish before spawning subprocesses.
|
||||
AUTO_REFRESH_DELAY_MS = 1500
|
||||
#: The automatic path gives up sooner than a manual refresh: nobody is
|
||||
#: watching it, and a stuck channel must not block the rest.
|
||||
AUTO_REFRESH_TIMEOUT = 45
|
||||
|
||||
|
||||
class FeedRefreshWorker(QThread):
|
||||
"""Sequentially refresh each subscription's recent uploads."""
|
||||
|
||||
@@ -45,10 +57,17 @@ class FeedRefreshWorker(QThread):
|
||||
channelFailed = Signal(str, str) # channel_id, error
|
||||
allDone = Signal()
|
||||
|
||||
def __init__(self, subscriptions: List[Dict[str, Any]], per_channel: int, parent=None) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
subscriptions: List[Dict[str, Any]],
|
||||
per_channel: int,
|
||||
parent=None,
|
||||
timeout: Optional[int] = None,
|
||||
) -> None:
|
||||
super().__init__(parent)
|
||||
self._subs = subscriptions
|
||||
self._per_channel = per_channel
|
||||
self._timeout = timeout
|
||||
self._cancelled = False
|
||||
|
||||
def cancel(self) -> None:
|
||||
@@ -60,8 +79,15 @@ class FeedRefreshWorker(QThread):
|
||||
if self._cancelled:
|
||||
break
|
||||
try:
|
||||
kwargs = {}
|
||||
if self._timeout is not None:
|
||||
kwargs["timeout"] = self._timeout
|
||||
entries = client.fetch_flat_entries(
|
||||
f"{sub['url'].rstrip('/')}/videos", start=1, end=self._per_channel, use_cache=False
|
||||
f"{sub['url'].rstrip('/')}/videos",
|
||||
start=1,
|
||||
end=self._per_channel,
|
||||
use_cache=False,
|
||||
**kwargs,
|
||||
)
|
||||
LibraryManager.upsert_feed_items(sub["channel_id"], entries)
|
||||
LibraryManager.mark_refreshed(sub["channel_id"])
|
||||
@@ -73,11 +99,20 @@ class FeedRefreshWorker(QThread):
|
||||
|
||||
|
||||
class FeedPage(QWidget):
|
||||
#: Emitted after subscribe/unsubscribe so other pages can re-read state.
|
||||
subscriptionsChanged = Signal()
|
||||
|
||||
def __init__(self, router, parent: Optional[QWidget] = None) -> None:
|
||||
super().__init__(parent)
|
||||
self._router = router
|
||||
self._refresh_worker: Optional[FeedRefreshWorker] = None
|
||||
self._account_worker: Optional[YtdlpWorker] = None
|
||||
#: monotonic timestamp of the last automatic refresh this session
|
||||
self._last_auto_refresh: float = 0.0
|
||||
self._done_count = 0
|
||||
self._failed_count = 0
|
||||
self._total_count = 0
|
||||
self._last_error = ""
|
||||
|
||||
layout = QVBoxLayout(self)
|
||||
layout.setContentsMargins(8, 8, 8, 8)
|
||||
@@ -117,8 +152,25 @@ class FeedPage(QWidget):
|
||||
side_layout.addWidget(self.subs_list)
|
||||
splitter.addWidget(side)
|
||||
|
||||
self.grid = VideoCardGrid(router, self)
|
||||
splitter.addWidget(self.grid)
|
||||
grid_host = QWidget()
|
||||
grid_layout = QVBoxLayout(grid_host)
|
||||
grid_layout.setContentsMargins(0, 0, 0, 0)
|
||||
self.grid = VideoCardGrid(router, grid_host)
|
||||
grid_layout.addWidget(self.grid, stretch=1)
|
||||
|
||||
# Feed is the first tab, so an empty feed is the first thing a new
|
||||
# install shows. Say what to do about it.
|
||||
self.empty_hint = QLabel("")
|
||||
self.empty_hint.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||
self.empty_hint.setWordWrap(True)
|
||||
self.empty_hint.setStyleSheet("color: #9aa0a6; padding: 24px; font-size: 13px;")
|
||||
self.empty_hint.hide()
|
||||
grid_layout.addWidget(self.empty_hint, stretch=1)
|
||||
# The grid carries its own bare "Nothing here yet"; two empty states
|
||||
# stacked on one page is one too many.
|
||||
self.grid.empty_label.hide()
|
||||
|
||||
splitter.addWidget(grid_host)
|
||||
splitter.setStretchFactor(0, 1)
|
||||
splitter.setStretchFactor(1, 4)
|
||||
splitter.setSizes([220, 900])
|
||||
@@ -142,6 +194,8 @@ class FeedPage(QWidget):
|
||||
LibraryManager.subscribe(str(channel_id), meta.get("title") or str(channel_id), meta["url"], meta.get("avatar_url"))
|
||||
self.status_label.setText(_("feed.subscribed", title=meta.get("title") or channel_id))
|
||||
self.reload_subscriptions()
|
||||
self._update_empty_state()
|
||||
self.subscriptionsChanged.emit()
|
||||
|
||||
def reload_subscriptions(self) -> None:
|
||||
self.subs_list.clear()
|
||||
@@ -158,21 +212,68 @@ class FeedPage(QWidget):
|
||||
else:
|
||||
self._refresh_local()
|
||||
|
||||
# --------------------------------------------------- refresh on opening
|
||||
|
||||
def on_tab_activated(self) -> None:
|
||||
"""
|
||||
Called by the main window when the Feed tab becomes visible.
|
||||
|
||||
The cached grid is already on screen -- it is loaded in the
|
||||
constructor and kept up to date -- so this only decides whether to go
|
||||
and fetch. A refresh is one yt-dlp subprocess per subscribed channel,
|
||||
so it is braked four ways: only channels that are actually stale, at
|
||||
most one automatic run per interval per session, a batch cap, and a
|
||||
short delay so it does not race the tab transition or first-run setup.
|
||||
"""
|
||||
if self._refresh_worker is not None or self._account_worker is not None:
|
||||
return
|
||||
if (self.mode_combo.currentData() or "local") != "local":
|
||||
return
|
||||
|
||||
minutes = ConfigManager.get("feed.auto_refresh_on_open_minutes")
|
||||
minutes = AUTO_REFRESH_DEFAULT_MINUTES if minutes is None else int(minutes)
|
||||
if minutes <= 0:
|
||||
return # opted out
|
||||
|
||||
now = time.monotonic()
|
||||
if self._last_auto_refresh and (now - self._last_auto_refresh) < minutes * 60:
|
||||
return
|
||||
|
||||
cutoff = time.time() - minutes * 60
|
||||
stale = [s for s in LibraryManager.subscriptions() if not s.get("last_refreshed") or s["last_refreshed"] < cutoff]
|
||||
if not stale:
|
||||
return
|
||||
|
||||
self._last_auto_refresh = now
|
||||
batch = stale[:AUTO_REFRESH_BATCH]
|
||||
if len(stale) > len(batch):
|
||||
logger.info(f"Feed auto-refresh: {len(batch)} of {len(stale)} stale channels this time.")
|
||||
QTimer.singleShot(AUTO_REFRESH_DELAY_MS, lambda: self._refresh_local(batch, auto=True))
|
||||
|
||||
# --------------------------------------------------------------- local
|
||||
|
||||
def _refresh_local(self) -> None:
|
||||
def _refresh_local(self, subs: Optional[List[Dict[str, Any]]] = None, auto: bool = False) -> None:
|
||||
if self._refresh_worker is not None:
|
||||
return
|
||||
subs = LibraryManager.subscriptions()
|
||||
if subs is None:
|
||||
subs = LibraryManager.subscriptions()
|
||||
if not subs:
|
||||
self.status_label.setText(_("feed.no_subscriptions"))
|
||||
if not auto:
|
||||
self.status_label.setText(_("feed.no_subscriptions"))
|
||||
return
|
||||
per_channel = int(ConfigManager.get("feed.per_channel_items") or 15)
|
||||
self.refresh_btn.setEnabled(False)
|
||||
self.status_label.setText(_("feed.refreshing", done=0, total=len(subs)))
|
||||
self._done_count = 0
|
||||
self._failed_count = 0
|
||||
self._last_error = ""
|
||||
self._total_count = len(subs)
|
||||
self._refresh_worker = FeedRefreshWorker(subs, per_channel, parent=self)
|
||||
self._refresh_worker = FeedRefreshWorker(
|
||||
subs,
|
||||
per_channel,
|
||||
parent=self,
|
||||
timeout=AUTO_REFRESH_TIMEOUT if auto else None,
|
||||
)
|
||||
self._refresh_worker.channelDone.connect(self._on_channel_done)
|
||||
self._refresh_worker.channelFailed.connect(self._on_channel_failed)
|
||||
self._refresh_worker.allDone.connect(self._on_refresh_done)
|
||||
@@ -181,22 +282,36 @@ class FeedPage(QWidget):
|
||||
def _on_channel_done(self, channel_id: str) -> None:
|
||||
self._done_count += 1
|
||||
self.status_label.setText(_("feed.refreshing", done=self._done_count, total=self._total_count))
|
||||
self._load_cached_feed()
|
||||
# Merge only this channel's rows. Re-reading the whole feed here
|
||||
# rebuilt every card after every channel: visible flicker, the scroll
|
||||
# position lost each time, and thumbnails re-read from disk.
|
||||
self._merge_channel(channel_id)
|
||||
|
||||
def _on_channel_failed(self, channel_id: str, error: str) -> None:
|
||||
self._done_count += 1
|
||||
self.status_label.setText(_("feed.channel_failed", error=error[:120]))
|
||||
self._failed_count += 1
|
||||
# Do not write the error into the status line here: the next
|
||||
# channelDone overwrites it immediately, so failures were invisible.
|
||||
# It is reported once at the end instead.
|
||||
self._last_error = error
|
||||
|
||||
def _on_refresh_done(self) -> None:
|
||||
self.refresh_btn.setEnabled(True)
|
||||
self.status_label.setText(_("feed.refreshed", count=self.grid.card_count()))
|
||||
if self._failed_count:
|
||||
self.status_label.setText(
|
||||
_("feed.refreshed_with_failures", count=self.grid.card_count(), failed=self._failed_count)
|
||||
)
|
||||
logger.warning(f"Feed refresh: {self._failed_count} channel(s) failed; last error: {self._last_error[:200]}")
|
||||
else:
|
||||
self.status_label.setText(_("feed.refreshed", count=self.grid.card_count()))
|
||||
self._update_empty_state()
|
||||
if self._refresh_worker is not None:
|
||||
self._refresh_worker.deleteLater()
|
||||
self._refresh_worker = None
|
||||
|
||||
def _load_cached_feed(self) -> None:
|
||||
items = LibraryManager.feed_items()
|
||||
entries = [
|
||||
@staticmethod
|
||||
def _to_entries(items: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
||||
return [
|
||||
{
|
||||
"id": it["video_id"],
|
||||
"url": it["url"],
|
||||
@@ -207,7 +322,41 @@ class FeedPage(QWidget):
|
||||
}
|
||||
for it in items
|
||||
]
|
||||
self.grid.set_entries(entries)
|
||||
|
||||
def _load_cached_feed(self) -> None:
|
||||
self.grid.set_entries(self._to_entries(LibraryManager.feed_items()))
|
||||
self._update_empty_state()
|
||||
|
||||
def _merge_channel(self, channel_id: str) -> None:
|
||||
"""Fold one channel's fresh rows into the grid without rebuilding it."""
|
||||
try:
|
||||
items = LibraryManager.feed_items(channel_id=channel_id)
|
||||
except TypeError:
|
||||
# Older signature without the filter: fall back to a full reload.
|
||||
self._load_cached_feed()
|
||||
return
|
||||
self.grid.merge_entries(self._to_entries(items))
|
||||
self._update_empty_state()
|
||||
|
||||
def _update_empty_state(self) -> None:
|
||||
"""
|
||||
Feed is the first tab now, so an empty one is the first thing a new
|
||||
user sees. Say what to do about it rather than showing a blank grid.
|
||||
"""
|
||||
self.grid.empty_label.hide()
|
||||
if self.grid.card_count() > 0:
|
||||
self.empty_hint.hide()
|
||||
self.grid.show()
|
||||
return
|
||||
self.empty_hint.setText(
|
||||
_("feed.empty_no_subscriptions")
|
||||
if not LibraryManager.subscriptions()
|
||||
else _("feed.empty_not_refreshed")
|
||||
)
|
||||
# Hide the empty grid so the hint sits in the middle of the page
|
||||
# rather than pinned under a large blank area.
|
||||
self.grid.hide()
|
||||
self.empty_hint.show()
|
||||
|
||||
# -------------------------------------------------------------- account
|
||||
|
||||
@@ -251,8 +400,37 @@ class FeedPage(QWidget):
|
||||
self._update_mode_availability()
|
||||
super().showEvent(event)
|
||||
|
||||
def hideEvent(self, event) -> None:
|
||||
# Leaving the tab cancels an automatic refresh. cancel() existed and
|
||||
# was never called, so a background sweep kept running -- and closing
|
||||
# the window destroyed a live QThread.
|
||||
self._cancel_refresh()
|
||||
super().hideEvent(event)
|
||||
|
||||
def _cancel_refresh(self) -> None:
|
||||
worker = self._refresh_worker
|
||||
if worker is None:
|
||||
return
|
||||
worker.cancel()
|
||||
if not worker.wait(3000):
|
||||
logger.warning("Feed refresh did not stop in time.")
|
||||
|
||||
def shutdown(self) -> None:
|
||||
"""Called on application close."""
|
||||
self._cancel_refresh()
|
||||
|
||||
def _on_mode_changed(self) -> None:
|
||||
ConfigManager.set("feed.mode", self.mode_combo.currentData())
|
||||
mode = self.mode_combo.currentData()
|
||||
ConfigManager.set("feed.mode", mode)
|
||||
# Switching modes left the previous mode's items on screen, so the
|
||||
# local feed appeared to be the account feed. Show what the new mode
|
||||
# actually has.
|
||||
if mode == "local":
|
||||
self._load_cached_feed()
|
||||
else:
|
||||
self.grid.set_entries([])
|
||||
self.status_label.setText(_("feed.account_needs_refresh"))
|
||||
self._update_empty_state()
|
||||
|
||||
def _on_sub_activated(self, item: QListWidgetItem) -> None:
|
||||
sub = item.data(Qt.ItemDataRole.UserRole)
|
||||
|
||||
Reference in New Issue
Block a user