Exemplo n.º 1
0
    def process_result(self, video_name, video_info, chosen_sub, link,
                       session):

        # download archive
        choice_prefix = chosen_sub[:chosen_sub.find("]") + 1]
        downloader = DownloaderManager.get_downloader_by_choice_prefix(
            choice_prefix)
        datatype, archive_data, error = downloader.download_file(
            chosen_sub, link, session=session)
        if error:
            return error, []

        # process archive
        error, extract_sub_names = process_archive(
            video_name,
            video_info,
            archive_data,
            datatype,
            self.both,
            self.single,
            self.sub_identifier,
        )
        if error:
            return error, []

        # save original archive
        if self.more:
            archive_path = path.join(video_info["store_path"],
                                     chosen_sub + datatype)
            with open(archive_path, "wb") as f:
                f.write(archive_data)
            print("save original file.")

        return "", extract_sub_names
Exemplo n.º 2
0
    def process_archive(self,
                        one_video,
                        video_info,
                        sub_choice,
                        link,
                        session,
                        rename=True,
                        delete=True):
        """ 解压字幕包,返回字幕包中字幕名列表

            Return:
                message: str, 无其它错误则为空
                extract_sub_names: list
        """
        message = ''
        if self.query:
            print(prefix + ' ')
        choice_prefix = sub_choice[:sub_choice.find(']') + 1]
        datatype, sub_data_bytes, err_msg = DownloaderManager.get_downloader_by_choice_prefix(
            choice_prefix).download_file(sub_choice, link, session=session)
        if err_msg:
            return err_msg, None
        extract_sub_names = []
        if datatype not in self.support_file_list:
            # 不支持的压缩包类型
            message = 'unsupported file type ' + datatype
            return message, None
        # 获得猜测字幕名称
        # 查询模式必有返回值,自动模式无猜测值返回None
        extract_sub_names = self.extract_subtitle(one_video,
                                                  video_info['path'],
                                                  sub_choice,
                                                  datatype,
                                                  sub_data_bytes,
                                                  rename,
                                                  self.single,
                                                  self.both,
                                                  self.plex,
                                                  delete=delete)
        if not extract_sub_names:
            return message, None
        for extract_sub_name, extract_sub_type in extract_sub_names:
            extract_sub_name = extract_sub_name.split('/')[-1]
            try:
                # zipfile: Historical ZIP filename encoding
                # try cp437 encoding
                extract_sub_name = extract_sub_name. \
                    encode('cp437').decode('gbk')
            except:
                pass
            try:
                print(prefix + ' ' + extract_sub_name)
            except UnicodeDecodeError:
                print(prefix + ' ' + extract_sub_name.encode('gbk'))
        return message, extract_sub_names
Exemplo n.º 3
0
    def process_result(self, video, chosen_sub, link, session):

        # download archive
        choice_prefix = chosen_sub[:chosen_sub.find("]") + 1]
        downloader = DownloaderManager.get_downloader_by_choice_prefix(
            choice_prefix)
        datatype, data, error = downloader.download_file(chosen_sub,
                                                         link,
                                                         session=session)
        if error:
            return error, []

        # process archive or subtitles downloaded
        if datatype in ARCHIVE_TYPES:
            error, extract_subs = self.process_archive(video, data, datatype)
        elif datatype in SUB_FORMATS:
            error, extract_subs = self.process_subtitle(video, data, datatype)
        else:
            error = "unsupported file type " + datatype

        if error:
            return error, []

        for extract_sub_name, extract_sub_type in extract_subs:
            extract_sub_name = extract_sub_name.split("/")[-1]
            try:
                # zipfile: Historical ZIP filename encoding
                # try cp437 encoding
                extract_sub_name = extract_sub_name.encode("cp437").decode(
                    "gbk")
            except Exception:
                pass
            try:
                print("\nExtracted:", extract_sub_name)
            except UnicodeDecodeError:
                print("\nExtracted:".encode("gbk"),
                      extract_sub_name.encode("gbk"))

        # save original archive
        if self.more and datatype in ARCHIVE_TYPES:
            archive_path = path.join(video.sub_store_path,
                                     chosen_sub + datatype)
            with open(archive_path, "wb") as f:
                f.write(data)
            print("save original file.")

        return "", extract_subs
Exemplo n.º 4
0
    def process_archive(self,
                        one_video,
                        video_info,
                        sub_choice,
                        link,
                        session,
                        rename=True,
                        delete=True):
        """ 解压字幕包,返回字幕包中字幕名列表

            Return:
                message: str, 无其它错误则为空
                extract_sub_names: list
        """
        message = ''
        if py == 2:
            encoding = chardet.detect(sub_choice)['encoding']
            if isinstance(sub_choice, str):
                sub_choice = sub_choice.decode(encoding)
            try:
                sub_choice = sub_choice.encode(GetSubtitles.output_encode)
            except:
                if isinstance(sub_choice, str):
                    sub_choice = sub_choice.encode(encoding)
                sub_choice = sub_choice.decode('utf8')
                sub_choice = sub_choice.encode(GetSubtitles.output_encode)
        if self.query:
            print(prefix + ' ')
        choice_prefix = sub_choice[:sub_choice.find(']') + 1]
        datatype, sub_data_bytes, err_msg = DownloaderManager.get_downloader_by_choice_prefix(
            choice_prefix).download_file(sub_choice, link, session=session)
        if err_msg:
            return err_msg, None
        extract_sub_names = []
        if datatype not in self.support_file_list:
            # 不支持的压缩包类型
            message = 'unsupported file type ' + datatype
            return message, None
        # 获得猜测字幕名称
        # 查询模式必有返回值,自动模式无猜测值返回None
        extract_sub_names = self.extract_subtitle(one_video,
                                                  video_info['path'],
                                                  sub_choice,
                                                  datatype,
                                                  sub_data_bytes,
                                                  rename,
                                                  self.single,
                                                  self.both,
                                                  self.plex,
                                                  delete=delete)
        if not extract_sub_names:
            return message, None
        for extract_sub_name, extract_sub_type in extract_sub_names:
            extract_sub_name = extract_sub_name.split('/')[-1]
            try:
                # zipfile: Historical ZIP filename encoding
                # try cp437 encoding
                extract_sub_name = extract_sub_name. \
                    encode('cp437').decode('gbk')
            except:
                pass
            try:
                if py == 2:
                    if isinstance(extract_sub_name, str):
                        encoding = chardet. \
                            detect(extract_sub_name)
                        encoding = encoding['encoding']
                        # reason of adding windows-1251 check:
                        # using ZIMUZU downloader
                        # I.Robot.2004.1080p.Bluray.x264.DTS-DEFiNiTE.mkv
                        if 'ISO' in encoding \
                                or "windows-1251" in encoding:
                            encoding = 'gbk'
                        extract_sub_name = extract_sub_name. \
                            decode(encoding)
                        extract_sub_name = extract_sub_name. \
                            encode(GetSubtitles.output_encode)
                    else:
                        extract_sub_name = extract_sub_name. \
                            encode(GetSubtitles.output_encode)
                print(prefix + ' ' + extract_sub_name)
            except UnicodeDecodeError:
                print(prefix + ' ' + extract_sub_name.encode('gbk'))
        return message, extract_sub_names