mothy-08 commited on
Commit ·
6b62397
1
Parent(s): ae687a1
Fix
Browse files- api/crawler.py +55 -8
- chrome-extension/sidepanel.css +4 -0
api/crawler.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
import time
|
|
|
|
|
|
|
| 2 |
import trafilatura
|
| 3 |
from trafilatura.sitemaps import sitemap_search
|
| 4 |
from api.utils import logger, is_valid_url
|
|
@@ -33,11 +35,23 @@ def smart_chunk(text: str, chunk_size=1000, overlap=100) -> list[str]:
|
|
| 33 |
def crawl_website(base_url: str, limit: int = 25):
|
| 34 |
logger.info(f"Starting crawl for {base_url}")
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
if not urls:
|
|
|
|
| 38 |
urls = [base_url]
|
| 39 |
|
| 40 |
valid_urls = [u for u in urls if is_valid_url(u, base_url)]
|
|
|
|
| 41 |
|
| 42 |
count = 0
|
| 43 |
for link in valid_urls:
|
|
@@ -45,17 +59,50 @@ def crawl_website(base_url: str, limit: int = 25):
|
|
| 45 |
break
|
| 46 |
|
| 47 |
try:
|
| 48 |
-
time.sleep(
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
if not result or not result.get("text"): # type: ignore
|
| 54 |
continue
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
|
|
|
| 59 |
text_chunks = smart_chunk(raw_text)
|
| 60 |
|
| 61 |
contextualized_chunks = [
|
|
|
|
| 1 |
import time
|
| 2 |
+
import re
|
| 3 |
+
import requests
|
| 4 |
import trafilatura
|
| 5 |
from trafilatura.sitemaps import sitemap_search
|
| 6 |
from api.utils import logger, is_valid_url
|
|
|
|
| 35 |
def crawl_website(base_url: str, limit: int = 25):
|
| 36 |
logger.info(f"Starting crawl for {base_url}")
|
| 37 |
|
| 38 |
+
headers = {
|
| 39 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
| 40 |
+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
|
| 41 |
+
"Accept-Language": "en-US,en;q=0.9",
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
urls = sitemap_search(base_url)
|
| 46 |
+
except Exception:
|
| 47 |
+
urls = []
|
| 48 |
+
|
| 49 |
if not urls:
|
| 50 |
+
logger.warning("No sitemap found (or blocked). Fallback to base URL.")
|
| 51 |
urls = [base_url]
|
| 52 |
|
| 53 |
valid_urls = [u for u in urls if is_valid_url(u, base_url)]
|
| 54 |
+
logger.info(f"Found {len(urls)} URLs, {len(valid_urls)} valid.")
|
| 55 |
|
| 56 |
count = 0
|
| 57 |
for link in valid_urls:
|
|
|
|
| 59 |
break
|
| 60 |
|
| 61 |
try:
|
| 62 |
+
time.sleep(1.0)
|
| 63 |
+
response = requests.get(link, headers=headers, timeout=10)
|
| 64 |
|
| 65 |
+
if response.status_code != 200:
|
| 66 |
+
logger.warning(f"Blocked or missing ({response.status_code}): {link}")
|
|
|
|
| 67 |
continue
|
| 68 |
|
| 69 |
+
# --- ROBUST EXTRACTION STRATEGY ---
|
| 70 |
+
page_title = "Unknown Page"
|
| 71 |
+
raw_text = ""
|
| 72 |
+
|
| 73 |
+
# Attempt 1: Bare Extraction (Best Quality)
|
| 74 |
+
try:
|
| 75 |
+
result = trafilatura.bare_extraction(
|
| 76 |
+
response.text, include_comments=False
|
| 77 |
+
)
|
| 78 |
+
if result and isinstance(result, dict) and result.get("text"):
|
| 79 |
+
page_title = result.get("title", "Unknown Page")
|
| 80 |
+
raw_text = result["text"]
|
| 81 |
+
except Exception as e:
|
| 82 |
+
# If trafilatura crashes (AttributeError, etc.), fail silently and try fallback
|
| 83 |
+
# logger.warning(f"Metadata extraction failed for {link}, using fallback.")
|
| 84 |
+
pass
|
| 85 |
+
|
| 86 |
+
# Attempt 2: Fallback Extraction (If Attempt 1 failed)
|
| 87 |
+
if not raw_text:
|
| 88 |
+
raw_text = trafilatura.extract(response.text, include_comments=False)
|
| 89 |
+
|
| 90 |
+
# Manual Title Extraction via Regex (since bare_extraction failed)
|
| 91 |
+
if raw_text:
|
| 92 |
+
title_match = re.search(
|
| 93 |
+
r"<title>(.*?)</title>",
|
| 94 |
+
response.text,
|
| 95 |
+
re.IGNORECASE | re.DOTALL,
|
| 96 |
+
)
|
| 97 |
+
if title_match:
|
| 98 |
+
clean_title = title_match.group(1).strip()
|
| 99 |
+
if clean_title:
|
| 100 |
+
page_title = clean_title
|
| 101 |
+
|
| 102 |
+
if not raw_text:
|
| 103 |
+
continue
|
| 104 |
|
| 105 |
+
# Chunking and Context Injection
|
| 106 |
text_chunks = smart_chunk(raw_text)
|
| 107 |
|
| 108 |
contextualized_chunks = [
|
chrome-extension/sidepanel.css
CHANGED
|
@@ -348,6 +348,10 @@ input:focus {
|
|
| 348 |
}
|
| 349 |
}
|
| 350 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
.loader {
|
| 352 |
border: 2px solid #f3f3f3;
|
| 353 |
border-top: 2px solid var(--accent-color);
|
|
|
|
| 348 |
}
|
| 349 |
}
|
| 350 |
|
| 351 |
+
#train-status {
|
| 352 |
+
margin-top: 8px;
|
| 353 |
+
}
|
| 354 |
+
|
| 355 |
.loader {
|
| 356 |
border: 2px solid #f3f3f3;
|
| 357 |
border-top: 2px solid var(--accent-color);
|