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

Crawl source information per item/page #31

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 2 additions & 3 deletions scrapy_webarchive/downloadermiddlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ def process_request(self, request: Request, spider: Spider):
if not warc_record:
self.stats.inc_value("webarchive/response_not_found", spider=spider)
return Response(url=request.url, status=404)

request.meta["warc_record_response"] = warc_record


# Record found, try to re-create a response from it.
response = record_transformer.response_for_record(warc_record, request)
response.meta["response_origin"] = warc_record
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that actually all meta keys need to start with webarchive_, to avoid naming conflicts.
Origin sounds like HTTP Origin, maybe it's better to use something with warc here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


if not response:
self.stats.inc_value("webarchive/response_not_recognized", spider=spider)
Expand Down
38 changes: 23 additions & 15 deletions scrapy_webarchive/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,19 @@ def __init__(self, settings: Settings, crawler: Crawler) -> None:
self.spider_name = crawler.spidercls.name if hasattr(crawler.spidercls, "name") else crawler.spider.name

# Get the store URI and configure the WACZ filename
store_uri, self.wacz_fname = self._retrieve_store_uri_and_wacz_fname()
self.store_uri, self.wacz_fname = self._retrieve_store_uri_and_wacz_fname()

# Initialize store and writer
self.store: FilesStoreProtocol = self._get_store(store_uri)
# Initialize store, writer and creator
self.store: FilesStoreProtocol = self._get_store(self.store_uri)
self.writer = WarcFileWriter(collection_name=self.spider_name)
self.wacz_creator = WaczFileCreator(
store=self.store,
warc_fname=self.writer.warc_fname,
wacz_fname=self.wacz_fname,
collection_name=crawler.spider.name,
title=self.settings["SW_WACZ_TITLE"],
description=self.settings["SW_WACZ_DESCRIPTION"],
)

def _check_configuration_prerequisites(self) -> None:
"""raises NotConfigured if essential settings or middleware configurations are incorrect."""
Expand Down Expand Up @@ -143,27 +151,27 @@ def response_downloaded(self, response: Response, request: Request, spider: Spid
request.meta["WARC-Date"] = get_formatted_dt_string(format=WARC_DT_FORMAT)

# Write response WARC record
record = self.writer.write_response(response, request)
response_record = self.writer.write_response(response, request)
self.stats.inc_value("webarchive/exporter/response_written", spider=spider)
self.stats.inc_value(
f"webarchive/exporter/writer_status_count/{record.http_headers.get_statuscode()}",
f"webarchive/exporter/writer_status_count/{response_record.http_headers.get_statuscode()}",
spider=spider,
)

# Write request WARC record
self.writer.write_request(request, concurrent_to=record)
request_record = self.writer.write_request(request, concurrent_to=response_record)
self.stats.inc_value("webarchive/exporter/request_written", spider=spider)

request.meta['exported_warc_response'] = response_record
request.meta['exported_warc_request'] = request_record
request.meta['exported_wacz_uri'] = self.export_uri

def spider_closed(self, spider: Spider) -> None:
wacz_creator = WaczFileCreator(
store=self.store,
warc_fname=self.writer.warc_fname,
wacz_fname=self.wacz_fname,
collection_name=spider.name,
title=self.settings["SW_WACZ_TITLE"],
description=self.settings["SW_WACZ_DESCRIPTION"],
)
wacz_creator.create()
self.wacz_creator.create()

@property
def export_uri(self) -> str:
return os.path.join(self.store_uri, self.wacz_creator.wacz_fname)


def get_archive_uri_template_dt_variables() -> dict:
Expand Down
Loading