-
Notifications
You must be signed in to change notification settings - Fork 4
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
Define standard params #34
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .spiders.base import BaseSpider, BaseSpiderParams | ||
from .params import make_params | ||
from .spiders.base import BaseSpider | ||
from .spiders.ecommerce import EcommerceSpider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
from enum import Enum | ||
from typing import Optional | ||
|
||
from pydantic import BaseModel, ConfigDict, Field, create_model | ||
from pydantic.fields import PydanticUndefined | ||
|
||
from zyte_spider_templates._geolocations import ( | ||
GEOLOCATION_OPTIONS_WITH_CODE, | ||
Geolocation, | ||
) | ||
from zyte_spider_templates.documentation import document_enum | ||
|
||
|
||
@document_enum | ||
class CrawlStrategy(str, Enum): | ||
full: str = "full" | ||
"""Follow most links within the domain of URL in an attempt to discover and | ||
extract as many products as possible.""" | ||
|
||
navigation: str = "navigation" | ||
"""Follow pagination, subcategories, and product detail pages.""" | ||
|
||
pagination_only: str = "pagination_only" | ||
"""Follow pagination and product detail pages. SubCategory links are | ||
ignored. Use this when some subCategory links are misidentified by | ||
ML-extraction.""" | ||
|
||
|
||
@document_enum | ||
class ExtractFrom(str, Enum): | ||
httpResponseBody: str = "httpResponseBody" | ||
"""Use HTTP responses. Cost-efficient and fast extraction method, which | ||
works well on many websites.""" | ||
|
||
browserHtml: str = "browserHtml" | ||
"""Use browser rendering. Often provides the best quality.""" | ||
|
||
|
||
class AllParams(BaseModel): | ||
url: str = Field( | ||
title="URL", | ||
description="Initial URL for the crawl. Enter the full URL including http(s), " | ||
"you can copy and paste it from your browser. Example: https://toscrape.com/", | ||
pattern=r"^https?://[^:/\s]+(:\d{1,5})?(/[^\s]*)*(#[^\s]*)?$", | ||
) | ||
geolocation: Optional[Geolocation] = Field( | ||
title="Geolocation", | ||
description="ISO 3166-1 alpha-2 2-character string specified in " | ||
"https://docs.zyte.com/zyte-api/usage/reference.html#operation/extract/request/geolocation.", | ||
default=None, | ||
json_schema_extra={ | ||
"enumMeta": { | ||
code: { | ||
"title": GEOLOCATION_OPTIONS_WITH_CODE[code], | ||
} | ||
for code in Geolocation | ||
} | ||
}, | ||
) | ||
max_requests: Optional[int] = Field( | ||
description=( | ||
"The maximum number of Zyte API requests allowed for the crawl.\n" | ||
"\n" | ||
"Requests with error responses that cannot be retried or exceed " | ||
"their retry limit also count here, but they incur in no costs " | ||
"and do not increase the request count in Scrapy Cloud." | ||
), | ||
default=100, | ||
json_schema_extra={ | ||
"widget": "request-limit", | ||
}, | ||
) | ||
crawl_strategy: CrawlStrategy = Field( | ||
title="Crawl strategy", | ||
description="Determines how the start URL and follow-up URLs are crawled.", | ||
default=CrawlStrategy.navigation, | ||
json_schema_extra={ | ||
"enumMeta": { | ||
CrawlStrategy.full: { | ||
"title": "Full", | ||
"description": "Follow most links within the domain of URL in an attempt to discover and extract as many products as possible.", | ||
}, | ||
CrawlStrategy.navigation: { | ||
"title": "Navigation", | ||
"description": "Follow pagination, subcategories, and product detail pages.", | ||
}, | ||
CrawlStrategy.pagination_only: { | ||
"title": "Pagination Only", | ||
"description": ( | ||
"Follow pagination and product detail pages. SubCategory links are ignored. " | ||
"Use this when some subCategory links are misidentified by ML-extraction." | ||
), | ||
}, | ||
}, | ||
}, | ||
) | ||
extract_from: Optional[ExtractFrom] = Field( | ||
title="Extraction source", | ||
description=( | ||
"Whether to perform extraction using a browser request " | ||
"(browserHtml) or an HTTP request (httpResponseBody)." | ||
), | ||
default=None, | ||
json_schema_extra={ | ||
"enumMeta": { | ||
ExtractFrom.browserHtml: { | ||
"title": "browserHtml", | ||
"description": "Use browser rendering. Often provides the best quality.", | ||
}, | ||
ExtractFrom.httpResponseBody: { | ||
"title": "httpResponseBody", | ||
"description": "Use HTTP responses. Cost-efficient and fast extraction method, which works well on many websites.", | ||
}, | ||
}, | ||
}, | ||
) | ||
|
||
|
||
def make_params( | ||
cls_name, | ||
params, | ||
*, | ||
default=None, | ||
required=None, | ||
set_args=None, | ||
): | ||
fields = {} | ||
default = default or {} | ||
required = set(required) if required else set() | ||
for param in params: | ||
field = AllParams.model_fields[param] | ||
if field in required: | ||
field.default = PydanticUndefined | ||
else: | ||
try: | ||
field.default = default[param] | ||
except KeyError: | ||
pass | ||
fields[param] = (field.annotation, field) | ||
model = create_model( | ||
cls_name, | ||
__config__=ConfigDict(extra="forbid"), | ||
**fields, | ||
) | ||
if set_args: | ||
model.set_args = set_args | ||
return model | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before further progress, I would like to decide if this is the way we want to approach this, i.e. a single model with all param definitions and a function to create a model with a subset of that.