Ejemplo n.º 1
0
def HistoryMenu():
    from support.history import get_history
    history = get_history()
    oc = SubFolderObjectContainer(title2=_("History"), replace_parent=True)

    for item in history.items[:100]:
        possible_language = item.language
        language_display = item.lang_name if not possible_language else display_language(
            possible_language)

        oc.add(
            DirectoryObject(key=Callback(ItemDetailsMenu,
                                         title=item.title,
                                         item_title=item.item_title,
                                         rating_key=item.rating_key),
                            title=u"%s (%s)" %
                            (item.item_title, _(item.mode_verbose)),
                            summary=_(u"%s in %s (%s, score: %s), %s",
                                      language_display, item.section_title,
                                      _(item.provider_name), item.score,
                                      df(item.time)),
                            thumb=item.thumb or default_thumb))

    history.destroy()

    return oc
Ejemplo n.º 2
0
    def download_subtitle(self, subtitle, rating_key, mode="m"):
        from interface.menu_helpers import set_refresh_menu_state

        item_type = subtitle.item_type
        part_id = subtitle.part_id
        metadata = get_plex_metadata(rating_key, part_id, item_type)
        providers = config.get_providers(media_type="series" if item_type == "episode" else "movies")
        scanned_parts = scan_videos([metadata], ignore_all=True, providers=providers)
        video, plex_part = scanned_parts.items()[0]

        pre_download_hook(subtitle)

        # downloaded_subtitles = {subliminal.Video: [subtitle, subtitle, ...]}
        download_subtitles([subtitle], providers=providers,
                           provider_configs=config.provider_settings,
                           pool_class=config.provider_pool, throttle_callback=config.provider_throttle)

        post_download_hook(subtitle)

        # may be redundant
        subtitle.pack_data = None

        download_successful = False

        if subtitle.content:
            try:
                save_subtitles(scanned_parts, {video: [subtitle]}, mode=mode, mods=config.default_mods)
                if mode == "m":
                    Log.Debug(u"%s: Manually downloaded subtitle for: %s", self.name, rating_key)
                    track_usage("Subtitle", "manual", "download", 1)
                elif mode == "b":
                    Log.Debug(u"%s: Downloaded better subtitle for: %s", self.name, rating_key)
                    track_usage("Subtitle", "better", "download", 1)
                download_successful = True
                refresh_item(rating_key)

            except:
                Log.Error(u"%s: Something went wrong when downloading specific subtitle: %s",
                          self.name, traceback.format_exc())
            finally:
                set_refresh_menu_state(None)

                if download_successful:
                    # store item in history
                    from support.history import get_history
                    item_title = get_title_for_video_metadata(metadata, add_section_title=False)
                    history = get_history()
                    history.add(item_title, video.id, section_title=video.plexapi_metadata["section"],
                                subtitle=subtitle,
                                mode=mode)
                    history.destroy()

                    # clear missing subtitles menu data
                    if not scheduler.is_task_running("MissingSubtitles"):
                        scheduler.clear_task_data("MissingSubtitles")
        else:
            set_refresh_menu_state(u"%s: Subtitle download failed (%s)" % (self.name, rating_key))
        return download_successful
Ejemplo n.º 3
0
    def download_subtitle(self, subtitle, rating_key, mode="m"):
        from interface.menu_helpers import set_refresh_menu_state

        item_type = subtitle.item_type
        part_id = subtitle.part_id
        metadata = get_plex_metadata(rating_key, part_id, item_type)
        scanned_parts = scan_videos(
            [metadata],
            kind="series" if item_type == "episode" else "movie",
            ignore_all=True)
        video, plex_part = scanned_parts.items()[0]

        # downloaded_subtitles = {subliminal.Video: [subtitle, subtitle, ...]}
        download_subtitles([subtitle],
                           providers=config.providers,
                           provider_configs=config.provider_settings,
                           pool_class=config.provider_pool)
        download_successful = False

        if subtitle.content:
            try:
                save_subtitles(scanned_parts, {video: [subtitle]},
                               mode=mode,
                               mods=config.default_mods)
                Log.Debug(u"%s: Manually downloaded subtitle for: %s",
                          self.name, rating_key)
                download_successful = True
                refresh_item(rating_key)
                track_usage("Subtitle", "manual", "download", 1)
            except:
                Log.Error(
                    u"%s: Something went wrong when downloading specific subtitle: %s",
                    self.name, traceback.format_exc())
            finally:
                set_refresh_menu_state(None)

                if download_successful:
                    # store item in history
                    from support.history import get_history
                    item_title = get_title_for_video_metadata(
                        metadata, add_section_title=False)
                    history = get_history()
                    history.add(
                        item_title,
                        video.id,
                        section_title=video.plexapi_metadata["section"],
                        subtitle=subtitle,
                        mode=mode)
        else:
            set_refresh_menu_state(u"%s: Subtitle download failed (%s)",
                                   self.name, rating_key)
        return download_successful
