#!/usr/bin/env python3
"""Scrape Ogham few meanings from dedicated sites.
Sources:
- oghamlore.com (upright / reversed)
- druidogham.wordpress.com (well / ill dignified)
- globalspiritualstudies.com (chart)
Ogham has no Lenormand-style pair tables; combinations in a spread
are synthesized later from two fews' meanings.
"""
from __future__ import annotations
import html as htmlmod
import json
import re
import ssl
import time
import urllib.error
import urllib.request
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
OUT = ROOT / "oracle_enricher" / "out" / "ogham_scrape" / "html"
CORPUS = ROOT / "oracle_enricher" / "data" / "sources" / "ogham_corpus.json"
CTX = ssl.create_default_context()
UA = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/122.0.0.0 Safari/537.36"
SLEEP = 0.15
# canon_id, name_en, tree_ru, druidogham path
FEDA = [
("beith", "Beith", "берёза", "the-fews-air/beith"),
("luis", "Luis", "рябина", "the-fews-air/luis"),
("fern", "Fearn", "ольха", "the-fews-air/fearn"),
("sail", "Sail", "ива", "the-fews-air/saille"),
("nion", "Nion", "ясень", "the-fews-air/nuin"),
("uath", "Huath", "боярышник", "the-fews-fire/huath"),
("duir", "Duir", "дуб", "the-fews-fire/duir"),
("tinne", "Tinne", "остролист", "the-fews-fire/tinne"),
("coll", "Coll", "орешник", "the-fews-fire/coll"),
("ceirt", "Quert", "яблоня", "the-fews-fire/quiert"),
("muin", "Muin", "лоза", "the-fews-water/muin"),
("gort", "Gort", "плющ", "the-fews-water/gort"),
("ngetal", "nGetal", "тростник", "the-fews-water/ngetal"),
("straif", "Straif", "тёрн", "the-fews-water/straif"),
("ruis", "Ruis", "бузина", "the-fews-water/ruis"),
("ailm", "Ailm", "сосна", "the-fews-earth/ailm"),
("onn", "Onn", "дрок", "the-fews-earth/onn"),
("ur", "Ur", "вереск", "the-fews-earth/ur"),
("eadha", "Eadha", "осина", "the-fews-earth/eadha"),
("idho", "Idho", "тис", "the-fews-earth/ioho"),
]
def slugify(url: str) -> str:
s = re.sub(r"^https?://", "", url)
return re.sub(r"[^a-zA-Z0-9._-]+", "_", s)[:180]
def fetch(url: str) -> tuple[int, str]:
OUT.mkdir(parents=True, exist_ok=True)
cache = OUT / (slugify(url) + ".html")
if cache.exists() and cache.stat().st_size > 400:
return 200, cache.read_text(encoding="utf-8", errors="replace")
req = urllib.request.Request(url, headers={"User-Agent": UA, "Accept-Language": "en"})
try:
with urllib.request.urlopen(req, context=CTX, timeout=40) as resp:
raw = resp.read()
enc = resp.headers.get_content_charset() or "utf-8"
text = raw.decode(enc, errors="replace")
status = resp.status
except urllib.error.HTTPError as e:
text = e.read().decode("utf-8", errors="replace") if e.fp else ""
cache.write_text(text, encoding="utf-8")
return e.code, text
except Exception as e:
return 0, str(e)
cache.write_text(text, encoding="utf-8")
time.sleep(SLEEP)
return status, text
def strip_tags(html: str) -> str:
html = re.sub(r"(?is)", " ", html)
html = re.sub(r"(?is)", " ", html)
html = re.sub(r"(?i)
", "\n", html)
html = re.sub(r"(?i)