def __init__(self, name, query, single, more, both, over, plex, debug, sub_num, downloader, sub_path): self.video_format_list = [ '.webm', '.mkv', '.flv', '.vob', '.ogv', '.ogg', '.drc', '.gif', '.gifv', '.mng', '.avi', '.mov', '.qt', '.wmv', '.yuv', '.rm', '.rmvb', '.asf', '.amv', '.mp4', '.m4p', '.m4v', 'mpg', '.mp2', '.mpeg', '.mpe', '.mpv', '.mpg', '.m2v', '.svi', '.3gp', '.3g2', '.mxf', '.roq', '.nsv', '.flv', '.f4v', '.f4p', '.f4a', '.f4b' ] self.sub_format_list = ['.ass', '.srt', '.ssa', '.sub'] self.support_file_list = ['.zip', '.rar', '.7z'] self.arg_name = name self.sub_store_path = sub_path self.both = both self.query, self.single = query, single self.more, self.over = more, over if not sub_num: self.sub_num = 5 else: self.sub_num = int(sub_num) self.plex = plex self.debug = debug self.s_error = '' self.f_error = '' if not downloader: self.downloader = DownloaderManager.downloaders else: if downloader not in DownloaderManager.downloader_names: print("\nNO SUCH DOWNLOADER:", "PLEASE CHOOSE FROM", ', '.join(DownloaderManager.downloader_names), '\n') sys.exit(1) self.downloader = [ DownloaderManager.get_downloader_by_name(downloader) ] self.failed_list = [] # [{'name', 'path', 'error', 'trace_back'}
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
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
def __init__( self, name, query, single, more, both, over, plex, debug, sub_num, downloader, sub_path, ): self.arg_name = name self.both = both self.query, self.single = query, single self.more, self.over = more, over if not sub_num: self.sub_num = 5 else: self.sub_num = int(sub_num) self.plex = plex self.debug = debug self.s_error = "" self.f_error = "" if not downloader: self.downloader = DownloaderManager.downloaders else: if downloader not in DownloaderManager.downloader_names: print( "\nNO SUCH DOWNLOADER:", "PLEASE CHOOSE FROM", ", ".join(DownloaderManager.downloader_names), "\n", ) sys.exit(1) self.downloader = [ DownloaderManager.get_downloader_by_name(downloader) ] self.failed_list = [] # [{'name', 'path', 'error', 'trace_back'} self.sub_identifier = "" if not self.plex else ".chi" self.sub_store_path = sub_path.replace('"', "") if not path.isdir(self.sub_store_path): if self.sub_store_path: print("store path is invalid: " + self.sub_store_path) self.sub_store_path = "" else: print("subtitles will be saved to: " + self.sub_store_path)
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
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