Ejemplo n.º 4
0
def HistoryMenu():
    from support.history import get_history
    history = get_history()
    oc = SubFolderObjectContainer(title2="History", replace_parent=True)

    for item in history.history_items:
        oc.add(DirectoryObject(
            key=Callback(ItemDetailsMenu, title=item.title, item_title=item.item_title,
                         rating_key=item.rating_key),
            title=u"%s (%s)" % (item.item_title, item.mode_verbose),
            summary=u"%s in %s (%s, score: %s), %s" % (item.lang_name, item.section_title,
                                                       item.provider_name, item.score, df(item.time))
        ))

    return oc
Ejemplo n.º 5
0
def migrate():
    """
    some Dict/Data migrations here, no need for a more in-depth migration path for now
    :return:
    """

    # migrate subtitle history from Dict to Data
    if "history" in Dict and Dict["history"]["history_items"]:
        Log.Debug("Running migration for history data")
        from support.history import get_history
        history = get_history()

        for item in reversed(Dict["history"]["history_items"]):
            history.add(item.item_title, item.rating_key, item.section_title, subtitle=item.subtitle, mode=item.mode,
                        time=item.time)

        del Dict["history"]
        Dict.Save()
Ejemplo n.º 6
0
def HistoryMenu():
    from support.history import get_history
    history = get_history()
    oc = SubFolderObjectContainer(title2=_("History"), replace_parent=True)

    for item in history.items[:100]:
        possible_language = item.language
        language_display = item.lang_name if not possible_language else display_language(possible_language)

        oc.add(DirectoryObject(
            key=Callback(ItemDetailsMenu, title=item.title, item_title=item.item_title,
                         rating_key=item.rating_key),
            title=u"%s (%s)" % (item.item_title, _(item.mode_verbose)),
            summary=_(u"%s in %s (%s, score: %s), %s", language_display, item.section_title,
                                                       _(item.provider_name), item.score, df(item.time)),
            thumb=item.thumb or default_thumb
        ))

    history.destroy()

    return oc
