feat(spiders system): a prototype of the new spiders system
- A modern spider design that uses AnyIO and asyncio, yet it's very similar to Scrapy spiders API because it's the easiest design for users, and to make it easier for new users. - Spiders can have multiple sessions per crawl, and users decide which session to use with each request. - A scheduler system that uses heapq logic. - The user can set the number of concurrent requests for a spider globally or per domain. - The user can set a download delay to control the speed of the spider more. - There's a global function that can be overridden to handle errors for all requests. (Similar to errback in scrapy). - There's a spider argument to set the allowed domains for the spider to stay in. - Each spider has a very detailed crawl stats that can be accessed right away from the code after the crawl finishes. Same case with scraped items. - The whole spider as written as any other script and you just run it. No command-line arguments, and no need to run it from the terminal through the library like other known alternatives. - Each spider has its own logger that forces sessions to use it. - Each spider has functions to override that run before start and after close. - There's a spider argument to set the logging level and another one to make the spider write to a log file. - This is only the start. A lot more features are coming in the way.
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from scrapling.engines.toolbelt.custom import Response
|
||||
from scrapling.core._types import Any, AsyncGenerator, Callable, Dict, Union
|
||||
|
||||
|
||||
class Request:
|
||||
def __init__(
|
||||
self,
|
||||
url: str,
|
||||
sid: str = "",
|
||||
callback: Callable[[Response], AsyncGenerator[Union[Dict[str, Any], "Request", None], None]] | None = None,
|
||||
priority: int = 0,
|
||||
dont_filter: bool = False,
|
||||
meta: dict[str, Any] | None = None,
|
||||
**kwargs: Any,
|
||||
) -> None:
|
||||
self.url: str = url
|
||||
self.sid: str = sid
|
||||
self.callback = callback
|
||||
self.priority: int = priority
|
||||
self.dont_filter: bool = dont_filter
|
||||
self.meta: dict[str, Any] = meta if meta else {}
|
||||
self._session_kwargs = kwargs if kwargs else {}
|
||||
|
||||
@property
|
||||
def domain(self) -> str:
|
||||
return urlparse(self.url).netloc
|
||||
|
||||
@property
|
||||
def _fp(self) -> str:
|
||||
"""Generate a unique fingerprint for deduplication."""
|
||||
# TODO: Improve fingerprint
|
||||
return f"{self.sid}:{self.url}"
|
||||
|
||||
def __repr__(self) -> str:
|
||||
callback_name = getattr(self.callback, "__name__", None) or "None"
|
||||
return f"<Request({self.url}) priority={self.priority} callback={callback_name}>"
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.url
|
||||
|
||||
def __lt__(self, other: object) -> bool:
|
||||
"""Compare requests by priority"""
|
||||
if not isinstance(other, Request):
|
||||
return NotImplemented
|
||||
return self.priority < other.priority
|
||||
|
||||
def __gt__(self, other: object) -> bool:
|
||||
"""Compare requests by priority"""
|
||||
if not isinstance(other, Request):
|
||||
return NotImplemented
|
||||
return self.priority > other.priority
|
||||
|
||||
def __eq__(self, other: object) -> bool:
|
||||
"""Requests are equal if they have the same fingerprint."""
|
||||
if not isinstance(other, Request):
|
||||
return NotImplemented
|
||||
return self._fp == other._fp
|
||||
Reference in New Issue
Block a user