-
Notifications
You must be signed in to change notification settings - Fork 128
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 #248 from allenai/warc_resource_support
Support for WARC resource record types.
- Loading branch information
Showing
3 changed files
with
156 additions
and
23 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
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,51 @@ | ||
import datetime | ||
from typing import TYPE_CHECKING, Optional | ||
|
||
from fastwarc.warc import WarcRecordType | ||
from necessary import necessary | ||
|
||
with necessary("dateparser", soft=True) as DATEPARSER_AVAILABLE: | ||
if DATEPARSER_AVAILABLE or TYPE_CHECKING: | ||
import dateparser | ||
|
||
|
||
DATE_FORMATS = ["%a, %d %b %Y %H:%M:%S %Z", "%Y-%m-%dT%H:%M:%SZ"] | ||
|
||
|
||
class WarcRecordInfo: | ||
def __init__(self, record): | ||
self.record = record | ||
|
||
if not self.is_valid: | ||
return None | ||
|
||
self.payload_id = record.headers.get("WARC-Payload-Digest").split(":")[1].lower() | ||
self.target_uri = record.headers.get("WARC-Target-URI") | ||
|
||
if record.record_type == WarcRecordType.response: | ||
ctype, *_ = (record.http_headers.get("Content-Type") or "").split(";") | ||
self.ctype = ctype | ||
self.date = WarcRecordInfo._parse_warc_timestamp(record.http_headers.get("Date")) | ||
elif record.record_type == WarcRecordType.resource: | ||
self.ctype, *_ = (record.headers.get("Content-Type") or "").split(";") | ||
self.date = WarcRecordInfo._parse_warc_timestamp(record.headers.get("WARC-Date")) | ||
else: | ||
raise ValueError(f"Unsupported record type: {record.record_type}") | ||
|
||
@property | ||
def is_valid(self) -> bool: | ||
if not self.record.headers.get("WARC-Payload-Digest"): | ||
return False | ||
|
||
if not self.record.headers.get("WARC-Target-URI"): | ||
return False | ||
|
||
return True | ||
|
||
@staticmethod | ||
def _parse_warc_timestamp(timestamp_str: Optional[str]) -> datetime.datetime: | ||
"""Parse a WARC timestamp into a datetime object.""" | ||
if not timestamp_str: | ||
return datetime.datetime.now() | ||
|
||
return dateparser.parse(date_string=timestamp_str, date_formats=DATE_FORMATS) or datetime.datetime.now() |
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,91 @@ | ||
import datetime | ||
from unittest import TestCase | ||
from unittest.mock import MagicMock, patch | ||
|
||
from fastwarc.warc import WarcRecord, WarcRecordType | ||
|
||
from dolma.warc.record_info import WarcRecordInfo | ||
|
||
|
||
class TestWarcRecordInfo(TestCase): | ||
def test_response_record(self): | ||
record_mock = MagicMock(spec=WarcRecord) | ||
record_mock.record_type = WarcRecordType.response | ||
record_mock.headers = {"WARC-Payload-Digest": "sha1:payload_id", "WARC-Target-URI": "http://example.com"} | ||
record_mock.http_headers = { | ||
"Content-Type": "text/html; charset=utf-8", | ||
"Date": "Thu, 20 Apr 2023 12:00:00 GMT", | ||
} | ||
|
||
record_info = WarcRecordInfo(record_mock) | ||
|
||
self.assertEqual(record_info.payload_id, "payload_id") | ||
self.assertEqual(record_info.target_uri, "http://example.com") | ||
self.assertEqual(record_info.ctype, "text/html") | ||
self.assertEqual(record_info.date, datetime.datetime(2023, 4, 20, 12, 0, 0)) | ||
|
||
def test_resource_record(self): | ||
record_mock = MagicMock(spec=WarcRecord) | ||
record_mock.record_type = WarcRecordType.resource | ||
record_mock.headers = { | ||
"WARC-Payload-Digest": "sha1:payload_id", | ||
"WARC-Target-URI": "http://example.com", | ||
"Content-Type": "application/json", | ||
"WARC-Date": "2023-04-20T12:00:00Z", | ||
} | ||
record_mock.http_headers = {} | ||
|
||
record_info = WarcRecordInfo(record_mock) | ||
|
||
self.assertEqual(record_info.payload_id, "payload_id") | ||
self.assertEqual(record_info.target_uri, "http://example.com") | ||
self.assertEqual(record_info.ctype, "application/json") | ||
self.assertEqual(record_info.date, datetime.datetime(2023, 4, 20, 12, 0, 0)) | ||
|
||
def test_unsupported_record_type(self): | ||
record_mock = MagicMock(spec=WarcRecord) | ||
record_mock.record_type = "unsupported" | ||
record_mock.headers = {"WARC-Payload-Digest": "sha1:payload_id", "WARC-Target-URI": "http://example.com"} | ||
record_mock.http_headers = {} | ||
|
||
with self.assertRaises(ValueError): | ||
WarcRecordInfo(record_mock) | ||
|
||
def test_missing_headers(self): | ||
record_mock = MagicMock(spec=WarcRecord) | ||
record_mock.record_type = WarcRecordType.response | ||
record_mock.headers = {} | ||
record_mock.http_headers = {} | ||
|
||
with patch("dolma.warc.record_info.datetime") as datetime_mock: | ||
now = datetime.datetime.now() | ||
datetime_mock.datetime.now.return_value = now | ||
record_info = WarcRecordInfo(record_mock) | ||
assert not record_info.is_valid | ||
|
||
def test_content_type_with_extra_info(self): | ||
record_mock = MagicMock(spec=WarcRecord) | ||
record_mock.record_type = WarcRecordType.response | ||
record_mock.headers = {"WARC-Payload-Digest": "sha1:payload_id", "WARC-Target-URI": "http://example.com"} | ||
record_mock.http_headers = { | ||
"Content-Type": "text/html; charset=utf-8; boundary=---123", | ||
"Date": "Thu, 20 Apr 2023 12:00:00 GMT", | ||
} | ||
|
||
record_info = WarcRecordInfo(record_mock) | ||
|
||
self.assertEqual(record_info.ctype, "text/html") | ||
|
||
def test_invalid_date_format(self): | ||
record_mock = MagicMock(spec=WarcRecord) | ||
record_mock.record_type = WarcRecordType.response | ||
record_mock.headers = {"WARC-Payload-Digest": "sha1:payload_id", "WARC-Target-URI": "http://example.com"} | ||
record_mock.http_headers = {"Content-Type": "text/html; charset=utf-8", "Date": "Invalid Date"} | ||
|
||
with patch("dolma.warc.record_info.datetime") as datetime_mock: | ||
now = datetime.datetime.now() | ||
datetime_mock.datetime.now.return_value = now | ||
record_info = WarcRecordInfo(record_mock) | ||
|
||
# Assert that the date is "close enough" to now, since it's hard to mock perfectly | ||
self.assertAlmostEqual(record_info.date.timestamp(), now.timestamp(), delta=1) |