diff options
| author | A Farzat <a@farzat.xyz> | 2025-08-21 15:26:29 +0300 |
|---|---|---|
| committer | A Farzat <a@farzat.xyz> | 2025-08-21 15:26:29 +0300 |
| commit | a9b512db0db44170afd96b6b4547594286425f81 (patch) | |
| tree | e1bb4060d162406318b1ff4ed96e6decb2841952 /components | |
| parent | 69bcc9822c82ee6d726c98deafbdd0a20527a55a (diff) | |
| download | csca5028-a9b512db0db44170afd96b6b4547594286425f81.tar.gz csca5028-a9b512db0db44170afd96b6b4547594286425f81.zip | |
Better updates and database management for Subscription
Diffstat (limited to 'components')
| -rw-r--r-- | components/subscriptions/main.py | 38 |
1 files changed, 25 insertions, 13 deletions
diff --git a/components/subscriptions/main.py b/components/subscriptions/main.py index 26b0c64..af9d891 100644 --- a/components/subscriptions/main.py +++ b/components/subscriptions/main.py @@ -1,16 +1,18 @@ from dataclasses import dataclass, field, asdict from datetime import datetime, UTC from sys import stderr -from typing import TypedDict, List, cast +from typing import TypedDict, List, cast, Dict, Any from bson.objectid import ObjectId from feedparser import parse # type: ignore from pymongo.collection import Collection from pymongo.results import InsertOneResult, UpdateResult -import schedule +from schedule import Job, Scheduler from components.database import subscriptions from components.subscriptions.typing import SubsDict from components.videos import VideoTuple +default_scheduler = Scheduler() + @dataclass class Subscription: _id: str @@ -22,9 +24,15 @@ class Subscription: subscribers: List[ObjectId] = field(default_factory=list) def __post_init__(self) -> None: - self._job: schedule.Job = schedule.every(self.time_between_fetches).minutes.do(self.fetch) self._collection: Collection[SubsDict] = subscriptions - self._in_db: bool = False + self._scheduler: Scheduler = default_scheduler + if len(self.videos) and type(self.videos[0]) != VideoTuple: + self.videos = [VideoTuple._make(vid) for vid in self.videos] + + def initialise_job(self) -> None: + self._job: Job = self._scheduler.every(self.time_between_fetches).minutes.do(self.fetch) + if self.last_fetch > datetime.min.replace(tzinfo=UTC): + self._job.next_run += self.last_fetch - datetime.now(tz=UTC) def fetch(self) -> None: try: @@ -41,11 +49,15 @@ class Subscription: self.videos[i] = vid break last_video_update = max((vid.updated for vid in self.videos)) + self.last_fetch = datetime.now(tz=UTC) if last_video_update > self.last_video_update: - print("Updating", self._id) + print("Updating", self._id, end=", ") + print("New vid count:", len(self.videos)) self.last_video_update = last_video_update - self.update_videos() - self.last_fetch = datetime.now(tz=UTC) + self.update_fetch(videos=True) + else: + self.update_fetch() + print("Fetched", self._id, "at", self.last_fetch) def asdict(self) -> SubsDict: return cast(SubsDict, asdict(self)) @@ -53,12 +65,12 @@ class Subscription: def insert(self) -> InsertOneResult: return self._collection.insert_one(self.asdict()) - def update_videos(self) -> UpdateResult: + def update_fetch(self, videos: bool=False) -> UpdateResult: + updated_values: Dict[str, Any] = { "last_fetch": self.last_fetch, } + if videos: + updated_values["videos"] = self.videos + updated_values["last_video_update"] = self.last_video_update return self._collection.update_one( {"_id": self._id}, - {"$set": { - "videos": self.videos, - "last_video_update": self.last_video_update, - "last_fetch": self.last_fetch, - }}, + {"$set": updated_values}, ) |
