-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from zytedata/articles_to_main
Job postings + ProductList extraction
- Loading branch information
Showing
27 changed files
with
1,848 additions
and
145 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ on: | |
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
test: | ||
|
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
.. _job-posting: | ||
|
||
============================================= | ||
Job posting spider template (``job_posting``) | ||
============================================= | ||
|
||
Basic use | ||
========= | ||
|
||
.. code-block:: shell | ||
scrapy crawl job_posting -a url="https://books.toscrape.com" | ||
Parameters | ||
========== | ||
|
||
.. autopydantic_model:: zyte_spider_templates.spiders.job_posting.JobPostingSpiderParams | ||
:inherited-members: BaseModel | ||
:exclude-members: model_computed_fields |
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,19 +1,22 @@ | ||
from typing import Any, Dict, Optional | ||
from typing import Any, Dict, Optional, Type | ||
|
||
import pytest | ||
from scrapy import Spider | ||
from scrapy.utils.test import TestSpider | ||
|
||
# https://docs.pytest.org/en/stable/how-to/writing_plugins.html#assertion-rewriting | ||
pytest.register_assert_rewrite("tests.utils") | ||
|
||
|
||
# scrapy.utils.test.get_crawler alternative that does not freeze settings. | ||
def get_crawler(*, settings: Optional[Dict[str, Any]] = None): | ||
def get_crawler( | ||
*, settings: Optional[Dict[str, Any]] = None, spider_cls: Type[Spider] = TestSpider | ||
): | ||
from scrapy.crawler import CrawlerRunner | ||
|
||
settings = settings or {} | ||
# Set by default settings that prevent deprecation warnings. | ||
settings["REQUEST_FINGERPRINTER_IMPLEMENTATION"] = "2.7" | ||
runner = CrawlerRunner(settings) | ||
crawler = runner.create_crawler(TestSpider) | ||
crawler = runner.create_crawler(spider_cls) | ||
return crawler |
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,9 @@ | ||
import pytest | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def mockserver(): | ||
from .mockserver import MockServer | ||
|
||
with MockServer() as server: | ||
yield server |
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 |
---|---|---|
@@ -0,0 +1,178 @@ | ||
import argparse | ||
import json | ||
import socket | ||
import sys | ||
import time | ||
from importlib import import_module | ||
from subprocess import PIPE, Popen | ||
from typing import Any, Dict | ||
|
||
from scrapy_zyte_api.responses import _API_RESPONSE | ||
from twisted.internet import reactor | ||
from twisted.web.resource import Resource | ||
from twisted.web.server import Site | ||
|
||
|
||
def get_ephemeral_port(): | ||
s = socket.socket() | ||
s.bind(("", 0)) | ||
return s.getsockname()[1] | ||
|
||
|
||
class DefaultResource(Resource): | ||
"""Mock server to fake Zyte API responses. | ||
To use, include the mockserver fixture in the signature of your test, and | ||
point the ZYTE_API_URL setting to the mock server. See | ||
``tests/test_ecommerce.py::test_crawl_strategies`` for an example. | ||
This mock server is designed to fake a website with the following pages: | ||
``` | ||
https://example.com/ | ||
https://example.com/page/2 | ||
https://example.com/category/1 | ||
https://example.com/category/1/page/2 | ||
https://example.com/non-navigation | ||
``` | ||
When browserHtml is requested (for any URL, listed above or not), it is | ||
a minimal HTML with an anchor tag pointing to | ||
https://example.com/non-navigation. | ||
When productNavigation is requested, nextPage and subCategories are filled | ||
accordingly. productNavigation.items always has 2 product URLs, which are | ||
the result of appending ``/product/<n>`` to the request URL. | ||
https://example.com/non-navigation is not reachable through | ||
productNavigation. | ||
When product or productList is requested, an item with the current URL is | ||
always returned. | ||
All output also includes unsupported links (mailto:…). | ||
""" | ||
|
||
def getChild(self, path, request): | ||
return self | ||
|
||
def render_POST(self, request): | ||
request_data = json.loads(request.content.read()) | ||
request.responseHeaders.setRawHeaders( | ||
b"Content-Type", | ||
[b"application/json"], | ||
) | ||
request.responseHeaders.setRawHeaders( | ||
b"request-id", | ||
[b"abcd1234"], | ||
) | ||
|
||
response_data: _API_RESPONSE = {} | ||
|
||
response_data["url"] = request_data["url"] | ||
|
||
non_navigation_url = "https://example.com/non-navigation" | ||
html = f"""<html><body><a href="{non_navigation_url}"></a><a href="mailto:[email protected]"></a></body></html>""" | ||
if request_data.get("browserHtml", False) is True: | ||
response_data["browserHtml"] = html | ||
|
||
if request_data.get("product", False) is True: | ||
response_data["product"] = { | ||
"url": request_data["url"], | ||
} | ||
|
||
if request_data.get("productList", False) is True: | ||
response_data["productList"] = { | ||
"url": request_data["url"], | ||
} | ||
|
||
if request_data.get("productNavigation", False) is True: | ||
kwargs: Dict[str, Any] = {} | ||
if ( | ||
"/page/" not in request_data["url"] | ||
and "/non-navigation" not in request_data["url"] | ||
): | ||
kwargs["nextPage"] = { | ||
"url": f"{request_data['url'].rstrip('/')}/page/2" | ||
} | ||
if "/category/" not in request_data["url"]: | ||
kwargs["subCategories"] = [ | ||
{"url": "mailto:[email protected]"}, | ||
{"url": f"{request_data['url'].rstrip('/')}/category/1"}, | ||
] | ||
else: | ||
kwargs["nextPage"] = {"url": "mailto:[email protected]"} | ||
response_data["productNavigation"] = { | ||
"url": request_data["url"], | ||
"items": [ | ||
{"url": "mailto:[email protected]"}, | ||
{"url": f"{request_data['url'].rstrip('/')}/product/1"}, | ||
{"url": f"{request_data['url'].rstrip('/')}/product/2"}, | ||
], | ||
**kwargs, | ||
} | ||
|
||
return json.dumps(response_data).encode() | ||
|
||
|
||
class MockServer: | ||
def __init__(self, resource=None, port=None): | ||
resource = resource or DefaultResource | ||
self.resource = "{}.{}".format(resource.__module__, resource.__name__) | ||
self.proc = None | ||
self.host = socket.gethostbyname(socket.gethostname()) | ||
self.port = port or get_ephemeral_port() | ||
self.root_url = "http://%s:%d" % (self.host, self.port) | ||
|
||
def __enter__(self): | ||
self.proc = Popen( | ||
[ | ||
sys.executable, | ||
"-u", | ||
"-m", | ||
"tests.mockserver", | ||
self.resource, | ||
"--port", | ||
str(self.port), | ||
], | ||
stdout=PIPE, | ||
) | ||
assert self.proc.stdout is not None | ||
self.proc.stdout.readline() | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_value, traceback): | ||
assert self.proc is not None | ||
self.proc.kill() | ||
self.proc.wait() | ||
time.sleep(0.2) | ||
|
||
def urljoin(self, path): | ||
return self.root_url + path | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("resource") | ||
parser.add_argument("--port", type=int) | ||
args = parser.parse_args() | ||
module_name, name = args.resource.rsplit(".", 1) | ||
sys.path.append(".") | ||
resource = getattr(import_module(module_name), name)() | ||
# Typing issue: https://github.com/twisted/twisted/issues/9909 | ||
http_port = reactor.listenTCP(args.port, Site(resource)) # type: ignore[attr-defined] | ||
|
||
def print_listening(): | ||
host = http_port.getHost() | ||
print( | ||
"Mock server {} running at http://{}:{}".format( | ||
resource, host.host, host.port | ||
) | ||
) | ||
|
||
# Typing issue: https://github.com/twisted/twisted/issues/9909 | ||
reactor.callWhenRunning(print_listening) # type: ignore[attr-defined] | ||
reactor.run() # type: ignore[attr-defined] | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.