WIP
This commit is contained in:
parent
61d7f6b646
commit
186492de85
2 changed files with 137 additions and 72 deletions
89
boostack_create_pages.py
Normal file
89
boostack_create_pages.py
Normal file
|
|
@ -0,0 +1,89 @@
|
||||||
|
import requests
|
||||||
|
import sys
|
||||||
|
|
||||||
|
# ==========================
|
||||||
|
# CONFIGURATION
|
||||||
|
# ==========================
|
||||||
|
|
||||||
|
BOOKSTACK_API_URL = "https://wiki-warmachine.ungol.fr/api"
|
||||||
|
API_TOKEN_ID = "VOTRE_TOKEN_ID"
|
||||||
|
API_TOKEN_SECRET = "VOTRE_TOKEN_SECRET"
|
||||||
|
|
||||||
|
PAGES_FILE = "pages.txt"
|
||||||
|
|
||||||
|
DEFAULT_CONTENT = """
|
||||||
|
<p><em>Page restaurée automatiquement depuis l'ancien wiki.</em></p>
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ==========================
|
||||||
|
# HEADERS
|
||||||
|
# ==========================
|
||||||
|
|
||||||
|
HEADERS = {
|
||||||
|
"Authorization": f"Token {API_TOKEN_ID}:{API_TOKEN_SECRET}",
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
|
||||||
|
# ==========================
|
||||||
|
# FUNCTIONS
|
||||||
|
# ==========================
|
||||||
|
|
||||||
|
def create_page(title, chapter_id, content=DEFAULT_CONTENT):
|
||||||
|
"""Create a page in BookStack"""
|
||||||
|
url = f"{BOOKSTACK_API_URL}/pages"
|
||||||
|
|
||||||
|
payload = {
|
||||||
|
"name": title,
|
||||||
|
"html": content,
|
||||||
|
"chapter_id": int(chapter_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
response = requests.post(url, headers=HEADERS, json=payload)
|
||||||
|
|
||||||
|
if response.status_code == 200:
|
||||||
|
page_id = response.json().get("id")
|
||||||
|
print(f"[OK] Page créée : '{title}' (ID {page_id})")
|
||||||
|
return page_id
|
||||||
|
else:
|
||||||
|
print(f"[ERREUR] Impossible de créer '{title}'")
|
||||||
|
print(response.status_code, response.text)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def load_pages(filename):
|
||||||
|
"""Load pages list from file"""
|
||||||
|
pages = []
|
||||||
|
with open(filename, "r", encoding="utf-8") as f:
|
||||||
|
for line in f:
|
||||||
|
line = line.strip()
|
||||||
|
if not line or line.startswith("#"):
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
title, chapter_id = line.split("|")
|
||||||
|
pages.append((title.strip(), chapter_id.strip()))
|
||||||
|
except ValueError:
|
||||||
|
print(f"[IGNORÉ] Ligne invalide : {line}")
|
||||||
|
return pages
|
||||||
|
|
||||||
|
|
||||||
|
# ==========================
|
||||||
|
# MAIN
|
||||||
|
# ==========================
|
||||||
|
|
||||||
|
def main():
|
||||||
|
pages = load_pages(PAGES_FILE)
|
||||||
|
|
||||||
|
if not pages:
|
||||||
|
print("Aucune page à créer.")
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
print(f"{len(pages)} pages à créer...\n")
|
||||||
|
|
||||||
|
for title, chapter_id in pages:
|
||||||
|
create_page(title, chapter_id)
|
||||||
|
|
||||||
|
print("\nImport terminé.")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
@ -6,14 +6,24 @@ from urllib.parse import urlparse, parse_qs, unquote
|
||||||
import unicodedata
|
import unicodedata
|
||||||
|
|
||||||
# --------------------------------------------------
|
# --------------------------------------------------
|
||||||
# PATHS
|
# CONFIG
|
||||||
# --------------------------------------------------
|
# --------------------------------------------------
|
||||||
|
|
||||||
PAGES_DIR = Path("../output/cleaned_pages")
|
PAGES_DIR = Path("../output_ok/cleaned_pages")
|
||||||
REGISTRY_PATH = Path("../output/equivalence_registry.json")
|
REGISTRY_PATH = Path("../output_ok/equivalence_registry.json")
|
||||||
OUTPUT_DIR = Path("../output/link_scan")
|
OUTPUT_DIR = Path("../output_ok/link_scan")
|
||||||
|
|
||||||
OUTPUT_DIR.mkdir(exist_ok=True)
|
OUTPUT_DIR.mkdir(exist_ok=True)
|
||||||
|
IGNORED_PREFIXES = (
|
||||||
|
"file ",
|
||||||
|
"image ",
|
||||||
|
"category ",
|
||||||
|
"template ",
|
||||||
|
"special ",
|
||||||
|
"help ",
|
||||||
|
"user ",
|
||||||
|
"talk ",
|
||||||
|
)
|
||||||
|
|
||||||
# --------------------------------------------------
|
# --------------------------------------------------
|
||||||
# LOAD REGISTRY
|
# LOAD REGISTRY
|
||||||
|
|
@ -34,88 +44,68 @@ def normalize_title(title: str) -> str:
|
||||||
if not title:
|
if not title:
|
||||||
return
|
return
|
||||||
title = title.strip()
|
title = title.strip()
|
||||||
|
title = unquote(title)
|
||||||
|
title = Path(title).stem
|
||||||
title = unicodedata.normalize("NFKC", title)
|
title = unicodedata.normalize("NFKC", title)
|
||||||
title = title.replace("_", " ")
|
title = title.replace("_", " ")
|
||||||
title = title.replace("’", "'").replace("‘", "'").replace("“", '"').replace("”", '"')
|
title = title.replace("’", "'").replace("‘", "'").replace("“", '"').replace("”", '"')
|
||||||
title = re.sub(r"\s+", " ", title)
|
title = re.sub(r"\s+", " ", title)
|
||||||
return title.casefold()
|
return title.casefold()
|
||||||
|
|
||||||
# -------------------------
|
|
||||||
# Extract MediaWiki target
|
|
||||||
# -------------------------
|
|
||||||
|
|
||||||
def extract_mediawiki_target(href: str):
|
def extract_mediawiki_target(href: str):
|
||||||
|
|
||||||
if not href:
|
if not href:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# ignore anchors
|
|
||||||
if href.startswith("#"):
|
if href.startswith("#"):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
parsed = urlparse(href)
|
parsed = urlparse(href)
|
||||||
|
|
||||||
# external link
|
|
||||||
if parsed.scheme in ("http", "https"):
|
if parsed.scheme in ("http", "https"):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
path = parsed.path or ""
|
path = parsed.path or ""
|
||||||
|
|
||||||
# /wiki/Page_Name
|
|
||||||
if "/wiki/" in path:
|
if "/wiki/" in path:
|
||||||
return path.split("/wiki/", 1)[1]
|
return path.split("/wiki/", 1)[1]
|
||||||
|
|
||||||
# index.php?title=Page
|
|
||||||
if "index.php" in path:
|
if "index.php" in path:
|
||||||
qs = parse_qs(parsed.query)
|
qs = parse_qs(parsed.query)
|
||||||
if "title" in qs:
|
if "title" in qs:
|
||||||
return qs["title"][0]
|
return qs["title"][0]
|
||||||
|
|
||||||
# fallback filename-like
|
|
||||||
return Path(path).stem
|
return Path(path).stem
|
||||||
|
|
||||||
|
|
||||||
# -------------------------
|
|
||||||
# Ignore unwanted namespaces
|
|
||||||
# -------------------------
|
|
||||||
|
|
||||||
IGNORED_PREFIXES = (
|
|
||||||
"file:",
|
|
||||||
"image:",
|
|
||||||
"template:",
|
|
||||||
"special:",
|
|
||||||
"help:",
|
|
||||||
"user:",
|
|
||||||
"talk:",
|
|
||||||
)
|
|
||||||
|
|
||||||
def is_ignored_namespace(title_norm: str):
|
def is_ignored_namespace(title_norm: str):
|
||||||
return title_norm.startswith(IGNORED_PREFIXES)
|
return title_norm.startswith(IGNORED_PREFIXES)
|
||||||
|
|
||||||
|
|
||||||
# -------------------------
|
|
||||||
# Extract article content
|
|
||||||
# -------------------------
|
|
||||||
|
|
||||||
def extract_article_links(soup):
|
def extract_article_links(soup):
|
||||||
|
|
||||||
content = soup.find("div", id="mw-content-text")
|
content = soup.find("div", id="mw-content-text")
|
||||||
if not content:
|
if not content:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
links = []
|
links = []
|
||||||
|
|
||||||
for a in content.select("a[href]"):
|
for a in content.select("a[href]"):
|
||||||
|
|
||||||
# ignore navboxes / metadata
|
|
||||||
if a.find_parent(class_="navbox"):
|
if a.find_parent(class_="navbox"):
|
||||||
continue
|
continue
|
||||||
|
links.append({
|
||||||
href = a.get("href")
|
"href": a.get("href"),
|
||||||
links.append(href)
|
"title": a.get("title"),
|
||||||
|
"text": a.get_text(strip=True),
|
||||||
|
})
|
||||||
return links
|
return links
|
||||||
|
|
||||||
|
def resolve_link(raw_target, title_attr):
|
||||||
|
candidates = []
|
||||||
|
if title_attr:
|
||||||
|
candidates.append(title_attr)
|
||||||
|
if raw_target:
|
||||||
|
candidates.append(raw_target)
|
||||||
|
for candidate in candidates:
|
||||||
|
norm = normalize_title(candidate)
|
||||||
|
if not norm:
|
||||||
|
continue
|
||||||
|
if is_ignored_namespace(norm):
|
||||||
|
return None, "ignored"
|
||||||
|
if norm in equivalences:
|
||||||
|
return equivalences[norm], "equivalence"
|
||||||
|
filename = norm.replace(" ", "_") + ".html"
|
||||||
|
if filename in valid_targets:
|
||||||
|
return filename, "direct"
|
||||||
|
return None, "unresolved"
|
||||||
|
|
||||||
# --------------------------------------------------
|
# --------------------------------------------------
|
||||||
# MAIN SCAN
|
# MAIN SCAN
|
||||||
|
|
@ -123,43 +113,29 @@ def extract_article_links(soup):
|
||||||
|
|
||||||
resolved_links = []
|
resolved_links = []
|
||||||
unresolved_links = []
|
unresolved_links = []
|
||||||
|
|
||||||
files = list(PAGES_DIR.glob("*.html"))
|
files = list(PAGES_DIR.glob("*.html"))
|
||||||
print(f"{len(files)} pages à analyser")
|
print(f"{len(files)} pages à analyser")
|
||||||
|
|
||||||
for i, file_path in enumerate(files, 1):
|
for i, file_path in enumerate(files, 1):
|
||||||
|
|
||||||
html = file_path.read_text(encoding="utf-8", errors="ignore")
|
html = file_path.read_text(encoding="utf-8", errors="ignore")
|
||||||
soup = BeautifulSoup(html, "html.parser")
|
soup = BeautifulSoup(html, "html.parser")
|
||||||
|
|
||||||
links = extract_article_links(soup)
|
links = extract_article_links(soup)
|
||||||
|
for link in links:
|
||||||
for href in links:
|
raw_target = extract_mediawiki_target(link["href"])
|
||||||
|
resolved, method = resolve_link(raw_target, link["title"])
|
||||||
raw_target = extract_mediawiki_target(href)
|
|
||||||
norm = normalize_title(raw_target)
|
|
||||||
|
|
||||||
if not norm:
|
|
||||||
continue
|
|
||||||
|
|
||||||
if is_ignored_namespace(norm):
|
|
||||||
continue
|
|
||||||
|
|
||||||
entry = {
|
entry = {
|
||||||
"source": file_path.name,
|
"source": file_path.name,
|
||||||
"href": href,
|
"href": link["href"],
|
||||||
"normalized": norm,
|
"title": link["title"],
|
||||||
|
"method": method,
|
||||||
}
|
}
|
||||||
|
|
||||||
resolved = equivalences.get(norm)
|
|
||||||
|
|
||||||
if resolved:
|
if resolved:
|
||||||
entry["resolved_title"] = resolved
|
entry["resolved"] = resolved
|
||||||
resolved_links.append(entry)
|
resolved_links.append(entry)
|
||||||
else:
|
else:
|
||||||
|
entry["raw_target"] = raw_target
|
||||||
unresolved_links.append(entry)
|
unresolved_links.append(entry)
|
||||||
|
if i % 200 == 0:
|
||||||
if i % 100 == 0:
|
|
||||||
print(f"{i}/{len(files)} analysées")
|
print(f"{i}/{len(files)} analysées")
|
||||||
|
|
||||||
# --------------------------------------------------
|
# --------------------------------------------------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue