Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Pass through last_crawled_date in crawler

Daoud Clarke929e902a7a69
diff --git a/mwmbl/crawl.py b/mwmbl/crawl.py
index 54ba2c4..b14e871 100644
--- a/mwmbl/crawl.py
+++ b/mwmbl/crawl.py
@@ -194,7 +194,8 @@ class Crawler:
if new_high_score:
result_items = [Result(url=doc.url, title=doc.title, extract=doc.extract,
- score=doc.score, term=doc.term, state=doc.state) for doc in new_items]
+ score=doc.score, term=doc.term, state=doc.state,
+ last_crawled=doc.last_crawled) for doc in new_items]
results = Results(api_key=MWMBL_API_KEY, results=result_items, crawler_version=CRAWLER_VERSION)
logger.info(f"Posting {len(result_items)} results")
response = requests.post(
diff --git a/mwmbl/crawler/app.py b/mwmbl/crawler/app.py
index 662e4c1..8cf3329 100644
--- a/mwmbl/crawler/app.py
+++ b/mwmbl/crawler/app.py
@@ -241,7 +241,7 @@ def _register_routes(r: Router | NinjaAPI, batch_cache: BatchCache, queued_batch
@r.post(
'/results',
- response={200: PostResultsResponse, 401: Error},
+ response={200: PostResultsResponse, 400: Error, 401: Error},
summary="Submit indexed results",
description=(
"Submit a set of pre-indexed search results directly into the Mwmbl index. "
@@ -266,14 +266,19 @@ def _register_routes(r: Router | NinjaAPI, batch_cache: BatchCache, queued_batch
return 401, {"message": "Invalid API key or insufficient scope (crawl scope required)."}
now = datetime.now(timezone.utc)
- documents = [
- Document(
+ now_ts = int(now.timestamp())
+
+ documents = []
+ for result in results.results:
+ if result.last_crawled is not None and result.last_crawled > now_ts:
+ return 400, {"message": f"last_crawled timestamp is in the future for URL: {result.url}"}
+ last_crawled = result.last_crawled if result.last_crawled is not None else now_ts
+ documents.append(Document(
url=result.url, title=result.title, extract=result.extract,
user_ids=[api_key.user.id],
- last_crawled=int(now.timestamp()),
- )
- for result in results.results
- ]
+ last_crawled=last_crawled,
+ ))
+
index_path = f"{settings.DATA_PATH}/{settings.INDEX_NAME}"
index_documents(documents, index_path)
filename = upload_object(results, now, api_key.user.username, "results")
diff --git a/mwmbl/crawler/batch.py b/mwmbl/crawler/batch.py
index 99b7406..7c40251 100644
--- a/mwmbl/crawler/batch.py
+++ b/mwmbl/crawler/batch.py
@@ -184,6 +184,11 @@ class Result(Schema):
description="Internal state flag (optional).",
example=1,
)
+ last_crawled: Optional[int] = Field(
+ default=None,
+ description="Unix timestamp (seconds since epoch) when the page was crawled.",
+ example=1704672000,
+ )
class Results(Schema):