def test_remove_file_extension(self): extension = ".ext" self.assertFalse(extension in File_name_parser.FILE_EXTENSIONS) self.assertEqual( File_name_parser.remove_file_extension( f"path-to-file/filename{extension}"), f"path-to-file/filename{extension}") for extension in File_name_parser.FILE_EXTENSIONS: self.assertEqual( File_name_parser.remove_file_extension( f"path-to-file/filename{extension}"), "path-to-file/filename") self.assertEqual( File_name_parser.remove_file_extension( f"path-to-file/funny.file.name{extension}"), "path-to-file/funny.file.name") for extension in File_name_parser.SUBTITLE_EXTENSIONS: for lang in File_name_parser.SUBTITLE_LANGS: self.assertEqual( File_name_parser.remove_file_extension( f"path-to-file/filename{lang}{extension}"), "path-to-file/filename") for extension in File_name_parser.MEDIA_EXTENSIONS: for lang in File_name_parser.SUBTITLE_LANGS: self.assertEqual( File_name_parser.remove_file_extension( f"path-to-file/filename{lang}{extension}"), f"path-to-file/filename{lang}")
def test_is_media(self): for extension in File_name_parser.MEDIA_EXTENSIONS: self.assertTrue( File_name_parser.is_media(f"path-to-file/filename{extension}")) self.assertFalse( File_name_parser.is_media( f"path-to-file/filename{extension}foo")) self.assertFalse(File_name_parser.is_media(f"path-to-file/filename"))
def test_is_subtitle(self): for extension in File_name_parser.SUBTITLE_EXTENSIONS: self.assertTrue( File_name_parser.is_subtitle( f"path-to-file/filename{extension}")) self.assertFalse( File_name_parser.is_subtitle( f"path-to-file/filename{extension}foo")) self.assertFalse( File_name_parser.is_subtitle(f"path-to-file/filename"))
def filter_show(show): file_paths = Scanner.filter_media_library(show) if show.show_name(): show_paths = [] for file_path in file_paths: if ((File_name_parser.guess_show_name(file_path) == show.show_name()) and (File_name_parser.guess_year(file_path) == show.year())): show_paths.append(file_path) file_paths = show_paths return file_paths
def test_remove_user_tags(self): tag = "[foobar]" self.assertEqual( File_name_parser.remove_user_tags( f"{tag}path-to-{tag}file/filename{tag}"), f"path-to-file/filename") self.assertEqual( File_name_parser.remove_user_tags( f"{tag} path-to-{tag} file/filename{tag} "), f"path-to-file/filename") self.assertEqual( File_name_parser.remove_user_tags( f" {tag}path-to- {tag}file/filename {tag}"), f" path-to- file/filename ")
def test_is_ova(self): term = "OVA" self.assertTrue(File_name_parser.is_ova(f"{term}as s/aa aa")) self.assertTrue(File_name_parser.is_ova(f"as s/{term}aa aa")) self.assertTrue(File_name_parser.is_ova(f"as s/aa aa{term}")) self.assertFalse(File_name_parser.is_ova(f"as s/{term.lower()}aa aa")) self.assertFalse(File_name_parser.is_ova(f"{term.lower()}as s/aa aa")) self.assertFalse(File_name_parser.is_ova(f"as s/aa aa{term.lower()}")) self.assertFalse(File_name_parser.is_ova(f"as s/aa aa"))
def test_is_ncop(self): term = "NCOP" self.assertTrue(File_name_parser.is_ncop(f"as s/{term}aa aa")) self.assertTrue(File_name_parser.is_ncop(f"{term}as s/aa aa")) self.assertTrue(File_name_parser.is_ncop(f"as s/aa aa{term}")) self.assertFalse(File_name_parser.is_ncop(f"as s/{term.lower()}aa aa")) self.assertFalse(File_name_parser.is_ncop(f"{term.lower()}as s/aa aa")) self.assertFalse(File_name_parser.is_ncop(f"as s/aa aa{term.lower()}")) self.assertFalse(File_name_parser.is_ncop(f"as s/aa aa"))
def filter_extra(extra): file_paths = Scanner.filter_season(extra) extra_paths = [] for file_path in file_paths: if File_name_parser.is_extra(file_path): extra_paths.append(file_path) file_paths = extra_paths return file_paths
def test_guess_season(self): season_num = 1 for success in [ f'show/Season {season_num}/e1.mkv', f'show/Season{season_num}/e1.mkv', f'show/Season.{season_num}/e1.mkv', f'show/S {season_num}/e1.mkv', f'show/S{season_num}/e1.mkv', f'show/S.{season_num}/e1.mkv', f'show/S{season_num}E01.mkv' ]: self.assertEqual(File_name_parser.guess_episode(success), season_num)
def filter_season(season): file_paths = Scanner.filter_show(season) if season.season_number(): season_paths = [] for file_path in file_paths: if (File_name_parser.guess_season(file_path) == season.season_number()): season_paths.append(file_path) file_paths = season_paths return file_paths
def test_movies(self): folder = "test" if __name__ != '__main__' else "" path = os.path.join(sys.path[0], folder, "test-movies") with open(path, "r") as test_movies: line = test_movies.readline() while line: if line.startswith('#') or not line.strip(): line = test_movies.readline() continue elif line.strip() == "EOF": break file_name = line.strip() line = test_movies.readline().strip() self.assertEqual(File_name_parser.guess_show_name(file_name), line) line = test_movies.readline().strip() self.assertEqual(File_name_parser.guess_year(file_name), None if line == "0" else int(line)) line = test_movies.readline()
def scan(file_paths, root_container): subtitle_cache = {} for file_path in file_paths: current_container = root_container show_name = File_name_parser.guess_show_name(file_path) year = File_name_parser.guess_year(file_path) season_number = File_name_parser.guess_season(file_path) episode_number = File_name_parser.guess_episode(file_path) is_media = File_name_parser.is_media(file_path) is_subtitle = File_name_parser.is_subtitle(file_path) is_extra = File_name_parser.is_extra(file_path) is_oad = File_name_parser.is_oad(file_path) is_ncop = File_name_parser.is_ncop(file_path) is_nced = File_name_parser.is_nced(file_path) is_ova = File_name_parser.is_ova(file_path) is_show = (season_number or episode_number or is_extra) is_movie = not is_show if not year: year = None if is_show: show = Show(root_container.path(), show_name, parent=current_container) show.set_year(year) existing = current_container.get_container(show.id()) if existing: show = existing else: current_container.containers.append(show) current_container = show if season_number: season = Season(root_container.path(), show_name, season_number, parent=current_container) existing = current_container.get_container(season.id()) if existing: season = existing else: current_container.containers.append(season) current_container = season if is_extra: extra = Extra(root_container.path(), show_name, season_number, parent=current_container) existing = current_container.get_container(extra.id()) if existing: extra = existing else: current_container.containers.append(extra) current_container = extra if episode_number or is_extra: media = Episode(file_path, episode_number, False, parent=current_container, is_oad=is_oad, is_ncop=is_ncop, is_nced=is_nced, is_ova=is_ova) existing = current_container.get_media(media.id()) if existing: media = existing else: current_container.media.append(media) elif is_movie: _show_name = show_name if not year else f'{show_name} ({year})' if is_subtitle: added = False for m in current_container.media: if (isinstance(m, Movie) and m.title() == _show_name): m.subtitles.append(file_path) added = True break if not added: subtitle_cache[_show_name] = file_path continue media = Movie(file_path, _show_name, False, parent=current_container) media.set_year(year) existing = current_container.get_media(media.id()) if existing: media = existing else: current_container.media.append(media) if _show_name in subtitle_cache.keys(): media.subtitles.append(subtitle_cache.pop(_show_name)) if media: if is_media: media.file = file_path elif is_subtitle: media.subtitles.append(file_path) return root_container
def test_is_ncop(self): term = "Extra" self.assertTrue(File_name_parser.is_extra(f"a s/{term}aa aa")) self.assertTrue(File_name_parser.is_extra(f"{term}a s/aa aa")) self.assertTrue(File_name_parser.is_extra(f"a s/aa aa{term}")) self.assertTrue(File_name_parser.is_extra(f"a s/{term.lower()}aa aa")) self.assertTrue(File_name_parser.is_extra(f"{term.lower()}as s/a aa")) self.assertTrue(File_name_parser.is_extra(f"a s/aa aa{term.lower()}")) self.assertFalse(File_name_parser.is_extra(f"a s/aa aa")) term = "NCOP" self.assertTrue(File_name_parser.is_extra(f"as s/{term}aa aa")) term = "NCED" self.assertTrue(File_name_parser.is_extra(f"as s/{term}aa aa")) term = "OVA" self.assertTrue(File_name_parser.is_extra(f"as s/{term}aa aa")) term = "OAD" self.assertTrue(File_name_parser.is_extra(f"as s/{term}aa aa"))
def test_shows(self): folder = "test" if __name__ != '__main__' else "" path = os.path.join(sys.path[0], folder, "test-shows") with open(path, "r") as test_shows: line = test_shows.readline() while line: if line.startswith('#') or not line.strip(): line = test_shows.readline() continue elif line.strip() == "EOF": break # new test block file_name = line.strip() line = test_shows.readline().strip() self.assertEqual(File_name_parser.guess_show_name(file_name), line) line = test_shows.readline().strip() self.assertEqual(File_name_parser.guess_season(file_name), int(line)) line = test_shows.readline().strip() self.assertEqual(File_name_parser.guess_episode(file_name), int(line)) line = test_shows.readline().strip() self.assertEqual(File_name_parser.guess_year(file_name), None if line == "0" else int(line)) line = test_shows.readline().strip() self.assertEqual(File_name_parser.remove_user_tags(file_name), line) line = test_shows.readline().strip() self.assertEqual(File_name_parser.remove_meta_tags(file_name), line) line = test_shows.readline().strip() self.assertEqual( File_name_parser.remove_file_extension(file_name), line) line = test_shows.readline().strip() self.assertEqual(File_name_parser.is_media(file_name), line.lower() == "true") line = test_shows.readline().strip() self.assertEqual(File_name_parser.is_subtitle(file_name), line.lower() == "true") line = test_shows.readline().strip() self.assertEqual(File_name_parser.is_oad(file_name), line.lower() == "true") line = test_shows.readline().strip() self.assertEqual(File_name_parser.is_extra(file_name), line.lower() == "true") line = test_shows.readline().strip() self.assertEqual(File_name_parser.is_ncop(file_name), line.lower() == "true") line = test_shows.readline().strip() self.assertEqual(File_name_parser.is_nced(file_name), line.lower() == "true") line = test_shows.readline().strip() self.assertEqual(File_name_parser.is_ova(file_name), line.lower() == "true") line = test_shows.readline()
def test_guess_episode(self): ep_num = 1 episode_info = [ f"EPISODE{ep_num}", f"episode{ep_num}", f"Episode{ep_num}", f"E{ep_num}", f"e{ep_num}", f"EP{ep_num}", f"ep{ep_num}", f"Ep{ep_num}", f'extra{ep_num}', f'EXTRA{ep_num}', f'Extra{ep_num}', f'OAD{ep_num}', f'NCOP{ep_num}', f'NCED{ep_num}', f'OVA{ep_num}', ] for info in episode_info: for success in [ f"path-to-file/filename {info}", f"path-to-file/file {info} name", f"path-to-file/{info} filename", f"path-to-file/filename S1{info}", f"path-to-file/file S1{info} name", f"path-to-file/S1{info} filename", f"path-to-file/filename.{info}", f"path-to-file/file.{info}.name", f"path-to-file/{info}.filename", f"path-to-file/{info}", f"path-to-file/{info}.mkv", f"path-to-file/S1{info}.mkv", f"path-to-file {info}/filename" ]: self.assertEqual(File_name_parser.guess_episode(success), ep_num) for fail in [ f"path-to-file/filename{info}", f"path-to-file/file{info}name", f"path-to-file/{info}filename" ]: self.assertEqual(File_name_parser.guess_episode(fail), File_name_parser.UNKNOWN_EPISODE) for success in [ f"path-to-file/filename {ep_num}", f"path-to-file/file {ep_num} name", f"path-to-file/{ep_num} filename", f"path-to-file/filename.{ep_num}", f"path-to-file/file.{ep_num}.name", f"path-to-file/{ep_num}.filename", f"path-to-file/{ep_num}", f"path-to-file/{ep_num}.mkv" ]: self.assertEqual(File_name_parser.guess_episode(success), ep_num) for fail in [ f"path-to-file/filename S1{ep_num}", f"path-to-file/file S1{ep_num} name", f"path-to-file/S1{ep_num} filename", f"path-to-file/S1{ep_num}.mkv" f"path-to-file/filename{ep_num}", f"path-to-file/file{ep_num}name", f"path-to-file/{ep_num}filename", f"path-to-file {ep_num}/filename", f"filename S1{ep_num}" ]: self.assertEqual(File_name_parser.guess_episode(fail), File_name_parser.UNKNOWN_EPISODE)
def test_guess_year(self): year = 2000 for tag in (f"[{year}]", f"({year})", f" {year} ", f".{year}."): self.assertEqual( File_name_parser.guess_year(f"{tag}path-to-file/filename"), year) self.assertEqual( File_name_parser.guess_year(f"path-to-{tag}file/filename"), year) self.assertEqual( File_name_parser.guess_year(f"path-to-file/filename{tag}"), year) for tag in (f' {year}', f'.{year}'): self.assertEqual( File_name_parser.guess_year(f"{tag}path-to-file/filename"), File_name_parser.UNKNOWN_YEAR) self.assertEqual( File_name_parser.guess_year(f"path-to-{tag}file/filename"), File_name_parser.UNKNOWN_YEAR) self.assertEqual( File_name_parser.guess_year(f"path-to-file/filename{tag}"), year) for tag in (f'{year} ', f'{year}.'): self.assertEqual( File_name_parser.guess_year(f"{tag}path-to-file/filename"), year) self.assertEqual( File_name_parser.guess_year(f"path-to-{tag}file/filename"), File_name_parser.UNKNOWN_YEAR) self.assertEqual( File_name_parser.guess_year(f"path-to-file/filename{tag}"), File_name_parser.UNKNOWN_YEAR) for tag in (f'{year}', f'{year}'): self.assertEqual( File_name_parser.guess_year(f"{tag}path-to-file/filename"), File_name_parser.UNKNOWN_YEAR) self.assertEqual( File_name_parser.guess_year(f"path-to-{tag}file/filename"), File_name_parser.UNKNOWN_YEAR) self.assertEqual( File_name_parser.guess_year(f"path-to-file/filename{tag}"), File_name_parser.UNKNOWN_YEAR) self.assertEqual( File_name_parser.guess_year(f"path-to-file/filename(98)"), File_name_parser.UNKNOWN_YEAR) self.assertEqual(File_name_parser.guess_year(f"path-to-file/filename"), File_name_parser.UNKNOWN_YEAR)