An open source, non-profit web search engine
Fetch curated domains from API in volunteer crawlers
Daoud Clarke50dba343b822
diff --git a/mwmbl/crawl.py b/mwmbl/crawl.pyindex b14e871..e7f3f67 100644--- a/mwmbl/crawl.py+++ b/mwmbl/crawl.py@@ -33,13 +33,36 @@ print("Mwmbl crawling statistics: https://mwmbl.org/stats")django.setup()from mwmbl.indexer.update_urls import record_urls_in_database-from mwmbl.crawler.retrieve import crawl_batch, crawl_url, CRAWLER_VERSION+from mwmbl.crawler.retrieve import crawl_batch, crawl_url, CRAWLER_VERSION, USER_AGENTfrom mwmbl.crawler.batch import HashedBatch, Result, Resultsfrom mwmbl.indexer.index_batches import index_batches, index_pagesBATCH_QUEUE_KEY = "batch-queue"REMOTE_SERVER = "https://api.mwmbl.org"+_curated_domains_cache: set[str] = set()+_curated_domains_fetched_at: float = 0.0+CURATED_DOMAINS_CACHE_SECONDS = 300.0+++def _fetch_curated_domains() -> set[str]:+ global _curated_domains_cache, _curated_domains_fetched_at+ now = time.monotonic()+ if now - _curated_domains_fetched_at < CURATED_DOMAINS_CACHE_SECONDS:+ return _curated_domains_cache+ try:+ response = requests.get(+ f"{REMOTE_SERVER}/api/v1/crawler/curated-domains",+ timeout=10,+ headers={"User-Agent": USER_AGENT},+ )+ response.raise_for_status()+ _curated_domains_cache = set(response.json())+ _curated_domains_fetched_at = now+ except Exception:+ logger.exception("Failed to fetch curated domains, using cached value")+ return _curated_domains_cache+# Validate environment variables when actually neededdef validate_environment():@@ -92,7 +115,7 @@ class Crawler:def url_queue(self):"""Lazy initialization of URL queue."""if self._url_queue is None:- self._url_queue = RedisURLQueue(self.redis, lambda: set())+ self._url_queue = RedisURLQueue(self.redis, _fetch_curated_domains)return self._url_queuedef check_redis(self):diff --git a/mwmbl/crawler/app.py b/mwmbl/crawler/app.pyindex 8cf3329..c21f38f 100644--- a/mwmbl/crawler/app.py+++ b/mwmbl/crawler/app.py@@ -239,6 +239,19 @@ def _register_routes(r: Router | NinjaAPI, batch_cache: BatchCache, queued_batch'status': 'ok'}+ @r.get(+ '/curated-domains',+ summary="Get curated domains for crawling",+ description=(+ "Retrieve the list of approved domains curated by mwmbl users. "+ "Crawlers should prioritize these domains when selecting URLs to crawl. "+ "Results are cached server-side for 5 minutes."+ ),+ )+ def get_curated_domains_endpoint(request) -> list[str]:+ from mwmbl.search_setup import get_curated_domains+ return sorted(get_curated_domains())+@r.post('/results',response={200: PostResultsResponse, 400: Error, 401: Error},