Ejemplo n.º 7
0
    def update(self, metadata, media, lang):
        Log.Debug("Sub-Zero %s, %s update called" %
                  (config.version, self.agent_type))
        intent = get_intent()

        if not media:
            Log.Error(
                "Called with empty media, something is really wrong with your setup!"
            )
            return

        item_ids = []
        try:
            config.init_subliminal_patches()
            videos = media_to_videos(media, kind=self.agent_type)

            # find local media
            update_local_media(metadata, media, media_type=self.agent_type)

            # media ignored?
            use_any_parts = False
            for video in videos:
                if is_ignored(video["id"]):
                    Log.Debug(u"Ignoring %s" % video)
                    continue
                use_any_parts = True

            if not use_any_parts:
                Log.Debug(u"Nothing to do.")
                return

            try:
                use_score = int(Prefs[self.score_prefs_key].strip())
            except ValueError:
                Log.Error(
                    "Please only put numbers into the scores setting. Exiting")
                return

            set_refresh_menu_state(media, media_type=self.agent_type)

            # scanned_video_part_map = {subliminal.Video: plex_part, ...}
            scanned_video_part_map = scan_videos(videos, kind=self.agent_type)

            downloaded_subtitles = None
            if not config.enable_agent:
                Log.Debug("Skipping Sub-Zero agent(s)")

            else:
                # downloaded_subtitles = {subliminal.Video: [subtitle, subtitle, ...]}
                downloaded_subtitles = download_best_subtitles(
                    scanned_video_part_map, min_score=use_score)
                item_ids = get_media_item_ids(media, kind=self.agent_type)

            downloaded_any = False
            if downloaded_subtitles:
                downloaded_any = any(downloaded_subtitles.values())

            if downloaded_any:
                save_subtitles(scanned_video_part_map,
                               downloaded_subtitles,
                               mods=config.default_mods)
                track_usage("Subtitle", "refreshed", "download", 1)

                for video, video_subtitles in downloaded_subtitles.items():
                    # store item(s) in history
                    for subtitle in video_subtitles:
                        item_title = get_title_for_video_metadata(
                            video.plexapi_metadata, add_section_title=False)
                        history = get_history()
                        history.add(
                            item_title,
                            video.id,
                            section_title=video.plexapi_metadata["section"],
                            subtitle=subtitle)
            else:
                # store subtitle info even if we've downloaded none
                store_subtitle_info(
                    scanned_video_part_map,
                    dict((k, []) for k in scanned_video_part_map.keys()),
                    None,
                    mode="a")

            update_local_media(metadata, media, media_type=self.agent_type)

        finally:
            # update the menu state
            set_refresh_menu_state(None)

            # notify any running tasks about our finished update
            for item_id in item_ids:
                #scheduler.signal("updated_metadata", item_id)

                # resolve existing intent for that id
                intent.resolve("force", item_id)

            Dict.Save()
Ejemplo n.º 8
0
def migrate():
    """
    some Dict/Data migrations here, no need for a more in-depth migration path for now
    :return:
    """

    # migrate subtitle history from Dict to Data
    if "history" in Dict and Dict["history"].get("history_items"):
        Log.Debug("Running migration for history data")
        from support.history import get_history
        history = get_history()

        for item in reversed(Dict["history"]["history_items"]):
            history.add(item.item_title,
                        item.rating_key,
                        item.section_title,
                        subtitle=item.subtitle,
                        mode=item.mode,
                        time=item.time)

        del Dict["history"]
        Dict.Save()

    # migrate subtitle storage from Dict to Data
    if "subs" in Dict:
        from support.storage import get_subtitle_storage
        from subzero.subtitle_storage import StoredSubtitle
        from support.plex_media import get_item

        subtitle_storage = get_subtitle_storage()

        for video_id, parts in Dict["subs"].iteritems():
            try:
                item = get_item(video_id)
            except:
                continue

            if not item:
                continue

            stored_subs = subtitle_storage.load_or_new(item)
            stored_subs.version = 1

            Log.Debug(u"Migrating %s" % video_id)

            stored_any = False
            for part_id, lang_dict in parts.iteritems():
                part_id = str(part_id)
                Log.Debug(u"Migrating %s, %s" % (video_id, part_id))

                for lang, subs in lang_dict.iteritems():
                    lang = str(lang)
                    if "current" in subs:
                        current_key = subs["current"]
                        provider_name, subtitle_id = current_key
                        sub = subs.get(current_key)
                        if sub and sub.get("title") and sub.get(
                                "mode"
                        ):  # ditch legacy data without sufficient info
                            stored_subs.title = sub["title"]
                            new_sub = StoredSubtitle(
                                sub["score"],
                                sub["storage"],
                                sub["hash"],
                                provider_name,
                                subtitle_id,
                                date_added=sub["date_added"],
                                mode=sub["mode"])

                            if part_id not in stored_subs.parts:
                                stored_subs.parts[part_id] = {}

                            if lang not in stored_subs.parts[part_id]:
                                stored_subs.parts[part_id][lang] = {}

                            Log.Debug(u"Migrating %s, %s, %s" %
                                      (video_id, part_id, current_key))

                            stored_subs.parts[part_id][lang][
                                current_key] = new_sub
                            stored_subs.parts[part_id][lang][
                                "current"] = current_key
                            stored_any = True

            if stored_any:
                subtitle_storage.save(stored_subs)

        subtitle_storage.destroy()
        del Dict["subs"]
        Dict.Save()
