87 lines
3.3 KiB
Python
Executable File
87 lines
3.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Сканер дистрибьюторов: обходит сайт, находит карточки вендора и PDF-файлы.
|
|
|
|
Зачем: у вендоров даташиты часто лежат только у дистрибьюторов. Ручной
|
|
обход каждый раз — долго, поэтому правила задаются конфигом.
|
|
|
|
Использование:
|
|
python3 dist-scan.py <host> [<start_path> ...]
|
|
|
|
Пишет найденные PDF в /tmp/dist-scan-<host>.txt и печатает сводку.
|
|
Ничего не добавляет в очередь автоматически — очередь собирает
|
|
отдельный шаг, чтобы владелец видел список.
|
|
"""
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
import urllib.parse
|
|
|
|
UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124 Safari/537.36"
|
|
MAX_PAGES = 60
|
|
|
|
|
|
def get(url, timeout=25):
|
|
try:
|
|
r = subprocess.run(["curl", "-sL", "-m", str(timeout), "-A", UA, url],
|
|
capture_output=True, text=True, timeout=timeout + 10)
|
|
return r.stdout or ""
|
|
except Exception:
|
|
return ""
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
print("нужен хост: python3 dist-scan.py avicon.ru")
|
|
return 2
|
|
host = sys.argv[1]
|
|
base = "https://" + host
|
|
starts = sys.argv[2:] or ["/"]
|
|
seen = set()
|
|
products = set()
|
|
pdfs = set()
|
|
|
|
queue = [base + p for p in starts]
|
|
while queue and len(seen) < MAX_PAGES:
|
|
url = queue.pop(0).split("#")[0]
|
|
if url in seen:
|
|
continue
|
|
seen.add(url)
|
|
html = get(url)
|
|
if not html:
|
|
continue
|
|
low = html.lower()
|
|
# карточки вендора
|
|
for m in re.findall(r'href="([^"]+)"', html):
|
|
# некоторые CMS отдают ссылки в процентах: avicon.ru%2Fcatalog%2F...
|
|
m = urllib.parse.unquote(m)
|
|
full = m if m.startswith("http") else urllib.parse.urljoin(base, m)
|
|
if not full.startswith(base):
|
|
continue
|
|
if re.search(r"(hitrolink|hti[-_])", full, re.I) and "/brands/" not in full:
|
|
products.add(full.split("?")[0])
|
|
if re.search(r"(catalog|brands|search)", full, re.I) and len(queue) < MAX_PAGES:
|
|
if full.split("?")[0] not in seen and full.split("?")[0] not in queue:
|
|
queue.append(full.split("?")[0])
|
|
# PDF прямо на странице
|
|
for m in re.findall(r'href="([^"]+\.pdf[^"]*)"', html, re.I):
|
|
if m.startswith("file://") or "C:" in m:
|
|
continue
|
|
full = m if m.startswith("http") else urllib.parse.urljoin(base, m)
|
|
pdfs.add(full)
|
|
|
|
print("хост: %s" % host)
|
|
print(" страниц просмотрено: %d" % len(seen))
|
|
print(" карточек вендора: %d" % len(products))
|
|
print(" PDF найдено: %d" % len(pdfs))
|
|
for u in sorted(pdfs)[:20]:
|
|
print(" ", urllib.parse.unquote(u)[:120])
|
|
|
|
out = "/tmp/dist-scan-%s.txt" % host
|
|
open(out, "w", encoding="utf-8").write("\n".join(sorted(pdfs)))
|
|
print(" список: %s" % out)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|