Skip to content

Commit

Permalink
Implement subclasses of VideoHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
SeoulSKY committed Jun 30, 2024
1 parent 314bfdd commit fbb2898
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
3 changes: 2 additions & 1 deletion ytnoti/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,8 @@ async def _post(self, request: Request):
url=entry["link"]["@href"],
thumbnail=thumbnail,
stats=stats,
timestamp=timestamp
timestamp=timestamp,
channel=channel
)

notification = Notification(channel, video)
Expand Down
29 changes: 22 additions & 7 deletions ytnoti/models/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
from abc import ABC, abstractmethod
from collections import OrderedDict
from os import PathLike
from pathlib import Path

from ytnoti.models.notification import Video

Expand Down Expand Up @@ -75,23 +75,38 @@ class FileVideoHistory(VideoHistory):
Represents a file-based history of videos.
"""

def __init__(self, path: PathLike[str]) -> None:
def __init__(self, dir_path: Path) -> None:
"""
Create a new FileVideoHistory instance.
:param path: The path to the file to store the history.
:param dir_path: The path to the directory to store the history files
"""

self._logger = logging.getLogger(self.__class__.__name__)
self._path = path
self._dir_path = dir_path

def _get_path(self, video: Video) -> Path:
"""
Get the path to the history file for a channel.
"""

return self._dir_path / video.channel.id

async def add(self, video: Video) -> None:
with open(self._path, "a", encoding="utf-8") as file:
self._logger.debug("Adding video (%s) to history", video.id)
self._dir_path.mkdir(exist_ok=True)

path = self._get_path(video)

with open(path, "a", encoding="utf-8") as file:
self._logger.debug("Adding video (%s) to history at %s", video.id, path)
file.write(f"{video.id}\n")

async def has(self, video: Video) -> bool:
with open(self._path, "r", encoding="utf-8") as file:
path = self._get_path(video)
if not path.exists():
return False

with open(path, "r", encoding="utf-8") as file:
for line in file:
if line.strip() == video.id:
return True
Expand Down
5 changes: 5 additions & 0 deletions ytnoti/models/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class Video:
Represents a YouTube video
"""

# pylint: disable=too-many-instance-attributes

id: str
"""The unique ID of the video"""

Expand All @@ -102,6 +104,9 @@ class Video:
timestamp: Timestamp
"""The timestamps of the video"""

channel: Channel
"""The channel of the video"""


@dataclass
class Notification:
Expand Down

0 comments on commit fbb2898

Please sign in to comment.