Ejemplo n.º 9
0
def extract_embedded_sub(**kwargs):
    rating_key = kwargs["rating_key"]
    part_id = kwargs.pop("part_id")
    stream_index = kwargs.pop("stream_index")
    with_mods = kwargs.pop("with_mods", False)
    language = Language.fromietf(kwargs.pop("language"))
    refresh = kwargs.pop("refresh", True)
    set_current = kwargs.pop("set_current", True)

    plex_item = kwargs.pop("plex_item", get_item(rating_key))
    item_type = get_item_kind_from_item(plex_item)
    part = kwargs.pop("part", get_part(plex_item, part_id))
    scanned_videos = kwargs.pop("scanned_videos", None)
    extract_mode = kwargs.pop("extract_mode", "a")

    any_successful = False

    if part:
        if not scanned_videos:
            metadata = get_plex_metadata(rating_key,
                                         part_id,
                                         item_type,
                                         plex_item=plex_item)
            scanned_videos = scan_videos([metadata],
                                         ignore_all=True,
                                         skip_hashing=True)

        for stream in part.streams:
            # subtitle stream
            if str(stream.index) == stream_index:
                is_forced = is_stream_forced(stream)
                bn = os.path.basename(part.file)

                set_refresh_menu_state(
                    _(u"Extracting subtitle %(stream_index)s of %(filename)s",
                      stream_index=stream_index,
                      filename=bn))
                Log.Info(u"Extracting stream %s (%s) of %s", stream_index,
                         str(language), bn)

                out_codec = stream.codec if stream.codec != "mov_text" else "srt"

                args = [
                    config.plex_transcoder, "-i", part.file, "-map",
                    "0:%s" % stream_index, "-f", out_codec, "-"
                ]
                output = None
                try:
                    output = subprocess.check_output(quote_args(args),
                                                     stderr=subprocess.PIPE,
                                                     shell=True)
                except:
                    Log.Error("Extraction failed: %s", traceback.format_exc())

                if output:
                    subtitle = ModifiedSubtitle(
                        language,
                        mods=config.default_mods if with_mods else None)
                    subtitle.content = output
                    subtitle.provider_name = "embedded"
                    subtitle.id = "stream_%s" % stream_index
                    subtitle.score = 0
                    subtitle.set_encoding("utf-8")

                    # fixme: speedup video; only video.name is needed
                    video = scanned_videos.keys()[0]
                    save_successful = save_subtitles(scanned_videos,
                                                     {video: [subtitle]},
                                                     mode="m",
                                                     set_current=set_current)
                    set_refresh_menu_state(None)

                    if save_successful and refresh:
                        refresh_item(rating_key)

                    # add item to history
                    item_title = get_title_for_video_metadata(
                        video.plexapi_metadata,
                        add_section_title=False,
                        add_episode_title=True)

                    history = get_history()
                    history.add(
                        item_title,
                        video.id,
                        section_title=video.plexapi_metadata["section"],
                        thumb=video.plexapi_metadata["super_thumb"],
                        subtitle=subtitle,
                        mode=extract_mode)
                    history.destroy()

                    any_successful = True

    return any_successful
