Implement domain whitelisting for web search results

This commit is contained in:
Que Nguyen
2024-06-13 07:14:48 +07:00
committed by GitHub
parent a382e82dec
commit 7b5f434a07
10 changed files with 63 additions and 29 deletions
+11 -1
View File
@@ -1,8 +1,18 @@
from typing import Optional
from urllib.parse import urlparse
from pydantic import BaseModel
def filter_by_whitelist(results, whitelist):
if not whitelist:
return results
filtered_results = []
for result in results:
domain = urlparse(result["url"]).netloc
if any(domain.endswith(whitelisted_domain) for whitelisted_domain in whitelist):
filtered_results.append(result)
return filtered_results
class SearchResult(BaseModel):
link: str
title: Optional[str]