jellyfin/src/episode.py

65 lines
2.1 KiB
Python
Raw Normal View History

2023-04-05 16:31:14 +00:00
"""set metadata to episodes"""
from datetime import datetime
from src.connect import Jellyfin, TubeArchivist
2023-04-06 04:58:04 +00:00
from src.static_types import TAVideo
2023-04-05 16:31:14 +00:00
class Episode:
"""interact with an single episode"""
2023-04-06 04:58:04 +00:00
def __init__(self, youtube_id: str, jf_id: str):
self.youtube_id: str = youtube_id
self.jf_id: str = jf_id
2023-04-05 16:31:14 +00:00
2023-04-06 04:58:04 +00:00
def sync(self) -> None:
2023-04-05 16:31:14 +00:00
"""sync episode metadata"""
2023-04-06 04:58:04 +00:00
ta_video: TAVideo = self.get_ta_video()
2023-04-05 16:31:14 +00:00
self.update_metadata(ta_video)
self.update_artwork(ta_video)
2023-04-06 04:58:04 +00:00
def get_ta_video(self) -> TAVideo:
2023-04-05 16:31:14 +00:00
"""get video metadata from ta"""
2023-04-06 04:58:04 +00:00
path: str = f"/video/{self.youtube_id}"
ta_video: TAVideo = TubeArchivist().get(path)
2023-04-05 16:31:14 +00:00
return ta_video
2023-04-06 04:58:04 +00:00
def update_metadata(self, ta_video: TAVideo) -> None:
2023-04-05 16:31:14 +00:00
"""update jellyfin metadata from item_id"""
2023-04-06 04:58:04 +00:00
published: str = ta_video["published"]
published_date: datetime = datetime.strptime(published, "%d %b, %Y")
data: dict = {
2023-04-05 16:31:14 +00:00
"Id": self.jf_id,
"Name": ta_video.get("title"),
"Genres": [],
"Tags": [],
"ProductionYear": published_date.year,
"ProviderIds": {},
"ParentIndexNumber": published_date.year,
"PremiereDate": published_date.isoformat(),
"Overview": self._get_desc(ta_video),
}
2023-04-06 04:58:04 +00:00
path: str = f"Items/{self.jf_id}"
2023-04-05 16:31:14 +00:00
Jellyfin().post(path, data)
2023-04-06 04:58:04 +00:00
def update_artwork(self, ta_video: TAVideo) -> None:
2023-04-05 16:31:14 +00:00
"""update episode artwork in jf"""
2023-04-06 04:58:04 +00:00
thumb_path: str = ta_video["vid_thumb_url"]
thumb_base64: bytes = TubeArchivist().get_thumb(thumb_path)
path: str = f"Items/{self.jf_id}/Images/Primary"
2023-04-05 16:31:14 +00:00
Jellyfin().post_img(path, thumb_base64)
2023-04-06 04:58:04 +00:00
def _get_desc(self, ta_video: TAVideo) -> str | bool:
2023-04-05 16:31:14 +00:00
"""get description"""
2023-04-06 04:58:04 +00:00
raw_desc: str = ta_video["description"]
if not raw_desc:
return False
desc_clean: str = raw_desc.replace("\n", "<br>")
2023-04-05 16:31:14 +00:00
if len(raw_desc) > 500:
2023-04-06 04:58:04 +00:00
return desc_clean[:500] + " ..."
2023-04-05 16:31:14 +00:00
2023-04-06 04:58:04 +00:00
return desc_clean