mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-22 16:06:19 -05:00
Scrape: match any of the comma-separated suffixes
The UI's File Suffixes field accepts a comma-separated list, but the downloader compared each link against the whole string, so a scrape with more than one suffix never matched anything. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
22b8b9c878
commit
1670b655b2
2 changed files with 26 additions and 2 deletions
|
|
@ -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 '<a href="zurück.txt">Zurück</a>' 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',
|
||||
])
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue