Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

seed_url → urls_file #50

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions tests/test_ecommerce.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,17 +395,18 @@ def test_metadata():
"title": "URL",
"type": "string",
},
"seed_url": {
"urls_file": {
"default": "",
"description": (
"URL that point to a list of URLs to crawl, e.g. "
"URL that point to a plain-text file with a list of "
"URLs to crawl, e.g. "
"https://example.com/url-list.txt. The linked list "
"must contain 1 URL per line."
),
"exclusiveRequired": True,
"group": "inputs",
"pattern": r"^https?://[^:/\s]+(:\d{1,5})?(/[^\s]*)*(#[^\s]*)?$",
"title": "Seed URL",
"title": "URLs file",
"type": "string",
},
"geolocation": {
Expand Down Expand Up @@ -698,7 +699,7 @@ def test_input_multiple():
EcommerceSpider.from_crawler(
crawler,
url="https://a.example",
seed_url="https://b.example",
urls_file="https://b.example",
)


Expand All @@ -708,7 +709,7 @@ def test_url_invalid():
EcommerceSpider.from_crawler(crawler, url="foo")


def test_seed_url():
def test_urls_file():
crawler = get_crawler()
url = "https://example.com"

Expand All @@ -718,7 +719,7 @@ def test_seed_url():
b"https://a.example\n \nhttps://b.example\nhttps://c.example\n\n"
)
mock_get.return_value = response
spider = EcommerceSpider.from_crawler(crawler, seed_url=url)
spider = EcommerceSpider.from_crawler(crawler, urls_file=url)
mock_get.assert_called_with(url)

start_requests = list(spider.start_requests())
Expand Down
12 changes: 6 additions & 6 deletions zyte_spider_templates/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ class MaxRequestsParam(BaseModel):
)


class SeedUrlParam(BaseModel):
seed_url: str = Field(
title="Seed URL",
class UrlsFileParam(BaseModel):
urls_file: str = Field(
title="URLs file",
description=(
"URL that point to a list of URLs to crawl, e.g. "
"https://example.com/url-list.txt. The linked list must contain 1 "
"URL per line."
"URL that point to a plain-text file with a list of URLs to "
"crawl, e.g. https://example.com/url-list.txt. The linked list "
"must contain 1 URL per line."
),
pattern=_URL_PATTERN,
default="",
Expand Down
8 changes: 4 additions & 4 deletions zyte_spider_templates/spiders/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
ExtractFromParam,
GeolocationParam,
MaxRequestsParam,
SeedUrlParam,
UrlParam,
UrlsFileParam,
)

# Higher priority than command-line-defined settings (40).
ARG_SETTING_PRIORITY: int = 50

_INPUT_FIELDS = ("url", "seed_url")
_INPUT_FIELDS = ("url", "urls_file")


class BaseSpiderParams(
ExtractFromParam,
MaxRequestsParam,
GeolocationParam,
SeedUrlParam,
UrlsFileParam,
UrlParam,
BaseModel,
):
Expand All @@ -47,7 +47,7 @@ def single_input(self):
"""Fields
:class:`~zyte_spider_templates.spiders.ecommerce.EcommerceSpiderParams.url`
and
:class:`~zyte_spider_templates.spiders.ecommerce.EcommerceSpiderParams.seed_url`
:class:`~zyte_spider_templates.spiders.ecommerce.EcommerceSpiderParams.urls_file`
form a mandatory, mutually-exclusive field group: one of them must be
defined, the rest must not be defined."""
input_fields = set(
Expand Down
8 changes: 4 additions & 4 deletions zyte_spider_templates/spiders/ecommerce.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ def from_crawler(cls, crawler: Crawler, *args, **kwargs) -> scrapy.Spider:
return spider

def _init_input(self):
seed_url = self.args.seed_url
if seed_url:
response = requests.get(seed_url)
urls_file = self.args.urls_file
if urls_file:
response = requests.get(urls_file)
urls = load_url_list(response.text)
self.logger.info(f"Loaded {len(urls)} initial URLs from {seed_url}.")
self.logger.info(f"Loaded {len(urls)} initial URLs from {urls_file}.")
self.start_urls = urls
else:
self.start_urls = [self.args.url]
Expand Down
Loading