Ejemplo n.º 10
0
    def run(self):
        super(SearchAllRecentlyAddedMissing, self).run()

        self.running = True
        self.prepare()

        from support.history import get_history
        history = get_history()

        now = datetime.datetime.now()
        min_score_series = int(
            Prefs["subtitles.search.minimumTVScore2"].strip())
        min_score_movies = int(
            Prefs["subtitles.search.minimumMovieScore2"].strip())
        series_providers = config.get_providers(media_type="series")
        movie_providers = config.get_providers(media_type="movies")

        is_recent_str = Prefs["scheduler.item_is_recent_age"]
        num, ident = is_recent_str.split()

        max_search_days = 0
        if ident == "days":
            max_search_days = int(num)
        elif ident == "weeks":
            max_search_days = int(num) * 7

        subtitle_storage = get_subtitle_storage()
        recent_files = subtitle_storage.get_recent_files(
            age_days=max_search_days)

        self.items_searching = len(recent_files)

        download_count = 0
        videos_with_downloads = 0

        config.init_subliminal_patches()

        Log.Info(u"%s: Searching for subtitles for %s items", self.name,
                 self.items_searching)

        def skip_item():
            self.items_searching = self.items_searching - 1
            self.percentage = int(
                self.items_done * 100 /
                self.items_searching) if self.items_searching > 0 else 100

        # search for subtitles in viable items
        try:
            for fn in recent_files:
                stored_subs = subtitle_storage.load(filename=fn)
                if not stored_subs:
                    Log.Debug("Skipping item %s because storage is empty", fn)
                    skip_item()
                    continue

                video_id = stored_subs.video_id

                # added_date <= max_search_days?
                if stored_subs.added_at + datetime.timedelta(
                        days=max_search_days) <= now:
                    Log.Debug("Skipping item %s because it's too old",
                              video_id)
                    skip_item()
                    continue

                if stored_subs.item_type == "episode":
                    min_score = min_score_series
                    providers = series_providers
                else:
                    min_score = min_score_movies
                    providers = movie_providers

                parts = []
                plex_item = get_item(video_id)

                if not plex_item:
                    Log.Info(u"%s: Item %s unknown, skipping", self.name,
                             video_id)
                    skip_item()
                    continue

                if not is_wanted(video_id, item=plex_item):
                    skip_item()
                    continue

                for media in plex_item.media:
                    parts += media.parts

                downloads_per_video = 0
                hit_providers = False
                for part in parts:
                    part_id = part.id

                    try:
                        metadata = get_plex_metadata(video_id, part_id,
                                                     stored_subs.item_type)
                    except PartUnknownException:
                        Log.Info(u"%s: Part %s:%s unknown, skipping",
                                 self.name, video_id, part_id)
                        continue

                    if not metadata:
                        Log.Info(u"%s: Part %s:%s unknown, skipping",
                                 self.name, video_id, part_id)
                        continue

                    Log.Debug(u"%s: Looking for missing subtitles: %s",
                              self.name, get_item_title(plex_item))
                    scanned_parts = scan_videos([metadata],
                                                providers=providers)

                    downloaded_subtitles = download_best_subtitles(
                        scanned_parts,
                        min_score=min_score,
                        providers=providers)
                    hit_providers = downloaded_subtitles is not None
                    download_successful = False

                    if downloaded_subtitles:
                        downloaded_any = any(downloaded_subtitles.values())
                        if not downloaded_any:
                            continue

                        try:
                            save_subtitles(scanned_parts,
                                           downloaded_subtitles,
                                           mode="a",
                                           mods=config.default_mods)
                            Log.Debug(
                                u"%s: Downloaded subtitle for item with missing subs: %s",
                                self.name, video_id)
                            download_successful = True
                            refresh_item(video_id)
                            track_usage("Subtitle", "manual", "download", 1)
                        except:
                            Log.Error(
                                u"%s: Something went wrong when downloading specific subtitle: %s",
                                self.name, traceback.format_exc())
                        finally:
                            scanned_parts = None
                            try:
                                item_title = get_title_for_video_metadata(
                                    metadata, add_section_title=False)
                                if download_successful:
                                    # store item in history
                                    for video, video_subtitles in downloaded_subtitles.items(
                                    ):
                                        if not video_subtitles:
                                            continue

                                        for subtitle in video_subtitles:
                                            downloads_per_video += 1
                                            history.add(
                                                item_title,
                                                video.id,
                                                section_title=metadata[
                                                    "section"],
                                                thumb=video.plexapi_metadata[
                                                    "super_thumb"],
                                                subtitle=subtitle,
                                                mode="a")

                                    downloaded_subtitles = None
                            except:
                                Log.Error(u"%s: DEBUG HIT: %s", self.name,
                                          traceback.format_exc())

                        Log.Debug(u"%s: Waiting %s seconds before continuing",
                                  self.name, self.PROVIDER_SLACK)
                        Thread.Sleep(self.PROVIDER_SLACK)

                download_count += downloads_per_video

                if downloads_per_video:
                    videos_with_downloads += 1

                self.items_done = self.items_done + 1
                self.percentage = int(
                    self.items_done * 100 /
                    self.items_searching) if self.items_searching > 0 else 100

                stored_subs = None

                if downloads_per_video:
                    Log.Debug(
                        u"%s: Subtitles have been downloaded, "
                        u"waiting %s seconds before continuing", self.name,
                        self.DL_PROVIDER_SLACK)
                    Thread.Sleep(self.DL_PROVIDER_SLACK)
                else:
                    if hit_providers:
                        Log.Debug(u"%s: Waiting %s seconds before continuing",
                                  self.name, self.PROVIDER_SLACK)
                        Thread.Sleep(self.PROVIDER_SLACK)
        finally:
            subtitle_storage.destroy()
            history.destroy()

        if download_count:
            Log.Debug(
                u"%s: done. Missing subtitles found for %s/%s items (%s subs downloaded)",
                self.name, videos_with_downloads, self.items_searching,
                download_count)
        else:
            Log.Debug(u"%s: done. No subtitles found for %s items", self.name,
                      self.items_searching)
