diff --git a/wrolpi/files/test/test_scrape_downloader.py b/wrolpi/files/test/test_scrape_downloader.py index 33e48151..c37d789a 100644 --- a/wrolpi/files/test/test_scrape_downloader.py +++ b/wrolpi/files/test/test_scrape_downloader.py @@ -278,3 +278,25 @@ async def test_fetch_html_decodes_declared_charset(test_directory, simple_file_s content = await ScrapeHTMLDownloader.fetch_html(f'http://127.0.0.1:{port}/page.html') assert 'Zurück' in content + + +@pytest.mark.asyncio +async def test_execute_collects_multiple_suffixes(test_directory): + """The UI submits a comma-separated list of suffixes; a link matching any of them is collected.""" + prepared = PreparedScrape( + url='https://example.com/dir', + depth=1, + suffix='.pdf,.html', + max_pages=100, + destination=test_directory, + ) + + with mock.patch('wrolpi.scrape_downloader.ScrapeHTMLDownloader.fetch_html', fake_fetch_html): + executed = await scrape_html_downloader.execute_download(prepared, make_test_ctx()) + + assert sorted(executed.download_urls) == sorted([ + 'https://example.com/one.pdf', + 'https://example.com/two.pdf', + 'https://example.com/dir/three.pdf', + 'https://example.com/dir/other.html', + ]) diff --git a/wrolpi/scrape_downloader.py b/wrolpi/scrape_downloader.py index 0c74b8d3..c4d65238 100644 --- a/wrolpi/scrape_downloader.py +++ b/wrolpi/scrape_downloader.py @@ -107,7 +107,9 @@ class ScrapeHTMLDownloader(Downloader): async def execute_download(self, prepared: PreparedScrape, ctx: DownloadContext, download: Download = None) -> ExecutedScrape: - """Crawl HTML pages up to depth, collecting links that match the configured suffix.""" + """Crawl HTML pages up to depth, collecting links that match any of the configured suffixes.""" + # The UI submits a comma-separated list of suffixes. + suffixes = tuple(i.strip() for i in prepared.suffix.split(',') if i.strip()) urls = [prepared.url] download_urls: List[str] = [] page_count = 0 @@ -143,7 +145,7 @@ class ScrapeHTMLDownloader(Downloader): logger.debug(f'Not a real anchor: {a}') continue child_url = resolve_url(url, href) - if child_url and child_url.lower().endswith(prepared.suffix): + if child_url and child_url.lower().endswith(suffixes): # Found a file that the User requested. logger.info(f'ScrapeHTMLDownloader will download {child_url}') download_urls.append(child_url)