Skip to content

Commit

Permalink
custom movie source
Browse files Browse the repository at this point in the history
  • Loading branch information
NextFire committed Feb 20, 2024
1 parent add7024 commit dc08d25
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 20 deletions.
8 changes: 7 additions & 1 deletion sachi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ def analyze_media(self):

audio = media_info.audio_tracks[0]
self.ctx.ac = audio.format
# FIXME:
if audio.other_channel_positions:
self.ctx.channels = ".".join(
audio.other_channel_positions[0].split("/")[:2]
)

self.media_analysis_done.set()

Expand All @@ -95,6 +100,7 @@ def analyze_match(self):
parent, episode = self.match.parent, self.match.episode

self.ctx.n = parent.title
self.ctx.y = parent.year

if episode is not None:
self.ctx.s = episode.season
Expand Down Expand Up @@ -125,7 +131,7 @@ async def template_new_path(self):
segment = template.render(ctx_dict)
segment = FS_SPECIAL_CHARS.sub("", segment)
new_path /= segment
new_path = new_path.with_suffix(self.path.suffix)
new_path = new_path.with_name(new_path.name + self.path.suffix)
self.set_rename_cell(str(new_path.relative_to(self.base_dir)))
self.new_path.set_result(new_path)

Expand Down
10 changes: 7 additions & 3 deletions sachi/screens/episodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@

from sachi.models import SachiMatch
from sachi.screens.rename import RenameScreen
from sachi.sources import SOURCE_CLASSES
from sachi.sources.base import SachiEpisodeModel, SachiParentModel, SachiSource
from sachi.sources.base import (
SachiEpisodeModel,
SachiParentModel,
SachiSource,
get_all_sources,
)


class ParentSelectionModal(ModalScreen[SachiParentModel]):
Expand Down Expand Up @@ -63,7 +67,7 @@ def compose(self) -> ComposeResult:
with Horizontal():
yield Input(placeholder="Search", id="search-input", restrict=r".+")
yield Select(
((f"{s.service} ({s.media_type})", s) for s in SOURCE_CLASSES),
((f"{s.service} ({s.media_type})", s) for s in get_all_sources()),
prompt="Source",
)
yield SelectionList[int](id="episodes-list")
Expand Down
11 changes: 0 additions & 11 deletions sachi/sources/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +0,0 @@
from typing import TYPE_CHECKING

from sachi.sources.tvdb import TVDBSource

if TYPE_CHECKING:
from sachi.sources.base import SachiSource


SOURCE_CLASSES: list[type["SachiSource"]] = [
TVDBSource,
]
14 changes: 12 additions & 2 deletions sachi/sources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ class MediaType(StrEnum):
@dataclass
class SachiParentModel[RefIdType]:
media_type: MediaType
refId: RefIdType
ref_id: RefIdType
title: str
year: int | None = None


@dataclass
class SachiEpisodeModel[RefIdType]:
refId: RefIdType
ref_id: RefIdType
season: int
episode: int
name: str | None = None
Expand Down Expand Up @@ -60,3 +60,13 @@ def __init_subclass__(cls, /, media_type: MediaType, service: str, **kwargs):
super().__init_subclass__(**kwargs)
cls.media_type = media_type
cls.service = service


def get_all_sources() -> list[type[SachiSource]]:
from sachi.sources.custom import CustomMovieSource
from sachi.sources.tvdb import TVDBSource

return [
TVDBSource,
CustomMovieSource,
]
33 changes: 33 additions & 0 deletions sachi/sources/custom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import re

from sachi.sources.base import (
MediaType,
SachiEpisodeModel,
SachiParentModel,
SachiSource,
)

INPUT_RE = re.compile(r"(?P<title>.+?)(?: \((?P<year>\d+)\))?$")


class CustomMovieSource(
SachiSource[None], media_type=MediaType.MOVIE, service="Custom"
):
async def search(self, query: str) -> list[SachiParentModel[None]]:
qmatch = INPUT_RE.match(query)
if qmatch is None:
return []
groups = qmatch.groupdict()
return [
SachiParentModel(
media_type=MediaType.MOVIE,
ref_id=None,
title=groups["title"],
year=int(groups["year"]) if groups["year"] else None,
)
]

async def get_episodes(
self, parent: SachiParentModel[None]
) -> list[SachiEpisodeModel[None]]:
return [SachiEpisodeModel(ref_id=None, season=0, episode=0)]
6 changes: 3 additions & 3 deletions sachi/sources/tvdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async def _search():
return [
SachiParentModel(
media_type=self.media_type,
refId=sr.tvdb_id,
ref_id=sr.tvdb_id,
title=sr.translations.get("eng", sr.name),
year=sr.year,
)
Expand All @@ -111,7 +111,7 @@ async def _episodes():
async with self.session.get(
self.server
/ "series"
/ str(parent.refId)
/ str(parent.ref_id)
/ "episodes"
/ "default"
/ "eng",
Expand All @@ -125,7 +125,7 @@ async def _episodes():
episodes_res = await _episodes()
return [
SachiEpisodeModel[int](
refId=ep.id,
ref_id=ep.id,
season=ep.seasonNumber,
episode=ep.number,
name=ep.name,
Expand Down

0 comments on commit dc08d25

Please sign in to comment.