Ejemplo n.º 11
0
    def update(self, metadata, media, lang):
        if not config.enable_agent:
            Log.Debug("Skipping Sub-Zero agent(s)")
            return

        Log.Debug("Sub-Zero %s, %s update called" %
                  (config.version, self.agent_type))
        intent = get_intent()

        if not media:
            Log.Error(
                "Called with empty media, something is really wrong with your setup!"
            )
            return

        item_ids = []
        try:
            config.init_subliminal_patches()
            videos = media_to_videos(media, kind=self.agent_type)

            # find local media
            update_local_media(metadata, media, media_type=self.agent_type)

            # media ignored?
            use_any_parts = False
            for video in videos:
                if is_ignored(video["id"]):
                    Log.Debug(u"Ignoring %s" % video)
                    continue
                use_any_parts = True

            if not use_any_parts:
                Log.Debug(u"Nothing to do.")
                return

            try:
                use_score = int(Prefs[self.score_prefs_key].strip())
            except ValueError:
                Log.Error(
                    "Please only put numbers into the scores setting. Exiting")
                return

            set_refresh_menu_state(media, media_type=self.agent_type)

            # scanned_video_part_map = {subliminal.Video: plex_part, ...}
            providers = config.get_providers(media_type=self.agent_type)
            try:
                scanned_video_part_map = scan_videos(videos,
                                                     providers=providers)
            except IOError, e:
                Log.Exception(
                    "Permission error, please check your folder/file permissions. Exiting."
                )
                if cast_bool(Prefs["check_permissions"]):
                    config.permissions_ok = False
                    config.missing_permissions = e.message
                return

            # auto extract embedded
            if config.embedded_auto_extract:
                if config.plex_transcoder:
                    agent_extract_embedded(scanned_video_part_map)
                else:
                    Log.Warning(
                        "Plex Transcoder not found, can't auto extract")

            # clear missing subtitles menu data
            if not scheduler.is_task_running("MissingSubtitles"):
                scheduler.clear_task_data("MissingSubtitles")

            downloaded_subtitles = None

            # debounce for self.debounce seconds
            now = datetime.datetime.now()
            if "last_call" in Dict:
                last_call = Dict["last_call"]
                if last_call + datetime.timedelta(seconds=self.debounce) > now:
                    wait = self.debounce - (now - last_call).seconds
                    if wait >= 1:
                        Log.Debug("Waiting %s seconds until continuing", wait)
                        Thread.Sleep(wait)

            # downloaded_subtitles = {subliminal.Video: [subtitle, subtitle, ...]}
            try:
                downloaded_subtitles = download_best_subtitles(
                    scanned_video_part_map,
                    min_score=use_score,
                    throttle_time=self.debounce,
                    providers=providers)
            except:
                Log.Exception(
                    "Something went wrong when downloading subtitles")

            if downloaded_subtitles is not None:
                Dict["last_call"] = datetime.datetime.now()

            item_ids = get_media_item_ids(media, kind=self.agent_type)

            downloaded_any = False
            if downloaded_subtitles:
                downloaded_any = any(downloaded_subtitles.values())

            if downloaded_any:
                save_successful = False
                try:
                    save_successful = save_subtitles(scanned_video_part_map,
                                                     downloaded_subtitles,
                                                     mods=config.default_mods)
                except:
                    Log.Exception("Something went wrong when saving subtitles")

                track_usage("Subtitle", "refreshed", "download", 1)

                # store SZ meta info even if download wasn't successful
                if not save_successful:
                    self.store_blank_subtitle_metadata(scanned_video_part_map)

                else:
                    for video, video_subtitles in downloaded_subtitles.items():
                        # store item(s) in history
                        for subtitle in video_subtitles:
                            item_title = get_title_for_video_metadata(
                                video.plexapi_metadata,
                                add_section_title=False)
                            history = get_history()
                            history.add(item_title,
                                        video.id,
                                        section_title=video.
                                        plexapi_metadata["section"],
                                        subtitle=subtitle)
                            history.destroy()
            else:
                # store SZ meta info even if we've downloaded none
                self.store_blank_subtitle_metadata(scanned_video_part_map)

            update_local_media(metadata, media, media_type=self.agent_type)
