def __post_init__(self, file_path: Path): if file_path is None: return quality_keys = { "audio_codec", "audio_profile", "screen_size", "source", "video_codec", "video_profile", } # inspect path data self._path_data = {} self._parse_path_data(file_path) # set common attributes self.media = MediaType(self._path_data["type"]) self.quality = (" ".join(self._path_data[key] for key in self._path_data.keys() if key in quality_keys) or None) self.group = self._path_data.get("release_group") self.extension = self._path_data.get("container")
def _parse(self, file_path: PurePath): path_data = {} options = {"type": self._settings.media} raw_data = dict(guessit(str(file_path), options)) if isinstance(raw_data.get("season"), list): raw_data = dict(guessit(str(file_path.parts[-1]), options)) for k, v in raw_data.items(): if hasattr(v, "alpha3"): try: path_data[k] = Language.parse(v) except MnamerException: continue elif isinstance(v, (int, str, date)): path_data[k] = v elif isinstance(v, list) and all( [isinstance(_, (int, str)) for _ in v] ): path_data[k] = v[0] if self._settings.media: media_type = self._settings.media elif path_data.get("type"): media_type = MediaType(path_data["type"]) else: media_type = None meta_cls = { MediaType.EPISODE: MetadataEpisode, MediaType.MOVIE: MetadataMovie, None: Metadata, }[media_type] self.metadata = meta_cls(language=self._settings.language) self.metadata.quality = ( " ".join( path_data[key] for key in path_data if key in ( "audio_codec", "audio_profile", "screen_size", "source", "video_codec", "video_profile", ) ) or None ) self.metadata.group = path_data.get("release_group") self.metadata.container = file_path.suffix or None if not self.metadata.language: try: self.metadata.language = path_data.get("language") except MnamerException: pass try: self.metadata.language_sub = path_data.get("subtitle_language") except MnamerException: pass if self.metadata.media is MediaType.MOVIE: self.metadata.name = path_data.get("title") self.metadata.year = path_data.get("year") elif self.metadata.media is MediaType.EPISODE: self.metadata.date = path_data.get("date") self.metadata.episode = path_data.get("episode") self.metadata.season = path_data.get("season") self.metadata.series = path_data.get("title") alternative_title = path_data.get("alternative_title") if alternative_title: self.metadata.series = ( f"{self.metadata.series} {alternative_title}" )