Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Record when a URL was crawled, not which month it was crawled in

Daoud Clarkeab0d0c2238e8
diff --git a/mwmbl/crawler/urls.py b/mwmbl/crawler/urls.py
index 888eb0e..7b5b02b 100644
--- a/mwmbl/crawler/urls.py
+++ b/mwmbl/crawler/urls.py
@@ -102,11 +102,14 @@ class URLDatabase:
if url.status in CRAWLED_STATUSES:
most_recent_urls.add(url.url)
- # The URL has just been crawled (or failed): mark it as crawled now so
- # it isn't immediately re-queued. Without this, a first-time crawl has
- # found_date=None (it wasn't in any bloom filter before this batch), so
- # queue_urls treats it as never-crawled and hands it out again.
- found_date = most_recent_date
+ # The URL has just been crawled (or failed): mark it as crawled at the
+ # time of the crawl so it isn't immediately re-queued. Without this, a
+ # first-time crawl has found_date=None (it wasn't in any bloom filter
+ # before this batch), so queue_urls treats it as never-crawled and hands
+ # it out again. The bloom filters only record the month, which is too
+ # coarse to use here: on the 31st, the start of the month is already 30
+ # days ago, which is exactly the re-queue threshold.
+ found_date = url.timestamp
new_urls.append(FoundURL(url.url, url.user_id_hash, url.status, url.timestamp, found_date))
return new_urls
diff --git a/test/test_url_database.py b/test/test_url_database.py
index 79da5af..41df870 100644
--- a/test/test_url_database.py
+++ b/test/test_url_database.py
@@ -32,40 +32,37 @@ def clean_bloom_filters():
_remove()
-def _current_month():
- now = datetime.utcnow()
- return datetime(now.year, now.month, 1)
-
-
def test_crawled_url_marked_as_recently_crawled(clean_bloom_filters):
- """A first-time crawled URL should come back with last_crawled set to now."""
+ """A first-time crawled URL should come back with last_crawled set to the crawl time."""
+ crawled_at = datetime.utcnow()
found = FoundURL(
url="https://example.com/page",
user_id_hash="user",
status=URLStatus.CRAWLED,
- timestamp=datetime.utcnow(),
+ timestamp=crawled_at,
last_crawled=None,
)
with URLDatabase() as url_db:
new_urls = url_db.update_found_urls([found])
assert len(new_urls) == 1
- assert new_urls[0].last_crawled == _current_month()
+ assert new_urls[0].last_crawled == crawled_at
def test_robots_denied_url_marked_as_recently_crawled(clean_bloom_filters):
"""A robots-denied URL must also be marked crawled so it isn't re-attempted."""
+ crawled_at = datetime.utcnow()
found = FoundURL(
url="https://example.com/blocked",
user_id_hash="user",
status=URLStatus.ERROR_ROBOTS_DENIED,
- timestamp=datetime.utcnow(),
+ timestamp=crawled_at,
last_crawled=None,
)
with URLDatabase() as url_db:
new_urls = url_db.update_found_urls([found])
- assert new_urls[0].last_crawled == _current_month()
+ assert new_urls[0].last_crawled == crawled_at
def test_crawled_url_is_not_requeued(clean_bloom_filters):
@@ -97,6 +94,24 @@ def test_crawled_url_is_not_requeued(clean_bloom_filters):
assert url_queue.get_domain_count("discovered.example") == 1
+def test_crawl_date_is_the_crawl_time_not_the_start_of_the_month(clean_bloom_filters):
+ """The bloom filters only record the month a URL was crawled in. Reporting the start
+ of that month as the crawl date makes a URL crawled on the 31st look 30 days old -
+ exactly the age at which queue_urls hands it out again."""
+ crawled_at = datetime(2026, 8, 31, 12, 0, 0)
+ found = FoundURL(
+ url="https://end-of-month.example/page",
+ user_id_hash="user",
+ status=URLStatus.CRAWLED,
+ timestamp=crawled_at,
+ last_crawled=None,
+ )
+ with URLDatabase() as url_db:
+ new_urls = url_db.update_found_urls([found])
+
+ assert new_urls[0].last_crawled == crawled_at
+
+
def test_url_queue_gives_its_curated_domains_to_the_default_blacklist():
"""The standalone crawler has no database and fetches approved domains over HTTP, so
the queue's own source of curated domains is what has to reach the blacklist."""