Ejemplo n.º 12
0
def extract_embedded_sub(**kwargs):
    rating_key = kwargs["rating_key"]
    part_id = kwargs.pop("part_id")
    stream_index = kwargs.pop("stream_index")
    with_mods = kwargs.pop("with_mods", False)
    language = Language.fromietf(kwargs.pop("language"))
    refresh = kwargs.pop("refresh", True)
    set_current = kwargs.pop("set_current", True)

    plex_item = kwargs.pop("plex_item", get_item(rating_key))
    item_type = get_item_kind_from_item(plex_item)
    part = kwargs.pop("part", get_part(plex_item, part_id))
    scanned_videos = kwargs.pop("scanned_videos", None)
    extract_mode = kwargs.pop("extract_mode", "a")

    any_successful = False

    if part:
        if not scanned_videos:
            metadata = get_plex_metadata(rating_key, part_id, item_type, plex_item=plex_item)
            scanned_videos = scan_videos([metadata], ignore_all=True, skip_hashing=True)

        for stream in part.streams:
            # subtitle stream
            if str(stream.index) == stream_index:
                is_forced = is_stream_forced(stream)
                bn = os.path.basename(part.file)

                set_refresh_menu_state(_(u"Extracting subtitle %(stream_index)s of %(filename)s",
                                         stream_index=stream_index,
                                         filename=bn))
                Log.Info(u"Extracting stream %s (%s) of %s", stream_index, str(language), bn)

                out_codec = stream.codec if stream.codec != "mov_text" else "srt"

                args = [
                    config.plex_transcoder, "-i", part.file, "-map", "0:%s" % stream_index, "-f", out_codec, "-"
                ]

                cmdline = quote_args(args)
                Log.Debug(u"Calling: %s", cmdline)
                if mswindows:
                    Log.Debug("MSWindows: Fixing encoding")
                    cmdline = cmdline.encode("mbcs")

                output = None
                try:
                    output = subprocess.check_output(cmdline, stderr=subprocess.PIPE, shell=True)
                except:
                    Log.Error("Extraction failed: %s", traceback.format_exc())

                if output:
                    subtitle = ModifiedSubtitle(language, mods=config.default_mods if with_mods else None)
                    subtitle.content = output
                    subtitle.provider_name = "embedded"
                    subtitle.id = "stream_%s" % stream_index
                    subtitle.score = 0
                    subtitle.set_encoding("utf-8")

                    # fixme: speedup video; only video.name is needed
                    video = scanned_videos.keys()[0]
                    save_successful = save_subtitles(scanned_videos, {video: [subtitle]}, mode="m",
                                                     set_current=set_current)
                    set_refresh_menu_state(None)

                    if save_successful and refresh:
                        refresh_item(rating_key)

                    # add item to history
                    item_title = get_title_for_video_metadata(video.plexapi_metadata,
                                                              add_section_title=False, add_episode_title=True)

                    history = get_history()
                    history.add(item_title, video.id, section_title=video.plexapi_metadata["section"],
                                thumb=video.plexapi_metadata["super_thumb"],
                                subtitle=subtitle, mode=extract_mode)
                    history.destroy()

                    any_successful = True

    return any_successful
