class TestExceptionsAreRaised(object): def setUp(self): files = 'tests/data/files' working = 'tests/data/working' self.tv = TvRenamr(working, log_level='critical') for fn in os.listdir(files): shutil.copy(os.path.join(files, fn), os.path.join(working, fn)) def tearDown(self): working = 'tests/data/working' for fn in os.listdir(working): os.remove(os.path.join(working,fn)) def test_unexpected_format_exception_should_be_raised_when_unrecognised_file_format(self): assert_raises(UnexpectedFormatException, self.tv.extract_details_from_file, 'chuck.avi') def test_episode_not_found_exception_should_be_raised_when_episode_not_found(self): credentials = self.tv.extract_details_from_file('chuck.s04e05.avi') assert_raises(EpisodeNotFoundException, self.tv.retrieve_episode_name, **credentials) def test_episode_already_exists_in_folder_exception_is_raised_when_new_file_name_already_exists_in_folder(self): fn = 'chuck.s02e05.avi' credentials = self.tv.extract_details_from_file(fn) credentials['title'] = self.tv.retrieve_episode_name(**credentials) path = self.tv.build_path(organise=False, **credentials) assert_raises(EpisodeAlreadyExistsInDirectoryException, self.tv.rename, fn, path) def test_incorrect_custom_regular_expression_syntax_exception_is_raised_when_any_of_the_custom_regular_expression_string_is_missing_the_defined_three_syntax_snippets(self): fn = 'chuck.s02e05.avi' assert_raises(IncorrectCustomRegularExpressionSyntaxException, self.tv.extract_details_from_file, fn, '.')
class TestTvRage(object): working = 'tests/data/working' def setup(self): files = 'tests/data/files' self.tv = TvRenamr(self.working, log_level='critical') for fn in os.listdir(files): shutil.copy(os.path.join(files, fn), os.path.join(self.working, fn)) def tearDown(self): for fn in os.listdir(self.working): os.remove(os.path.join(self.working,fn)) def test_searching_tv_rage_with_an_ambiguous_name_returns_the_correct_show(self): self.tv.retrieve_episode_name(library='tvrage', **{'show':'the o.c.', 'season':'03', 'episode':'04'}) assert_equal(self.tv.format_show_name('the o.c.', the=False), 'The O.C.') def test_searching_tv_rage_for_an_incorrect_name_returns_a_show_not_found_exception(self): assert_raises(ShowNotFoundException, self.tv.retrieve_episode_name, library='tvrage', **{'show':'west wing', 'season':'04', 'episode':'01'}) def test_searching_tv_rage_for_an_episode_that_does_not_exist_returns_an_episode_not_found_exception(self): assert_raises(EpisodeNotFoundException, self.tv.retrieve_episode_name, library='tvrage', **{'show':'chuck', 'season':'03', 'episode':'32'}) def test_searching_tv_rage_for_a_specific_episode_returns_the_correct_episode(self): fn = 'the.big.bang.theory.S03E01.HDTV.XviD-NoTV.avi' credentials = self.tv.extract_details_from_file(fn) title = self.tv.retrieve_episode_name(library='tvrage', **credentials) assert_equals(title, 'The Electric Can Opener Fluctuation') def test_tv_rage_returns_a_formatted_show_name(self): fn = 'the.big.bang.theory.S03E01.HDTV.XviD-NoTV.avi' credentials = self.tv.extract_details_from_file(fn) self.tv.retrieve_episode_name(library='tvrage', **credentials) assert_equals(self.tv.format_show_name(credentials['show'], the=False), 'The Big Bang Theory')
def rename(self, details): working, filename = details try: tv = TvRenamr(working, self.config, options.debug, options.dry) episode = Episode(tv.extract_details_from_file(filename, user_regex=options.regex)) if options.show: episode.show_name = options.show if options.season: episode.season = options.season if options.episode: episode.episode = options.episode episode.title = tv.retrieve_episode_name(episode, library=options.library, canonical=options.canonical) episode.show_name = tv.format_show_name(episode.show_name, the=options.the, override=options.show_override) path = tv.build_path( episode, rename_dir=options.rename_dir, organise=options.organise, format=options.output_format ) tv.rename(filename, path) except (ConfigNotFoundException, NoMoreLibrariesException, NoNetworkConnectionException): if options.dry or options.debug: self._stop_dry_run() sys.exit(1) except ( EmptyEpisodeNameException, EpisodeAlreadyExistsInDirectoryException, EpisodeNotFoundException, IncorrectCustomRegularExpressionSyntaxException, InvalidXMLException, OutputFormatMissingSyntaxException, ShowNotFoundException, UnexpectedFormatException, ): pass except Exception as err: if options.debug: # In debug mode, show the full traceback. raise log.critical("tvr: critical error: %s" % str(err)) sys.exit(1)
def rename(self, details): working, filename = details try: tv = TvRenamr(working, self.config, options.debug, options.dry) credentials = tv.extract_details_from_file(filename, \ user_regex=options.regex) if options.season: credentials['season'] = options.season if options.episode: credentials['episode'] = options.episode credentials['title'] = tv.retrieve_episode_name( \ library=options.library, \ canonical=options.canonical, \ **credentials) credentials['show'] = tv.format_show_name( \ show=credentials['show'], the=options.the,\ override=options.name) path = tv.build_path(rename_dir=options.rename_dir, \ organise=options.organise, \ format=options.output_format, **credentials) tv.rename(filename, path) except (ConfigNotFoundException, NoNetworkConnectionException): if options.dry or options.debug: self.__stop_dry_run() exit() except (EmptyEpisodeNameException, \ EpisodeAlreadyExistsInDirectoryException, \ EpisodeNotFoundException, \ IncorrectCustomRegularExpressionSyntaxException, \ OutputFormatMissingSyntaxException, ShowNotFoundException, \ UnexpectedFormatException, XMLEmptyException): pass except Exception as err: if options.debug: log.critical(err) pass
class TestMain(object): working = 'tests/data/working' def setUp(self): files = 'tests/data/files' self.tv = TvRenamr(self.working, log_level='critical') for fn in os.listdir(files): shutil.copy(os.path.join(files, fn), os.path.join(self.working, fn)) def tearDown(self): for fn in os.listdir(self.working): os.remove(os.path.join(self.working,fn)) def test_instantiate_core(self): assert_true(isinstance(TvRenamr("/", log_level='critical'), TvRenamr)) def test_passing_in_a_show_name_renames_a_file_using_that_name(self): fn = 'avatar.s1e08.blah.HDTV.XViD.avi' credentials = self.tv.extract_details_from_file(fn) credentials['show'] = 'Avatar: The Last Airbender' credentials['title'] = self.tv.retrieve_episode_name(**credentials) path = self.tv.build_path(organise=False, **credentials) self.tv.rename(fn, path) assert_true(os.path.isfile(os.path.join(self.working, 'Avatar, The Last Airbender - 108 - Winter Solstice (2), Avatar Roku.avi'))) def test_passing_in_a_season_number_to_retrieve_episode_name_returns_the_correct_episode_name_from_that_season(self): credentials = self.tv.extract_details_from_file('chuck.s1e08.blah.avi') credentials['season'] ='2' assert_equal(self.tv.retrieve_episode_name(**credentials), 'Chuck Versus the Gravitron') def test_passing_in_a_season_number_renames_a_file_using_that_season_number(self): fn = 'chuck.s1e08.blah.HDTV.XViD.avi' credentials = self.tv.extract_details_from_file(fn) credentials['season'] = '2' credentials['title'] = self.tv.retrieve_episode_name(**credentials) credentials['show'] = self.tv.format_show_name(credentials['show'], the=False) path = self.tv.build_path(organise=False, **credentials) self.tv.rename(fn, path) assert_true(os.path.isfile(os.path.join(self.working, 'Chuck - 208 - Chuck Versus the Gravitron.avi'))) def test_passing_in_an_episode_number_returns_the_correct_episode_title(self): fn = 'chuck.s1e08.blah.HDTV.XViD.avi' credentials = self.tv.extract_details_from_file(fn) credentials['episode'] = '9' credentials['title'] = self.tv.retrieve_episode_name(**credentials) credentials['show'] = self.tv.format_show_name(credentials['show'], the=None) path = self.tv.build_path(organise=False, **credentials) self.tv.rename(fn, path) assert_true(os.path.isfile(os.path.join(self.working, 'Chuck - 109 - Chuck Versus the Imported Hard Salami.avi'))) def test_passing_in_an_episode_number_renames_a_file_using_that_episode_number(self): credentials = self.tv.extract_details_from_file('chuck.s1e08.blah.HDTV.XViD.avi') credentials['episode'] = '9' assert_equal(self.tv.retrieve_episode_name(**credentials), 'Chuck Versus the Imported Hard Salami') def test_setting_the_position_of_a_shows_leading_the_to_the_end_of_the_file_name(self): fn = 'The.Big.Bang.Theory.S03E01.HDTV.XviD-NoTV.avi' credentials = self.tv.extract_details_from_file(fn) credentials['title'] = self.tv.retrieve_episode_name(**credentials) credentials['show'] = self.tv.format_show_name(credentials['show'], the=True) path = self.tv.build_path(organise=False, **credentials) self.tv.rename(fn, path) assert_true(os.path.isfile(os.path.join(self.working, 'Big Bang Theory, The - 301 - The Electric Can Opener Fluctuation.avi'))) def test_setting_an_episodes_format_as_name_season_episode_title(self): fn = 'chuck.s1e08.blah.HDTV.XViD.avi' credentials = self.tv.extract_details_from_file(fn) credentials['title'] = self.tv.retrieve_episode_name(**credentials) credentials['show'] = self.tv.format_show_name(credentials['show'], the=False) assert_equals(self.tv.build_path(rename_dir=self.working, organise=False, format='%n - %s%e - %t%x', **credentials), 'tests/data/working/Chuck - 108 - Chuck Versus the Truth.avi') def test_setting_an_episodes_format_as_season_episode_title_name(self): fn = 'chuck.s1e08.blah.HDTV.XViD.avi' credentials = self.tv.extract_details_from_file(fn) credentials['title'] = self.tv.retrieve_episode_name(**credentials) credentials['show'] = self.tv.format_show_name(credentials['show'], the=False) assert_equals(self.tv.build_path(rename_dir=self.working, organise=False, format='%s - %e - %t - %n%x', **credentials), 'tests/data/working/1 - 08 - Chuck Versus the Truth - Chuck.avi') def test_setting_an_episodes_format_as_title_episode_season_name(self): fn = 'chuck.s1e08.blah.HDTV.XViD.avi' credentials = self.tv.extract_details_from_file(fn) credentials['title'] = self.tv.retrieve_episode_name(**credentials) credentials['show'] = self.tv.format_show_name(credentials['show'], the=False) assert_equals(self.tv.build_path(rename_dir=self.working, organise=False, format='%t - %e - %s - %n%x', **credentials), 'tests/data/working/Chuck Versus the Truth - 08 - 1 - Chuck.avi') def test_extracting_season_from_file_format_s0e00(self): credentials = self.tv.extract_details_from_file("chuck.s2e06.avi") assert_equal(credentials['season'], '2') def test_extracting_season_from_file_format_s00e00(self): credentials = self.tv.extract_details_from_file("chuck.s20e05.avi") assert_equal(credentials['season'], '20') def test_extracting_episode_from_file_format_s0e00(self): credentials = self.tv.extract_details_from_file('chuck.s2e05.avi') assert_equal(credentials['episode'], '05') def test_extracting_episode_from_file_format_s00e00(self): credentials = self.tv.extract_details_from_file('chuck.s20e05') assert_equal(credentials['episode'], '05') def test_extracting_season_from_file_format_0x00(self): credentials = self.tv.extract_details_from_file("chuck.2x05.avi") assert_equal(credentials['season'], '2') def test_extracting_season_from_file_format_00x00(self): credentials = self.tv.extract_details_from_file("chuck.20x05.avi") assert_equal(credentials['season'], '20') def test_extracting_episode_from_file_format_0x00(self): credentials = self.tv.extract_details_from_file('chuck.2x05.avi') assert_equal(credentials['episode'], '05') def test_extracting_episode_from_file_format_00x00(self): credentials = self.tv.extract_details_from_file('chuck.20x05') assert_equal(credentials['episode'], '05') def test_extracting_season_from_file_format_000(self): credentials = self.tv.extract_details_from_file("chuck.205.avi") assert_equal(credentials['season'], '2') def test_extracting_season_from_file_format_0000(self): credentials = self.tv.extract_details_from_file("chuck.2005.avi") assert_equal(credentials['season'], '20') def test_extracting_episode_from_file_format_000(self): credentials = self.tv.extract_details_from_file('chuck.205.avi') assert_equal(credentials['episode'], '05') def test_extracting_episode_from_file_format_0000(self): credentials = self.tv.extract_details_from_file('chuck.2005') assert_equal(credentials['episode'], '05') def test_extracting_season_with_custom_regular_expression_passing_in_season_and_episode_digit_lengths_from_file_format_000(self): credentials = self.tv.extract_details_from_file('chuck.025', user_regex='%n.%s{2}%e{1}') assert_equal(credentials['season'], '02') def test_extracting_episode_with_custom_regular_expression_passing_in_season_and_episode_digit_lengths_from_file_format_000(self): credentials = self.tv.extract_details_from_file('chuck.025', user_regex='%n.%s{2}%e{1}') assert_equal(credentials['episode'], '5') def test_extracting_season_with_custom_regular_expression_passing_in_season_digit_lengths_from_file_format_000(self): credentials = self.tv.extract_details_from_file('chuck.0250', user_regex='%n.%s{2}%e') assert_equal(credentials['season'], '02') def test_extracting_season_with_custom_regular_expression_passing_in_episode_digit_lengths_from_file_format_000(self): credentials = self.tv.extract_details_from_file('chuck.025', user_regex='%n.%s%e{1}') assert_equal(credentials['episode'], '5') # def test_removing_the_part_section_from_an_episode_in_a_multiple_episode_group(self): # # need to find a way to force the use of part in the name. # fn = 'stargate.sg-1.s9e03.blah.HDTV.XViD.avi' # credentials = self.tv.extract_details_from_file(fn) # names = self.tv.retrieve_episode_name(show=credentials['show'], season=credentials['season'], episode=credentials['episode']) # name = self.tv.remove_part_from_multiple_episodes(names[0]) # new_fn = self.tv.set_output_format(show=names[0], season=credentials['season'], episode=credentials['episode'], title=names[1]) # path = self.tv.build_path(new_filename=new_fn, extension=credentials[3]) # self.tv.rename(fn, path) # assert_true(os.path.isfile(os.path.join(self.working, 'Stargate SG-1 - 903 - Origin (3).avi'))) # def test_replacing_a_show_name_from_the_exceptions_file_returns_the_correct_show_name(self): # fn = 'american.dad.s2e08.foo.bar.avi' # credentials = self.tv.extract_details_from_file(fn) # show_name = self.tv.convert_show_names_using_exceptions_file('tests/exceptions.txt', credentials['show']) # assert_equal(show_name, 'american dad!') # def test_replacing_a_show_name_from_the_exceptions_file_renames_a_file_correctly(self): # fn = 'american.dad.s2e08.foo.bar.avi' # credentials = self.tv.extract_details_from_file(fn) # credentials['show'] = self.tv.convert_show_names_using_exceptions_file('tests/exceptions.txt', credentials['show']) # title = self.tv.retrieve_episode_name(show=credentials['show'], season=credentials['season'], episode=credentials['episode']) # credentials['show'] = title['show'] # credentials['title'] = title['title'] # path = self.tv.build_path(show=credentials['show'], season=credentials['season'], episode=credentials['episode'], title=credentials['title'], extension=credentials['extension']) # self.tv.rename(fn, path) # assert_true(os.path.isfile(os.path.join(self.working, 'American Dad! - 208 - Irregarding Steve.avi'))) #
class TestAutoMoving(object): working = 'tests/data/working' organise = True organised = 'tests/data/organised' def setUp(self): files = 'tests/data/files' self.tv = TvRenamr(self.working, log_level='critical') for fn in os.listdir(files): shutil.copy(os.path.join(files, fn), self.working) def tearDown(self): shutil.rmtree(self.working) os.mkdir(self.working) shutil.rmtree(self.organised) os.mkdir(self.organised) def test_using_organise_renames_the_file_correctly(self): fn = 'chuck.s1e06.foo.HD.avi' credentials = self.tv.extract_details_from_file(fn) credentials['title'] = self.tv.retrieve_episode_name(**credentials) credentials['show'] = self.tv.format_show_name(credentials['show'], the=False) path = self.tv.build_path(organise=self.organise, rename_dir=self.organised, **credentials) self.tv.rename(fn, path) assert_true(os.path.isfile(os.path.join(self.organised + '/Chuck/Season 1', 'Chuck - 106 - Chuck Versus the Sandworm.avi'))) def test_using_organise_moves_the_file_to_the_correct_folder(self): fn = 'stargate.sg-1.s10e18.xvid.avi' credentials = self.tv.extract_details_from_file(fn) credentials['title'] = self.tv.retrieve_episode_name(**credentials) credentials['show'] = self.tv.format_show_name(credentials['show'], the=False) path = self.tv.build_path(organise=self.organise, rename_dir=self.organised, **credentials) self.tv.rename(fn, path) for fn in os.listdir(self.organised): if fn == 'Stargate SG-1': full_path = fn for other in os.listdir(os.path.join(self.organised,fn)): if other == 'Season 10': full_path = full_path +'/'+ other +'/' for fn in os.listdir(os.path.join(self.organised,full_path)): full_path = full_path + fn assert_equal(full_path, 'Stargate SG-1/Season 10/Stargate SG-1 - 1018 - Family Ties.avi') def test_using_organise_returns_the_correct_path_based_on_the_episode(self): credentials = self.tv.extract_details_from_file('true.blood.0205.avi') credentials['title'] = self.tv.retrieve_episode_name(**credentials) credentials['show'] = self.tv.format_show_name(credentials['show'], the=False) path = self.tv.build_path(organise=self.organise, rename_dir=self.organised, **credentials) assert_equal(path, 'tests/data/organised/True Blood/Season 2/True Blood - 205 - Never Let Me Go.avi') def test_moving_the_leading_the_to_the_end_of_a_show_name_causes_the_show_folder_name_to_follow_suit_when_using_organise(self): fn = 'The.Big.Bang.Theory.S03E01.HDTV.XviD-NoTV.avi' credentials = self.tv.extract_details_from_file(fn) credentials['title'] = self.tv.retrieve_episode_name(**credentials) credentials['show'] = self.tv.format_show_name(credentials['show'], the=True) path = self.tv.build_path(organise=self.organise, rename_dir=self.organised, **credentials) self.tv.rename(fn, path) assert_true(os.path.isdir(os.path.join(self.organised, 'Big Bang Theory, The/Season 3')))