-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrawler.py
More file actions
467 lines (407 loc) · 15.3 KB
/
crawler.py
File metadata and controls
467 lines (407 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
"""
Sitemap Crawler
Production-ready sitemap crawler with intelligent page classification.
Fetches and labels all URLs from a company's sitemap with automatic
page type detection.
"""
import defusedxml.ElementTree as ET
from typing import Optional, Dict, List
from datetime import datetime
from collections import OrderedDict
import httpx
from httpx import Timeout, Limits
import logging
from urllib.parse import urlparse
import re
import asyncio
import time
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
from .models import SitemapPage, SitemapPageList, PageLabel
logger = logging.getLogger(__name__)
class SitemapCrawler:
"""
Crawls company sitemap and auto-labels all URLs.
Features:
- Fetches sitemap from standard locations (/sitemap.xml, /sitemap_index.xml)
- Handles recursive sitemap_index.xml with concurrent processing
- Intelligent page type classification (10 page types)
- LRU caching with configurable TTL
- Rate limiting and comprehensive error handling
- Security-first URL validation
"""
# Default URL patterns for page type classification
DEFAULT_PATTERNS = {
"blog": [
r"\/blog\/?",
r"\/news\/?",
r"\/articles\/?",
r"\/posts\/?",
r"\/insights\/?",
r"\/stories\/?",
r"\/updates\/?",
r"\/press\/?",
],
"product": [
r"\/products?\/?",
r"\/solutions?\/?",
r"\/pricing\/?",
r"\/features\/?",
r"\/plans\/?",
r"\/offerings?\/?",
r"\/store\/?",
r"\/shop\/?",
r"\/catalog\/?",
r"\/deals?\/?",
r"\/inventory\/?",
],
"service": [
r"\/services?\/?",
r"\/consulting\/?",
r"\/agency\/?",
r"\/professional-services\/?",
],
"docs": [
r"\/docs?\/?",
r"\/documentation\/?",
r"\/guides?\/?",
r"\/tutorials?\/?",
r"\/help\/?",
r"\/kb\/?",
r"\/knowledge-base\/?",
r"\/faq\/?",
],
"resource": [
r"\/whitepapers?\/?",
r"\/case-studies?\/?",
r"\/templates?\/?",
r"\/tools?\/?",
r"\/calculators?\/?",
r"\/webinars?\/?",
r"\/videos?\/?",
r"\/ebooks?\/?",
r"\/reports?\/?",
],
"company": [
r"\/about\/?",
r"\/about-us\/?",
r"\/team\/?",
r"\/careers?\/?",
r"\/jobs?\/?",
r"\/culture\/?",
r"\/company\/?",
r"\/who-we-are\/?",
r"\/mission\/?",
r"\/vision\/?",
r"\/values?\/?",
r"\/leadership\/?",
r"\/newsroom\/?",
],
"legal": [
r"\/imprint\/?",
r"\/impressum\/?",
r"\/privacy\/?",
r"\/privacy-policy\/?",
r"\/terms?\/?",
r"\/terms-of-service\/?",
r"\/terms-of-use\/?",
r"\/legal\/?",
r"\/disclaimer\/?",
r"\/cookies?\/?",
r"\/data-protection\/?",
r"\/gdpr\/?",
],
"contact": [
r"\/contact\/?",
r"\/contact-us\/?",
r"\/get-in-touch\/?",
r"\/reach-us\/?",
r"\/talk-to-us\/?",
r"\/support\/?",
r"\/customer-support\/?",
r"\/help-desk\/?",
r"\/email-us\/?",
],
"landing": [
r"\/campaigns?\/?",
r"\/lp\/?",
r"\/landing\/?",
r"\/offers?\/?",
r"\/promotions?\/?",
r"\/deals?\/?",
r"\/promos?\/?",
],
}
# Dangerous URL protocols that should be rejected for security
DANGEROUS_PROTOCOLS = [
'javascript:', 'file:', 'data:', 'vbscript:',
'about:', 'chrome:', 'chrome-extension:'
]
def __init__(
self,
custom_patterns: Optional[Dict[str, List[str]]] = None,
timeout: Optional[Timeout] = None,
cache_ttl: int = 3600,
max_urls: int = 10000,
max_cache_size: int = 100,
):
"""
Initialize SitemapCrawler.
Args:
custom_patterns: Custom URL patterns for classification
timeout: HTTP timeout configuration
cache_ttl: Cache time-to-live in seconds
max_urls: Maximum URLs to process
max_cache_size: Maximum cache entries (LRU eviction)
Raises:
ValueError: If max_urls or max_cache_size is <= 0, or cache_ttl < 0
"""
# Validate parameters
if max_urls <= 0:
raise ValueError(f"max_urls must be > 0, got {max_urls}")
if max_cache_size <= 0:
raise ValueError(f"max_cache_size must be > 0, got {max_cache_size}")
if cache_ttl < 0:
raise ValueError(f"cache_ttl must be >= 0, got {cache_ttl}")
self.patterns = custom_patterns or self.DEFAULT_PATTERNS
self.timeout = timeout or Timeout(
connect=5.0,
read=10.0,
write=5.0,
pool=5.0,
)
self.cache_ttl = cache_ttl
self.max_urls = max_urls
self.max_cache_size = max_cache_size
# LRU cache with thread safety
self._cache: OrderedDict[str, tuple[SitemapPageList, float]] = OrderedDict()
self._cache_hits = 0
self._cache_misses = 0
self._cache_lock = asyncio.Lock()
async def crawl(self, company_url: str) -> SitemapPageList:
"""
Crawl company's sitemap and return labeled pages.
Args:
company_url: Company website URL
Returns:
SitemapPageList with classified pages
Raises:
ValueError: If company_url is invalid
"""
start_time = time.time()
# Normalize and validate URL
company_url = company_url.rstrip('/')
if not self._is_valid_url(company_url):
raise ValueError(f"Invalid company_url: {company_url}")
logger.info(f"Starting sitemap crawl for {company_url}")
# Check cache (thread-safe)
cache_key = f"{company_url}:{self.max_urls}"
async with self._cache_lock:
if cache_key in self._cache:
result, timestamp = self._cache[cache_key]
if time.time() - timestamp < self.cache_ttl:
self._cache_hits += 1
self._cache.move_to_end(cache_key) # LRU
duration = time.time() - start_time
logger.info(f"Cache hit: {result.count()} URLs in {duration:.2f}s")
return result
else:
del self._cache[cache_key] # Expired
self._cache_misses += 1
try:
# Fetch all URLs from sitemap(s)
urls = await self._fetch_all_urls(company_url)
if not urls:
return self._empty_sitemap(company_url)
# Apply URL limit
if len(urls) > self.max_urls:
logger.warning(f"Limiting {len(urls)} URLs to {self.max_urls}")
urls = urls[:self.max_urls]
# Classify pages
pages = []
for url in urls:
if self._is_valid_url(url):
page = self._classify_page(url)
pages.append(page)
# Create result
result = SitemapPageList(
pages=pages,
company_url=company_url,
total_urls=len(urls),
fetch_timestamp=datetime.now().isoformat()
)
# Cache result (thread-safe)
async with self._cache_lock:
self._cache[cache_key] = (result, time.time())
self._cache.move_to_end(cache_key)
# LRU eviction
while len(self._cache) > self.max_cache_size:
self._cache.popitem(last=False)
duration = time.time() - start_time
logger.info(f"Crawl complete: {len(pages)} URLs in {duration:.2f}s")
return result
except Exception as e:
logger.error(f"Crawl failed for {company_url}: {e}")
return self._empty_sitemap(company_url)
def get_cache_stats(self) -> Dict[str, any]:
"""Get cache performance statistics."""
total_requests = self._cache_hits + self._cache_misses
hit_rate = (self._cache_hits / total_requests * 100) if total_requests > 0 else 0
return {
"cache_hits": self._cache_hits,
"cache_misses": self._cache_misses,
"hit_rate": f"{hit_rate:.1f}%",
"cache_size": len(self._cache),
"max_cache_size": self.max_cache_size
}
def clear_cache(self):
"""Clear the cache and reset statistics."""
self._cache.clear()
self._cache_hits = 0
self._cache_misses = 0
def _empty_sitemap(self, company_url: str) -> SitemapPageList:
"""Create empty sitemap for error cases."""
return SitemapPageList(
pages=[],
company_url=company_url,
total_urls=0,
fetch_timestamp=datetime.now().isoformat()
)
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=2, min=2, max=8),
retry=retry_if_exception_type((httpx.TimeoutException, httpx.HTTPStatusError)),
reraise=True,
)
async def _fetch_all_urls(self, company_url: str) -> List[str]:
"""Fetch all URLs from sitemap(s) with retry logic."""
all_urls = []
sitemap_locations = [
f"{company_url}/sitemap.xml",
f"{company_url}/sitemap_index.xml",
f"{company_url}/sitemap/sitemap.xml",
]
# Try www variant
parsed = urlparse(company_url)
if parsed.netloc and not parsed.netloc.startswith("www."):
base = f"{parsed.scheme}://www.{parsed.netloc}"
sitemap_locations.extend([
f"{base}/sitemap.xml",
f"{base}/sitemap_index.xml",
f"{base}/sitemap/sitemap.xml",
])
async with httpx.AsyncClient(
timeout=self.timeout,
follow_redirects=True,
limits=Limits(max_connections=5, max_keepalive_connections=2)
) as client:
for sitemap_url in sitemap_locations:
try:
await asyncio.sleep(0.5) # Rate limiting
response = await client.get(sitemap_url)
response.raise_for_status()
root = ET.fromstring(response.content)
ns = {"sm": "http://www.sitemaps.org/schemas/sitemap/0.9"}
# Check for sitemap index
sitemaps = root.findall(".//sm:sitemap/sm:loc", ns)
if sitemaps:
# Concurrent sub-sitemap fetching
sub_urls = [elem.text for elem in sitemaps if elem.text]
tasks = [
self._fetch_sub_sitemap(client, url)
for url in sub_urls
]
results = await asyncio.gather(*tasks, return_exceptions=True)
for result in results:
if isinstance(result, list):
all_urls.extend(result)
if all_urls:
break
else:
# Regular sitemap
urls = self._extract_urls(response.content)
all_urls.extend(urls)
break
except httpx.HTTPStatusError as e:
if e.response.status_code in (404, 403, 401):
continue # Try next location
if e.response.status_code in (503, 429, 500, 502, 504):
raise # Retry
continue
except (httpx.TimeoutException, ET.ParseError):
continue
return list(set(all_urls)) # Deduplicate
async def _fetch_sub_sitemap(self, client: httpx.AsyncClient, url: str) -> List[str]:
"""Fetch URLs from a sub-sitemap."""
try:
await asyncio.sleep(0.2) # Rate limiting
response = await client.get(url)
response.raise_for_status()
return self._extract_urls(response.content)
except Exception as e:
logger.debug(f"Sub-sitemap fetch failed {url}: {e}")
return []
@staticmethod
def _extract_urls(content: bytes) -> List[str]:
"""Extract URLs from sitemap XML."""
urls = []
try:
root = ET.fromstring(content)
ns = {"sm": "http://www.sitemaps.org/schemas/sitemap/0.9"}
for url_elem in root.findall(".//sm:url/sm:loc", ns):
if url_elem.text:
urls.append(url_elem.text)
except ET.ParseError:
pass
return urls
def _is_valid_url(self, url: str) -> bool:
"""Validate URL for security and format."""
if not url or not isinstance(url, str):
return False
try:
# Security: Check for dangerous protocols
url_lower = url.lower().strip()
if any(url_lower.startswith(proto) for proto in self.DANGEROUS_PROTOCOLS):
return False
parsed = urlparse(url)
return (
parsed.scheme in ['http', 'https'] and
parsed.netloc and
'.' in parsed.netloc
)
except Exception:
return False
def _classify_page(self, url: str) -> SitemapPage:
"""Classify page based on URL patterns."""
path = urlparse(url).path.lower()
# Score each label
scores: Dict[PageLabel, float] = {
label: 0.0 for label in self.patterns.keys() # type: ignore
}
scores["other"] = 0.1 # Default score
for label, patterns in self.patterns.items():
for pattern in patterns:
if re.search(pattern, path):
scores[label] += 0.4
# Best match
best_label: PageLabel = max(scores, key=scores.get) # type: ignore
confidence = min(scores[best_label], 1.0)
title = self._extract_title_from_url(url)
return SitemapPage(
url=url,
label=best_label,
title=title,
path=path,
confidence=confidence,
)
@staticmethod
def _extract_title_from_url(url: str) -> str:
"""Extract human-readable title from URL."""
path = urlparse(url).path
parts = path.rstrip("/").split("/")
# Get last non-empty part
for part in reversed(parts):
if part:
# Convert slug to title
title = part.replace("-", " ").replace("_", " ")
return " ".join(word.capitalize() for word in title.split()) or "Untitled"
return "Untitled"