Ejemplo n.º 13
0
    def update(self, metadata, media, lang):
        if not config.enable_agent:
            Log.Debug("Skipping Sub-Zero agent(s)")
            return

        Log.Debug("Sub-Zero %s, %s update called" % (config.version, self.agent_type))

        if not media:
            Log.Error("Called with empty media, something is really wrong with your setup!")
            return

        intent = get_intent()

        item_ids = []
        try:
            config.init_subliminal_patches()
            all_videos = media_to_videos(media, kind=self.agent_type)

            # media ignored?
            ignore_parts_cleanup = []
            videos = []
            for video in all_videos:
                if not is_wanted(video["id"], item=video["item"]):
                    Log.Debug(u'Skipping "%s"' % video["filename"])
                    ignore_parts_cleanup.append(video["path"])
                    continue
                videos.append(video)

            # find local media
            update_local_media(all_videos, ignore_parts_cleanup=ignore_parts_cleanup)

            if not videos:
                Log.Debug(u"Nothing to do.")
                return

            try:
                use_score = int(Prefs[self.score_prefs_key].strip())
            except ValueError:
                Log.Error("Please only put numbers into the scores setting. Exiting")
                return

            set_refresh_menu_state(media, media_type=self.agent_type)

            # scanned_video_part_map = {subliminal.Video: plex_part, ...}
            providers = config.get_providers(media_type=self.agent_type)
            try:
                scanned_video_part_map = scan_videos(videos, providers=providers)
            except IOError, e:
                Log.Exception("Permission error, please check your folder/file permissions. Exiting.")
                if cast_bool(Prefs["check_permissions"]):
                    config.permissions_ok = False
                    config.missing_permissions = e.message
                return

            # auto extract embedded
            if config.embedded_auto_extract:
                if config.plex_transcoder:
                    agent_extract_embedded(scanned_video_part_map)
                else:
                    Log.Warn("Plex Transcoder not found, can't auto extract")

            # clear missing subtitles menu data
            if not scheduler.is_task_running("MissingSubtitles"):
                scheduler.clear_task_data("MissingSubtitles")

            downloaded_subtitles = None

            # debounce for self.debounce seconds
            now = datetime.datetime.now()
            if "last_call" in Dict:
                last_call = Dict["last_call"]
                if last_call + datetime.timedelta(seconds=self.debounce) > now:
                    wait = self.debounce - (now - last_call).seconds
                    if wait >= 1:
                        Log.Debug("Waiting %s seconds until continuing", wait)
                        Thread.Sleep(wait)

            # downloaded_subtitles = {subliminal.Video: [subtitle, subtitle, ...]}
            try:
                downloaded_subtitles = download_best_subtitles(scanned_video_part_map, min_score=use_score,
                                                               throttle_time=self.debounce, providers=providers)
            except:
                Log.Exception("Something went wrong when downloading subtitles")

            if downloaded_subtitles is not None:
                Dict["last_call"] = datetime.datetime.now()

            item_ids = get_media_item_ids(media, kind=self.agent_type)

            downloaded_any = False
            if downloaded_subtitles:
                downloaded_any = any(downloaded_subtitles.values())

            if downloaded_any:
                save_successful = False
                try:
                    save_successful = save_subtitles(scanned_video_part_map, downloaded_subtitles,
                                                     mods=config.default_mods)
                except:
                    Log.Exception("Something went wrong when saving subtitles")

                track_usage("Subtitle", "refreshed", "download", 1)

                # store SZ meta info even if download wasn't successful
                if not save_successful:
                    self.store_blank_subtitle_metadata(scanned_video_part_map)

                else:
                    for video, video_subtitles in downloaded_subtitles.items():
                        # store item(s) in history
                        for subtitle in video_subtitles:
                            history = get_history()
                            item_title = get_title_for_video_metadata(video.plexapi_metadata, add_section_title=False)
                            history.add(item_title, video.id, section_title=video.plexapi_metadata["section"],
                                        thumb=video.plexapi_metadata["super_thumb"],
                                        subtitle=subtitle)
                            history.destroy()
            else:
                # store SZ meta info even if we've downloaded none
                self.store_blank_subtitle_metadata(scanned_video_part_map)

            update_local_media(videos)