diff --git a/podcast_downloader/__main__.py b/podcast_downloader/__main__.py index e90c2b9..77d3106 100644 --- a/podcast_downloader/__main__.py +++ b/podcast_downloader/__main__.py @@ -26,8 +26,8 @@ flatten_rss_links_data, get_raw_rss_entries_from_web, limit_file_name, - only_last_entity, only_entities_from_date, + only_last_n_entities, ) @@ -78,7 +78,7 @@ def configuration_to_function_on_empty_directory( configuration_value: str, ) -> Callable[[Iterable[RSSEntity]], Iterable[RSSEntity]]: if configuration_value == "download_last": - return only_last_entity + return partial(only_last_n_entities, 1) if configuration_value == "download_all_from_feed": return lambda source: source @@ -90,6 +90,11 @@ def configuration_to_function_on_empty_directory( from_date = get_n_age_date(int(from_n_day_match[1]), local_time) return only_entities_from_date(from_date) + last_n_episodes = re.match(r"^download_last_(\d+)_episodes", configuration_value) + if last_n_episodes: + download_limit = int(last_n_episodes[1]) + return partial(only_last_n_entities, download_limit) + from_nth_day_match = re.match(r"^download_from_(.*)", configuration_value) if from_nth_day_match: day_label = parse_day_label(from_nth_day_match[1]) diff --git a/podcast_downloader/rss.py b/podcast_downloader/rss.py index 22f3c05..7885b69 100644 --- a/podcast_downloader/rss.py +++ b/podcast_downloader/rss.py @@ -110,8 +110,10 @@ def build_only_new_entities( ) -def only_last_entity(raw_rss_entries: Iterator[RSSEntity]) -> Iterator[RSSEntity]: - return islice(raw_rss_entries, 1) +def only_last_n_entities( + n: int, raw_rss_entries: Iterator[RSSEntity] +) -> Iterator[RSSEntity]: + return islice(raw_rss_entries, n) def is_entity_newer(from_date: time.struct_time, entity: RSSEntity) -> bool: diff --git a/tests/only_last_rss_entity_test.py b/tests/only_last_rss_entity_test.py deleted file mode 100644 index ee72a38..0000000 --- a/tests/only_last_rss_entity_test.py +++ /dev/null @@ -1,21 +0,0 @@ -import unittest - -from podcast_downloader.rss import only_last_entity -from commons import rss_entity_generator - - -class TestOnlyLastRSSEntity(unittest.TestCase): - def test_get_only_last_rss_entity(self): - # Assign - entity_1, entity_2, entity_3, entity_4, entity_5 = rss_entity_generator(limit=5) - - rss_entities = [entity_1, entity_2, entity_3, entity_4, entity_5] - expected = [entity_1] - - # Act - result = list(only_last_entity(rss_entities)) - - # Assert - self.assertSequenceEqual( - result, expected, "Only the last entity should be return as result" - ) diff --git a/tests/only_n_last_entities_test.py b/tests/only_n_last_entities_test.py new file mode 100644 index 0000000..2ac7a1c --- /dev/null +++ b/tests/only_n_last_entities_test.py @@ -0,0 +1,36 @@ +import unittest + +from podcast_downloader.rss import only_last_n_entities +from commons import rss_entity_generator + + +class TestOnlyNLastRSSEntity(unittest.TestCase): + def test_get_only_last_n_rss_entities_happy_path(self): + # Assign + entity_1, entity_2, entity_3, entity_4, entity_5 = rss_entity_generator(limit=5) + + rss_entities = [entity_1, entity_2, entity_3, entity_4, entity_5] + expected = [entity_1, entity_2] + + # Act + result = list(only_last_n_entities(2, rss_entities)) + + # Assert + self.assertSequenceEqual( + result, expected, "Only the last entity should be return as result" + ) + + def test_get_only_last_n_rss_entities_less_than_required(self): + # Assign + entity_1 = rss_entity_generator(limit=1) + + rss_entities = [entity_1] + expected = [entity_1] + + # Act + result = list(only_last_n_entities(2, rss_entities)) + + # Assert + self.assertSequenceEqual( + result, expected, "Only the last entity should be return as result" + )