cli/ytrssil/datatypes.py

75 lines
1.8 KiB
Python
Raw Normal View History

2021-08-06 00:07:30 +02:00
from __future__ import annotations
2021-07-30 11:48:32 +02:00
from dataclasses import dataclass, field
from datetime import datetime
from typing import Any, Optional, TypedDict
class VideoData(TypedDict):
video_id: str
name: str
url: str
channel_id: str
channel_name: str
timestamp: datetime
watch_timestamp: Optional[datetime]
2021-08-06 00:07:30 +02:00
class ChannelData(TypedDict):
channel_id: str
name: str
new_videos: dict[str, Any]
watched_videos: dict[str, Any]
2021-07-30 11:48:32 +02:00
@dataclass
class Video:
video_id: str
name: str
url: str
channel_id: str
channel_name: str
timestamp: datetime
2021-08-06 00:07:30 +02:00
watch_timestamp: Optional[datetime] = None
2021-07-30 11:48:32 +02:00
def __str__(self) -> str:
return f'{self.channel_name} - {self.name} - {self.video_id}'
2021-08-06 00:07:30 +02:00
@classmethod
def from_dict(cls, data: VideoData) -> Video:
return cls(**data)
2021-07-30 11:48:32 +02:00
@dataclass
class Channel:
channel_id: str
name: str
new_videos: dict[str, Video] = field(default_factory=lambda: dict())
watched_videos: dict[str, Video] = field(default_factory=lambda: dict())
def add_video(self, video: Video) -> bool:
if (
video.video_id in self.watched_videos
or video.video_id in self.new_videos
):
return False
2021-07-30 11:48:32 +02:00
self.new_videos[video.video_id] = video
return True
2021-07-30 11:48:32 +02:00
def mark_video_as_watched(self, video: Video) -> None:
self.new_videos.pop(video.video_id)
self.watched_videos[video.video_id] = video
def __str__(self) -> str:
return f'{self.name} - {len(self.new_videos)}'
2021-08-06 00:07:30 +02:00
@classmethod
def from_dict(cls, data: ChannelData) -> Channel:
return cls(
channel_id=data['channel_id'],
name=data['name'],
new_videos=data.get('new_videos', {}).copy(),
watched_videos=data.get('watched_videos', {}).copy(),
)