Repository navigation
Expand file tree
/
Copy pathsite_mapper.py
More file actions
487 lines (429 loc) · 15.7 KB
/
Copy pathsite_mapper.py
File metadata and controls
487 lines (429 loc) · 15.7 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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
from __future__ import annotations
import gzip
import html as html_mod
import logging
import re
import xml.parsers.expat
import urllib.parse
from typing import Any
import httpx
from security import safe_fetch
from config import get_http_client
log = logging.getLogger("site_mapper")
SITEMAP_NS = "http://www.sitemaps.org/schemas/sitemap/0.9"
ATOM_NS = "http://www.w3.org/2005/Atom"
MAX_SITEMAP_BYTES = 50 * 1024 * 1024
MAX_RECURSION = 5
COMMON_SITEMAP_PATHS = [
"/sitemap.xml", "/sitemap.xml.gz",
"/sitemap_index.xml", "/sitemap-index.xml",
"/sitemap_index.xml.gz", "/sitemap-index.xml.gz",
"/.sitemap.xml",
"/sitemap", "/sitemap/",
"/sitemap/sitemap-index.xml", "/sitemap/sitemap.xml",
"/sitemaps/sitemap.xml", "/sitemaps/sitemap.xml.gz",
"/sitemap_news.xml", "/sitemap-news.xml",
"/sitemap_news.xml.gz", "/sitemap-news.xml.gz",
"/admin/config/search/xmlsitemap",
]
CONTENT_TYPES_UNLIKELY_XML = {
"text/html", "application/json", "text/plain",
"image/", "video/", "audio/", "font/",
}
async def map_site(
url: str,
max_depth: int = 0,
max_urls: int = 1000,
timeout: int = 30,
include_sitemap: bool = True,
include_links: bool = True,
same_domain: bool = True,
exclude_patterns: list[str] | None = None,
rank: bool = False,
) -> dict[str, Any]:
parsed = urllib.parse.urlparse(url)
base = f"{parsed.scheme}://{parsed.netloc}"
exclude = None
if exclude_patterns:
try:
exclude = re.compile("|".join(exclude_patterns))
except re.error:
pass
seen: set[str] = set()
results: list[dict[str, Any]] = []
source = "none"
c = get_http_client()
if include_sitemap:
sitemap_urls: list[str] = []
try:
r = await safe_fetch(c, f"{base}/robots.txt", timeout=10)
if r.status_code == 200:
for line in r.text.splitlines():
line = line.strip()
if line.lower().startswith("sitemap:"):
sitemap_urls.append(line.split(":", 1)[1].strip())
except Exception:
pass
for path in COMMON_SITEMAP_PATHS:
try:
r = await safe_fetch(c, f"{base}{path}", timeout=8)
ctype = (r.headers.get("content-type", "") or "").lower()
if r.status_code != 200:
continue
if any(u in ctype for u in CONTENT_TYPES_UNLIKELY_XML):
continue
sitemap_urls.append(str(r.url))
if len(sitemap_urls) >= 5:
break
except Exception:
pass
for su in sitemap_urls:
await _parse_sitemap_recursive(
su, c, seen, results, exclude,
base if same_domain else "", max_urls, set(), 0, max_depth if max_depth > 0 else MAX_RECURSION,
)
if results:
source = "sitemap"
if include_links and len(results) < max_urls:
html_text = await _fetch_page_html(url, c)
if html_text:
links: set[str] = set()
for m in re.finditer(r'(?:href|src)=["\'](.*?)["\']', html_text, re.IGNORECASE):
href = html_mod.unescape(m.group(1)).strip()
if not href or href.startswith("#") or href.startswith("javascript:") or href.startswith("data:"):
continue
absolute = urllib.parse.urljoin(url, href)
parsed_link = urllib.parse.urlparse(absolute)
if not parsed_link.scheme.startswith("http"):
continue
# Keep the query string: ?p=/?page= URLs are distinct
# pages; drop only the fragment (same-document anchor).
normalised = urllib.parse.urlunparse((
parsed_link.scheme, parsed_link.netloc,
parsed_link.path.rstrip("/") or "/", "",
parsed_link.query, "",
))
if exclude and exclude.search(normalised):
continue
if normalised not in seen:
seen.add(normalised)
links.add(normalised)
for link in sorted(links):
if link.startswith(base):
results.append({"url": link, "source": "link"})
if len(results) >= max_urls:
break
if results and source == "none":
source = "links"
# Ranked mode: score each URL by fetch quality + merge priors so the
# caller gets important-first order instead of sitemap-alphabetical.
# Canonical dedupe via merge.norm_key (sitemap vs link variants of the
# same page collapse). Off by default — costs one scored pass.
if rank and results:
results = await _rank_urls(results, max_urls)
return {
"success": True,
"url": url,
"base_domain": base,
"source": source,
"total": len(results),
"urls": results[:max_urls],
}
async def _rank_urls(
entries: list[dict[str, Any]], max_urls: int,
) -> list[dict[str, Any]]:
"""Dedupe by canonical key, score by fetch quality, important-first."""
from merge import norm_key
from fetch import fetch_url
import asyncio
# Canonical dedupe: first occurrence wins (sitemap beats link-follow).
uniq: dict[str, dict[str, Any]] = {}
for e in entries:
k = norm_key(e["url"])
if k not in uniq:
uniq[e["url"]] = e
deduped = list(uniq.values())
async def _score(e: dict[str, Any]) -> tuple[float, dict[str, Any]]:
try:
f = await fetch_url(e["url"], max_chars=2000, cache_ttl=86400)
q = float(f.get("quality", 0.0)) if f.get("success") else 0.0
except Exception:
q = 0.0
# Sitemap signals: priority/changefreq/lastmod are author-ranked
# importance — use them directly instead of a domain guess.
prior = float(e.get("priority", 0.0) or 0.0) / 1.0
freq_boost = {"always": 0.3, "hourly": 0.25, "daily": 0.2,
"weekly": 0.1, "monthly": 0.05}.get(
str(e.get("changefreq", "")).lower(), 0.0)
return (q * 0.7 + prior * 0.2 + freq_boost, e)
scored = await asyncio.gather(*[_score(e) for e in deduped[:max_urls]])
scored.sort(key=lambda t: t[0], reverse=True)
return [{**e, "rank_score": round(s, 3)} for s, e in scored]
async def _parse_sitemap_recursive(
sitemap_url: str,
c: httpx.AsyncClient,
seen: set[str],
results: list[dict[str, Any]],
exclude: re.Pattern | None,
domain_filter: str,
max_urls: int,
parent_urls: set[str],
depth: int,
max_depth: int = MAX_RECURSION,
) -> None:
if depth > max_depth:
return
if sitemap_url in parent_urls:
return
content = await _fetch_sitemap_content(sitemap_url, c)
if content is None:
return
new_parents = parent_urls | {sitemap_url}
if content.startswith("<"):
# XML — could be sitemap index, urlset, RSS, or Atom
try:
import xml.etree.ElementTree as ET
root = ET.fromstring(content[:10000])
root_tag = _strip_ns(root.tag)
except Exception:
root_tag = ""
parsed = False
if root_tag in ("sitemapindex", "urlset"):
parsed = await _parse_xml_sitemap(
content, sitemap_url, seen, results,
exclude, domain_filter, max_urls, c, new_parents, depth + 1, max_depth,
)
elif root_tag == "rss":
parsed = await _parse_rss_sitemap(
content, seen, results, exclude, domain_filter, max_urls,
)
elif root_tag == "feed":
parsed = await _parse_atom_sitemap(
content, seen, results, exclude, domain_filter, max_urls,
)
if not parsed:
# malformed XML — try Expat fallback
_parse_xml_fallback(content, seen, results, domain_filter, max_urls, exclude)
else:
# plain text sitemap (one URL per line)
_parse_text_sitemap(content, seen, results, domain_filter, max_urls, exclude)
async def _fetch_sitemap_content(url: str, c: httpx.AsyncClient) -> str | None:
try:
r = await safe_fetch(c, url, timeout=15)
if r.status_code != 200:
return None
raw = r.content
if not raw:
return None
if len(raw) > MAX_SITEMAP_BYTES:
log.warning("Sitemap at %s exceeds %d bytes, skipping", url, MAX_SITEMAP_BYTES)
return None
if url.endswith(".gz") or raw[:2] == b"\x1f\x8b":
try:
raw = gzip.decompress(raw)
except Exception:
pass
if _looks_like_nonsitemap(url, r.headers.get("content-type", "")):
return None
return raw.decode("utf-8", errors="replace")
except Exception:
return None
def _looks_like_nonsitemap(url: str, content_type: str) -> bool:
ct = content_type.lower().split(";")[0].strip()
if any(u in ct for u in CONTENT_TYPES_UNLIKELY_XML):
return True
if ct in ("text/xml", "application/xml", "application/rss+xml", "application/atom+xml", ""):
return False
# unknown content type — accept, let content parsing decide
return False
def _strip_ns(tag: str) -> str:
return tag.split("}", 1)[-1] if tag.startswith("{") else tag
async def _parse_xml_sitemap(
content: str,
source_url: str,
seen: set[str],
results: list[dict[str, Any]],
exclude: re.Pattern | None,
domain_filter: str,
max_urls: int,
c: httpx.AsyncClient,
parent_urls: set[str],
depth: int,
max_depth: int = MAX_RECURSION,
) -> bool:
try:
import xml.etree.ElementTree as ET
root = ET.fromstring(content)
except Exception:
return False
tag = _strip_ns(root.tag)
if tag == "sitemapindex":
sub_loc = f"{{{SITEMAP_NS}}}loc"
for sm in root.findall(f"{{{SITEMAP_NS}}}sitemap"):
loc = sm.find(sub_loc)
if loc is not None and loc.text:
url = _clean_url(loc.text.strip())
await _parse_sitemap_recursive(
url, c, seen, results, exclude,
domain_filter, max_urls, parent_urls, depth + 1, max_depth,
)
return True
if tag == "urlset":
for u in root.findall(f"{{{SITEMAP_NS}}}url"):
loc = u.find(f"{{{SITEMAP_NS}}}loc")
if loc is None or not loc.text:
continue
url = _clean_url(loc.text.strip())
if url in seen:
continue
seen.add(url)
if exclude and exclude.search(url):
continue
if domain_filter and not url.startswith(domain_filter):
continue
entry: dict[str, Any] = {"url": url, "source": "sitemap"}
lastmod = u.find(f"{{{SITEMAP_NS}}}lastmod")
if lastmod is not None and lastmod.text:
entry["last_modified"] = lastmod.text.strip()
priority = u.find(f"{{{SITEMAP_NS}}}priority")
if priority is not None and priority.text:
try:
entry["priority"] = float(priority.text.strip())
except ValueError:
pass
changefreq = u.find(f"{{{SITEMAP_NS}}}changefreq")
if changefreq is not None and changefreq.text:
entry["changefreq"] = changefreq.text.strip()
results.append(entry)
if len(results) >= max_urls:
break
return True
return False
def _parse_xml_fallback(
content: str,
seen: set[str],
results: list[dict[str, Any]],
domain_filter: str,
max_urls: int,
exclude: re.Pattern | None,
) -> None:
urls: list[str] = []
current_tag = ""
in_loc = False
def start(name: str, attrs: dict[str, str]) -> None:
nonlocal in_loc, current_tag
current_tag = _strip_ns(name)
in_loc = current_tag == "loc"
def end(name: str) -> None:
nonlocal in_loc
current_tag = _strip_ns(name)
if current_tag == "loc":
in_loc = False
def data(text: str) -> None:
if in_loc and text.strip():
url = _clean_url(text.strip())
if url:
urls.append(url)
try:
p = xml.parsers.expat.ParserCreate()
p.StartElementHandler = start
p.EndElementHandler = end
p.CharacterDataHandler = data
p.Parse(content, True)
except Exception:
return
for u in urls:
if u in seen: continue
seen.add(u)
if exclude and exclude.search(u): continue
if domain_filter and not u.startswith(domain_filter): continue
results.append({"url": u, "source": "sitemap"})
if len(results) >= max_urls: break
async def _parse_rss_sitemap(
content: str,
seen: set[str],
results: list[dict[str, Any]],
exclude: re.Pattern | None,
domain_filter: str,
max_urls: int,
) -> bool:
try:
import xml.etree.ElementTree as ET
root = ET.fromstring(content)
except Exception:
return False
for item in root.iter("item"):
link = item.find("link")
if link is not None and link.text:
url = _clean_url(link.text.strip())
if url in seen: continue
seen.add(url)
if exclude and exclude.search(url): continue
if domain_filter and not url.startswith(domain_filter): continue
results.append({"url": url, "source": "sitemap"})
if len(results) >= max_urls:
break
return True
async def _parse_atom_sitemap(
content: str,
seen: set[str],
results: list[dict[str, Any]],
exclude: re.Pattern | None,
domain_filter: str,
max_urls: int,
) -> bool:
try:
import xml.etree.ElementTree as ET
root = ET.fromstring(content)
except Exception:
return False
for entry in root.iter(f"{{{ATOM_NS}}}entry"):
for link in entry.findall(f"{{{ATOM_NS}}}link"):
href = link.get("href", "")
if href:
url = _clean_url(href)
if url in seen: continue
seen.add(url)
if exclude and exclude.search(url): continue
if domain_filter and not url.startswith(domain_filter): continue
results.append({"url": url, "source": "sitemap"})
if len(results) >= max_urls:
break
return True
def _parse_text_sitemap(
content: str,
seen: set[str],
results: list[dict[str, Any]],
domain_filter: str,
max_urls: int,
exclude: re.Pattern | None,
) -> None:
for line in content.splitlines():
line = line.strip()
if not line or line.startswith("#"):
continue
url = _clean_url(line)
if not url:
continue
if not urllib.parse.urlparse(url).scheme.startswith("http"):
continue
if url in seen: continue
seen.add(url)
if exclude and exclude.search(url): continue
if domain_filter and not url.startswith(domain_filter): continue
results.append({"url": url, "source": "sitemap"})
if len(results) >= max_urls:
break
def _clean_url(url: str) -> str:
url = html_mod.unescape(url)
url = re.sub(r"\s+", "", url)
return url
async def _fetch_page_html(url: str, c: httpx.AsyncClient) -> str | None:
try:
r = await safe_fetch(c, url, timeout=10)
ct = (r.headers.get("content-type", "") or "").lower()
if r.status_code == 200 and "text/html" in ct:
return r.text
except Exception:
pass
return None