Allow feed titles and subtitles to change

Some sites forget to include a `<title>` tag in their feed, causing their title
column to be the empty string.

If any sites ever add/change their title tag, we should update their DB row
accordingly.

Same story for subtitles.
This commit is contained in:
David Florness 2022-11-07 21:05:26 -05:00
parent f12d32ad3c
commit 38b3b5a50c
No known key found for this signature in database
GPG Key ID: 060576B7352B8DE3
2 changed files with 19 additions and 5 deletions

View File

@ -136,11 +136,11 @@ class RSSBot(Plugin):
await asyncio.gather(*tasks)
async def _poll_once(self) -> None:
subs = await self.dbm.get_feeds()
if not subs:
feeds = await self.dbm.get_feeds()
if not feeds:
return
now = int(time())
tasks = [self.try_parse_feed(feed=feed) for feed in subs if feed.next_retry < now]
tasks = [self.try_parse_feed(feed=feed) for feed in feeds.values() if feed.next_retry < now]
feed: Feed
entries: Iterable[Entry]
self.log.info(f"Polling {len(tasks)} feeds")
@ -150,6 +150,12 @@ class RSSBot(Plugin):
f"Fetching {feed.id} (backoff: {feed.error_count} / {feed.next_retry}) "
f"success: {bool(entries)}"
)
if feed.title != feeds[feed.id].title:
self.log.info(f"Feed {feed.id} title changed from {feeds[feed.id].title} to {feed.title}")
self.dbm.set_title(feed, feed.title)
if feed.subtitle != feeds[feed.id].subtitle:
self.log.info(f"Feed {feed.id} subtitle changed from {feeds[feed.id].subtitle} to {feed.subtitle}")
self.dbm.set_subtitle(feed, feed.subtitle)
if not entries:
error_count = feed.error_count + 1
next_retry_delay = self.config["update_interval"] * 60 * error_count

View File

@ -118,7 +118,7 @@ class DBManager:
def __init__(self, db: Database) -> None:
self.db = db
async def get_feeds(self) -> list[Feed]:
async def get_feeds(self) -> dict[int, Feed]:
q = """
SELECT id, url, title, subtitle, link, next_retry, error_count,
room_id, user_id, notification_template, send_notice
@ -132,7 +132,7 @@ class DBManager:
except KeyError:
feed = feeds[row["id"]] = Feed.from_row(row)
feed.subscriptions.append(Subscription.from_row(row))
return list(feeds.values())
return feeds
async def get_feeds_by_room(self, room_id: RoomID) -> list[tuple[Feed, UserID]]:
q = """
@ -212,6 +212,14 @@ class DBManager:
q = "UPDATE feed SET error_count = $2, next_retry = $3 WHERE id = $1"
await self.db.execute(q, info.id, error_count, next_retry)
async def set_title(self, info: Feed, new_title: str) -> None:
q = "UPDATE feed SET title = $2 WHERE id = $1"
await self.db.execute(q, info.id, new_title)
async def set_subtitle(self, info: Feed, new_subtitle: str) -> None:
q = "UPDATE feed SET subtitle = $2 WHERE id = $1"
await self.db.execute(q, info.id, new_subtitle)
async def subscribe(
self,
feed_id: int,