def test_guess_with_fail(self): success, subname = guess_subtitle( TestGuessSubtitle.test_archive_list2, TestGuessSubtitle.test_movie_info, ) self.assertEqual((success, subname), TestGuessSubtitle.test_archive_result2)
def process_archive( self, video, archive_data, datatype, ): """ 解压字幕包,返回解压字幕名列表 params: video: Video object archive_data: binary archive data datatype: str, archive type return: error: str, error message extract_subs: list, [<subname, subtype>, ...] """ error = "" if datatype not in ARCHIVE_TYPES: error = "unsupported file type " + datatype return error, [] sub_lists_dict = get_file_list(archive_data, datatype) if len(sub_lists_dict) == 0: error = "no subtitle in this archive" return error, [] # get subtitles to extract if not self.single: success, sub_name = guess_subtitle(list(sub_lists_dict.keys()), video.info) if not success: error = "no guess result in auto mode" return error, [] else: sub_name = choose_subtitle(list(sub_lists_dict.keys())) # build new names sub_title, sub_type = path.splitext(sub_name) extract_subs = [[sub_name, sub_type]] if self.both: another_sub_type = ".srt" if sub_type == ".ass" else ".ass" another_sub = sub_name.replace(sub_type, another_sub_type) another_sub = path.basename(another_sub) for subname in list(sub_lists_dict.keys()): if another_sub in subname: extract_subs.append([subname, another_sub_type]) break if len(extract_subs) == 1: print("no %s subtitles in this archive" % another_sub_type) # delete existed subtitles video.delete_existed_subtitles() # extract subtitles for one_sub, one_sub_type in extract_subs: sub_new_name = video.name + video.sub_identifier + one_sub_type extract_path = path.join(video.sub_store_path, sub_new_name) with open(extract_path, "wb") as sub: file_handler = sub_lists_dict[one_sub] sub.write(file_handler.read(one_sub)) return error, extract_subs
def test_guess_with_empty_list(self): success, subname = guess_subtitle([], {}) self.assertEqual((success, subname), (False, None))