An open source, non-profit web search engine
Use domain link scores to prioritise URLs to crawl
Daoud Clarkecf063e12b14f
diff --git a/devdata/index-v2.tinysearch b/devdata/index-v2.tinysearchindex 7f03d99..5654547 100644Binary files a/devdata/index-v2.tinysearch and b/devdata/index-v2.tinysearch differdiff --git a/mwmbl/crawler/domains.py b/mwmbl/crawler/domains.pyindex 3cbdfe3..dd01f85 100644--- a/mwmbl/crawler/domains.py+++ b/mwmbl/crawler/domains.py@@ -3,12 +3,16 @@ Record which source domains link to destination domains. Each source domain is adestination domains."""from itertools import islice+from logging import getLoggerfrom django.conf import settingsfrom pybloomfilter import BloomFilterfrom mwmbl.hn_top_domains_filtered import DOMAINS++logger = getLogger(__name__)+DOMAIN_GROUPS = ['github.com','en.wikipedia.org',@@ -45,18 +49,19 @@ class DomainLinkDatabase:bloom_filter.close()def update_domain_links(self, source: str, target: set[str]):- if source in TOP_DOMAINS:- url_group = 'top'+ if source in DOMAIN_GROUPS:+ domain_group = source+ elif source in TOP_DOMAINS:+ domain_group = 'top'elif source in OTHER_DOMAINS:- url_group = 'other'- elif source in DOMAIN_GROUPS:- url_group = source+ domain_group = 'other'else:# This is a URL that we don't care aboutreturn- bloom_filter = self.links[url_group]+ logger.info(f"Updating domain links for {domain_group} with {target} links")+ bloom_filter = self.links[domain_group]bloom_filter.update(target)def get_domain_score(self, domain: str) -> float:- return sum(1 if domain in bloom_filter else 0 for bloom_filter in self.links.values())/len(self.links)+ return sum(1 if domain in bloom_filter else 0 for bloom_filter in self.links.values())diff --git a/mwmbl/indexer/update_urls.py b/mwmbl/indexer/update_urls.pyindex 94d76cb..cecdd77 100644--- a/mwmbl/indexer/update_urls.py+++ b/mwmbl/indexer/update_urls.py@@ -102,10 +102,10 @@ def process_link(user_id_hash, crawled_page_domain, link, timestamp, url_timestaurl_users[link] = user_id_hashurl_timestamps[link] = timestamp- domain = f'{parsed_link.scheme}://{parsed_link.netloc}/'- url_users[domain] = user_id_hash- url_timestamps[domain] = timestamp- domain_links[crawled_page_domain].add(domain)+ root_url = f'{parsed_link.scheme}://{parsed_link.netloc}/'+ url_users[root_url] = user_id_hash+ url_timestamps[root_url] = timestamp+ domain_links[crawled_page_domain].add(parsed_link.netloc)def get_datetime_from_timestamp(timestamp: float) -> datetime:diff --git a/mwmbl/url_queue.py b/mwmbl/url_queue.pyindex d520b87..63f7787 100644--- a/mwmbl/url_queue.py+++ b/mwmbl/url_queue.py@@ -7,6 +7,7 @@ from queue import Emptyfrom random import Randomfrom typing import KeysView, Union+from mwmbl.crawler.domains import DomainLinkDatabasefrom mwmbl.crawler.urls import BATCH_SIZE, URLDatabase, URLStatus, FoundURL, REASSIGN_MIN_HOURSfrom mwmbl.database import Databasefrom mwmbl.hn_top_domains_filtered import DOMAINS as TOP_DOMAINS, DOMAINS@@ -57,35 +58,39 @@ class URLQueue:return num_processeddef _process_found_urls(self, found_urls: list[FoundURL], blacklist_domains: set[str]):- min_updated_date = datetime.utcnow() - timedelta(hours=REASSIGN_MIN_HOURS)-logger.info(f"Found URLS: {len(found_urls)}")- valid_urls = [found_url for found_url in found_urls if found_url.status == URLStatus.NEW.value or (- found_url.status == URLStatus.ASSIGNED.value and found_url.timestamp < min_updated_date)]+ logger.info(f"Found: {found_urls[:100]}")+ valid_urls = [found_url for found_url in found_urls if found_url.status == URLStatus.NEW]logger.info(f"Valid URLs: {len(valid_urls)}")self._sort_urls(valid_urls, blacklist_domains)logger.info(f"Queue size: {self.num_queued_batches}")while self.num_queued_batches < MAX_QUEUE_SIZE and len(self._top_urls) >= self._min_top_domains:total_top_urls = sum(len(urls) for urls in self._top_urls.values())- logger.info(f"Total top URLs stored: {total_top_urls}")+ logger.info(f"Total top URLs stored: {total_top_urls} for domains {self._top_urls.keys()}")total_other_urls = sum(len(urls) for urls in self._other_urls.values())- logger.info(f"Total other URLs stored: {total_other_urls}")+ logger.info(f"Total other URLs stored: {total_other_urls} for domains {self._other_urls.keys()}")self._batch_urls()logger.info(f"Queue size after batching: {self.num_queued_batches}")def _sort_urls(self, valid_urls: list[FoundURL], blacklist_domains: set[str]):- for found_url in valid_urls:- try:- domain = get_domain(found_url.url)- except ValueError:- continue- if is_domain_blacklisted(domain, blacklist_domains):- continue- url_store = self._top_urls if domain in TOP_DOMAINS else self._other_urls- url_store[domain][found_url.url] = 1/len(found_url.url)+ with DomainLinkDatabase() as link_db:+ for found_url in valid_urls:+ try:+ domain = get_domain(found_url.url)+ except ValueError:+ continue+ if is_domain_blacklisted(domain, blacklist_domains):+ continue+ if domain in TOP_DOMAINS:+ self._top_urls[domain][found_url.url] = 1/len(found_url.url)+ else:+ domain_score = link_db.get_domain_score(domain)+ if domain_score > 0:+ logger.info(f"Domain score for {domain}: {domain_score}")+ self._other_urls[domain][found_url.url] = 1/len(found_url.url)logger.info(f"URL store updated: {len(self._top_urls)} top domains, {len(self._other_urls)} other domains")