feat(git): initial commit,
This commit is contained in:
@@ -0,0 +1,433 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Extractor ampliado de contenido WordPress/Elementor -> src/content/site.json
|
||||
|
||||
Lee la base de datos SQLite recuperada del export de Duplicator y genera un
|
||||
site.json que mantiene compatibilidad con el que ya consumen App.jsx /
|
||||
Section.jsx / Navbar.jsx / Footer.jsx (mismas claves: site, menu, sections
|
||||
con headings/paragraphs/images/lists) y anade informacion nueva que el
|
||||
extractor anterior no capturaba: acordeones, tabs, carruseles, slideshows,
|
||||
barras de progreso, texto animado, los dos paginas legales, los menus reales
|
||||
y los campos del formulario de contacto.
|
||||
|
||||
Uso:
|
||||
python3 scripts/extract_site.py <ruta-a-wp.sqlite-valido> [salida.json]
|
||||
"""
|
||||
import sys
|
||||
import re
|
||||
import json
|
||||
import sqlite3
|
||||
|
||||
HOME_ID = 10044
|
||||
PAGES = {
|
||||
"cookies": {"id": 2316, "slug": "informacion-sobre-las-cookies"},
|
||||
"avisoLegal": {"id": 2318, "slug": "aviso-legal-y-proteccion-de-datos"},
|
||||
}
|
||||
WPFORMS_ID = 2938
|
||||
KIT_ID = 12
|
||||
MENU_TERM_IDS = {"principal": 5, "footer": 6}
|
||||
|
||||
# Rangos de indices raiz (hijos directos del arbol _elementor_data de la
|
||||
# home) que componen cada seccion anclada. Se confirmaron inspeccionando el
|
||||
# arbol: cada seccion empieza en un contenedor con settings._element_id y
|
||||
# se extiende hasta el indice anterior al siguiente anchor; "contacto" no
|
||||
# tiene su propio contenedor anclado (solo un heading suelto con ese id), asi
|
||||
# que se separa a mano de la cola de "precedentes_historicos".
|
||||
SECTION_RANGES = [
|
||||
("first-section", 0, 2),
|
||||
("significa", 2, 5),
|
||||
("inmigración_masiva", 5, 10),
|
||||
("detener_el_reemplazo_poblacional", 10, 21),
|
||||
("que_podemoshacer", 21, 28),
|
||||
("precedentes_historicos", 28, 32),
|
||||
("contacto", 32, 34),
|
||||
]
|
||||
|
||||
NESTED_WIDGET_TYPES = {
|
||||
"nested-accordion", "nested-tabs", "nested-carousel",
|
||||
"premium-carousel-widget", "slides", "premium-addon-progressbar",
|
||||
"premium-addon-fancy-text", "premium-nav-menu",
|
||||
}
|
||||
SKIP_WIDGET_TYPES = {"divider", "spacer"}
|
||||
|
||||
IMG_URL_RE = re.compile(r"https?://remigracion\.es/wp-content/uploads/[^\"'\s)]+")
|
||||
|
||||
|
||||
def php_unserialize(data):
|
||||
"""Parser minimo de php serialize() para leer el kit global de Elementor.
|
||||
Trabaja en bytes: las longitudes de PHP (s:N:"...") cuentan bytes UTF-8,
|
||||
no caracteres, y este dump tiene acentos (o, a, n) que son multibyte."""
|
||||
b = data.encode("utf-8")
|
||||
pos = 0
|
||||
|
||||
def parse():
|
||||
nonlocal pos
|
||||
kind = b[pos:pos + 1]
|
||||
if kind == b"s":
|
||||
m = re.match(rb's:(\d+):"', b[pos:])
|
||||
length = int(m.group(1))
|
||||
start = pos + m.end()
|
||||
val = b[start:start + length].decode("utf-8")
|
||||
pos = start + length + 2 # cierra comillas + ;
|
||||
return val
|
||||
if kind == b"i":
|
||||
m = re.match(rb"i:(-?\d+);", b[pos:])
|
||||
pos += m.end()
|
||||
return int(m.group(1))
|
||||
if kind == b"d":
|
||||
m = re.match(rb"d:([^;]+);", b[pos:])
|
||||
pos += m.end()
|
||||
return float(m.group(1))
|
||||
if kind == b"b":
|
||||
m = re.match(rb"b:([01]);", b[pos:])
|
||||
pos += m.end()
|
||||
return m.group(1) == b"1"
|
||||
if kind == b"N":
|
||||
pos += 2
|
||||
return None
|
||||
if kind == b"a":
|
||||
m = re.match(rb"a:(\d+):\{", b[pos:])
|
||||
count = int(m.group(1))
|
||||
pos += m.end()
|
||||
result = {}
|
||||
is_list = True
|
||||
next_idx = 0
|
||||
for _ in range(count):
|
||||
k = parse()
|
||||
v = parse()
|
||||
if k != next_idx:
|
||||
is_list = False
|
||||
next_idx += 1
|
||||
result[k] = v
|
||||
pos += 1 # cierra '}'
|
||||
if is_list:
|
||||
return [result[i] for i in range(count)]
|
||||
return result
|
||||
raise ValueError(f"tipo php-serialize no soportado: {kind!r} en {b[pos:pos+30]!r}")
|
||||
|
||||
return parse()
|
||||
|
||||
|
||||
def list_items_from_html(html):
|
||||
"""Si el HTML es una lista <ul>/<ol>, devuelve sus <li> como lista de
|
||||
strings; si no, devuelve None."""
|
||||
stripped = html.strip()
|
||||
if not re.match(r"^<(ul|ol)[\s>]", stripped, re.I):
|
||||
return None
|
||||
items = re.findall(r"<li[^>]*>(.*?)</li>", stripped, re.I | re.S)
|
||||
return [i.strip() for i in items] if items else None
|
||||
|
||||
|
||||
def extract_container(elements):
|
||||
"""Recorre recursivamente una lista de elementos Elementor y devuelve
|
||||
{headings, paragraphs, images, lists, widgets}. Los widgets anidados
|
||||
(acordeon, tabs, carrusel...) se extraen aparte en vez de aplanarse."""
|
||||
out = {"headings": [], "paragraphs": [], "images": [], "lists": [], "widgets": []}
|
||||
|
||||
def walk(nodes):
|
||||
for n in nodes:
|
||||
wtype = n.get("widgetType")
|
||||
settings = n.get("settings", {})
|
||||
|
||||
if wtype == "heading":
|
||||
html = settings.get("title", "")
|
||||
if html:
|
||||
out["headings"].append({"level": settings.get("header_size", "h2"), "html": html})
|
||||
continue
|
||||
|
||||
if wtype == "text-editor":
|
||||
html = settings.get("editor", "")
|
||||
if html:
|
||||
items = list_items_from_html(html)
|
||||
if items:
|
||||
out["lists"].append(items)
|
||||
else:
|
||||
out["paragraphs"].append(html)
|
||||
continue
|
||||
|
||||
if wtype == "image":
|
||||
img = settings.get("image", {}) or {}
|
||||
if img.get("url"):
|
||||
out["images"].append({"url": img["url"], "alt": img.get("alt", "")})
|
||||
continue
|
||||
|
||||
if wtype in SKIP_WIDGET_TYPES:
|
||||
continue
|
||||
|
||||
if wtype == "nested-accordion":
|
||||
items = settings.get("items", [])
|
||||
panels = n.get("elements", [])
|
||||
out["widgets"].append({
|
||||
"type": "accordion",
|
||||
"items": [
|
||||
{"title": it.get("item_title", ""), **extract_container(panel.get("elements", []))}
|
||||
for it, panel in zip(items, panels)
|
||||
],
|
||||
})
|
||||
continue
|
||||
|
||||
if wtype == "nested-tabs":
|
||||
tabs = settings.get("tabs", [])
|
||||
panels = n.get("elements", [])
|
||||
out["widgets"].append({
|
||||
"type": "tabs",
|
||||
"items": [
|
||||
{"title": t.get("tab_title", ""), **extract_container(panel.get("elements", []))}
|
||||
for t, panel in zip(tabs, panels)
|
||||
],
|
||||
})
|
||||
continue
|
||||
|
||||
if wtype == "nested-carousel":
|
||||
slide_meta = settings.get("carousel_items", [])
|
||||
panels = n.get("elements", [])
|
||||
out["widgets"].append({
|
||||
"type": "carousel",
|
||||
"slides": [
|
||||
{"title": m.get("slide_title", ""), **extract_container(panel.get("elements", []))}
|
||||
for m, panel in zip(slide_meta, panels)
|
||||
],
|
||||
})
|
||||
continue
|
||||
|
||||
if wtype == "premium-carousel-widget":
|
||||
# Sin contenido textual propio: solo controla un carrusel
|
||||
# visual de las secciones vecinas (progressbars). Se deja
|
||||
# constancia de que existe para que Fase 2 decida el efecto.
|
||||
rep = settings.get("premium_carousel_templates_repeater", [])
|
||||
out["widgets"].append({"type": "carousel-decorative", "slideCount": len(rep)})
|
||||
continue
|
||||
|
||||
if wtype == "slides":
|
||||
slides = settings.get("slides", [])
|
||||
out["widgets"].append({
|
||||
"type": "slideshow",
|
||||
"slides": [
|
||||
{
|
||||
"heading": s.get("heading", ""),
|
||||
"description": s.get("description", ""),
|
||||
"buttonText": s.get("button_text", ""),
|
||||
"image": {"url": (s.get("background_image") or {}).get("url", "")},
|
||||
}
|
||||
for s in slides
|
||||
],
|
||||
})
|
||||
continue
|
||||
|
||||
if wtype == "premium-addon-progressbar":
|
||||
out["widgets"].append({
|
||||
"type": "progressbar",
|
||||
"leftLabel": settings.get("premium_progressbar_left_label", ""),
|
||||
"rightLabel": settings.get("premium_progressbar_right_label", ""),
|
||||
"percent": settings.get("premium_progressbar_progress_percentage", ""),
|
||||
"image": {"url": (settings.get("image_upload") or {}).get("url", "")},
|
||||
})
|
||||
continue
|
||||
|
||||
if wtype == "premium-addon-fancy-text":
|
||||
out["widgets"].append({
|
||||
"type": "fancy-text",
|
||||
"prefix": settings.get("premium_fancy_prefix_text", ""),
|
||||
"base": settings.get("text", ""),
|
||||
"strings": [s.get("premium_text_strings_text_field", "") for s in settings.get("premium_fancy_text_strings", [])],
|
||||
"suffix": settings.get("premium_fancy_suffix_text", ""),
|
||||
})
|
||||
continue
|
||||
|
||||
if wtype == "premium-nav-menu":
|
||||
# El contenido real del menu no vive aqui (son placeholders
|
||||
# del widget); referencia al menu real de WordPress, ya
|
||||
# extraido aparte en site.json.menus.
|
||||
out["widgets"].append({"type": "nav-menu", "menuTermId": settings.get("pa_nav_menus", "")})
|
||||
continue
|
||||
|
||||
if wtype:
|
||||
# Red de seguridad: cualquier tipo de widget no previsto se
|
||||
# deja constancia de que existe en vez de perderlo en silencio.
|
||||
out["widgets"].append({"type": f"unhandled:{wtype}", "settingsKeys": list(settings.keys())[:15]})
|
||||
continue
|
||||
|
||||
if n.get("elements"):
|
||||
walk(n["elements"])
|
||||
|
||||
walk(elements)
|
||||
return out
|
||||
|
||||
|
||||
def get_elementor_data(cur, post_id):
|
||||
cur.execute("SELECT meta_value FROM wp_postmeta WHERE post_id=? AND meta_key='_elementor_data'", (post_id,))
|
||||
row = cur.fetchone()
|
||||
return json.loads(row[0]) if row and row[0] else []
|
||||
|
||||
|
||||
def extract_sections(cur):
|
||||
data = get_elementor_data(cur, HOME_ID)
|
||||
sections = []
|
||||
for anchor, start, end in SECTION_RANGES:
|
||||
content = extract_container(data[start:end])
|
||||
sections.append({"anchor": anchor, **content})
|
||||
return sections
|
||||
|
||||
|
||||
def extract_pages(cur):
|
||||
pages = {}
|
||||
for key, info in PAGES.items():
|
||||
cur.execute("SELECT post_title FROM wp_posts WHERE ID=?", (info["id"],))
|
||||
row = cur.fetchone()
|
||||
title = row[0] if row else ""
|
||||
data = get_elementor_data(cur, info["id"])
|
||||
content = extract_container(data)
|
||||
pages[key] = {"id": info["id"], "title": title, "slug": info["slug"], **content}
|
||||
return pages
|
||||
|
||||
|
||||
def resolve_menu_target(cur, object_id):
|
||||
"""post_type / post_type_archive menu items guardan la URL real en la
|
||||
pagina referenciada (object_id), no en _menu_item_url (que suele venir
|
||||
vacio para estos). Devuelve (titulo, url) o (None, None) si no aplica."""
|
||||
if not object_id:
|
||||
return None, None
|
||||
cur.execute("SELECT ID, post_title, post_name FROM wp_posts WHERE ID=?", (object_id,))
|
||||
row = cur.fetchone()
|
||||
if not row:
|
||||
return None, None
|
||||
pid, title, slug = row
|
||||
url = "/" if pid == HOME_ID else f"/{slug}"
|
||||
return title, url
|
||||
|
||||
|
||||
def extract_menus(cur):
|
||||
menus = {}
|
||||
for name, term_id in MENU_TERM_IDS.items():
|
||||
cur.execute("""
|
||||
SELECT p.ID, p.post_title, p.menu_order
|
||||
FROM wp_posts p
|
||||
JOIN wp_term_relationships tr ON tr.object_id = p.ID
|
||||
JOIN wp_term_taxonomy tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
|
||||
WHERE tt.term_id = ? AND p.post_type = 'nav_menu_item'
|
||||
ORDER BY p.menu_order
|
||||
""", (term_id,))
|
||||
items = []
|
||||
for pid, title, order in cur.fetchall():
|
||||
cur.execute("SELECT meta_key, meta_value FROM wp_postmeta WHERE post_id=? AND meta_key IN ('_menu_item_url','_menu_item_object_id','_menu_item_type')", (pid,))
|
||||
meta = dict(cur.fetchall())
|
||||
item_type = meta.get("_menu_item_type", "")
|
||||
url = meta.get("_menu_item_url", "")
|
||||
if item_type == "post_type" and not url:
|
||||
resolved_title, resolved_url = resolve_menu_target(cur, meta.get("_menu_item_object_id"))
|
||||
title = title or resolved_title or ""
|
||||
url = resolved_url or ""
|
||||
items.append({
|
||||
"order": order,
|
||||
"title": title,
|
||||
"url": url,
|
||||
"type": item_type,
|
||||
})
|
||||
menus[name] = items
|
||||
return menus
|
||||
|
||||
|
||||
def extract_theme(cur):
|
||||
cur.execute("SELECT meta_value FROM wp_postmeta WHERE post_id=? AND meta_key='_elementor_page_settings'", (KIT_ID,))
|
||||
row = cur.fetchone()
|
||||
if not row or not row[0]:
|
||||
return {}
|
||||
kit = php_unserialize(row[0])
|
||||
colors = {c["_id"]: {"title": c["title"], "hex": c["color"]} for c in kit.get("system_colors", [])}
|
||||
typo = {}
|
||||
for t in kit.get("system_typography", []):
|
||||
size = t.get("typography_font_size", {})
|
||||
typo[t["_id"]] = {
|
||||
"title": t.get("title", ""),
|
||||
"fontFamily": t.get("typography_font_family", ""),
|
||||
"fontWeight": t.get("typography_font_weight", ""),
|
||||
"fontSize": size.get("size") if isinstance(size, dict) else None,
|
||||
}
|
||||
return {"colors": colors, "typography": typo}
|
||||
|
||||
|
||||
def extract_contact_form(cur):
|
||||
cur.execute("SELECT post_content FROM wp_posts WHERE ID=?", (WPFORMS_ID,))
|
||||
row = cur.fetchone()
|
||||
if not row or not row[0]:
|
||||
return {}
|
||||
form = json.loads(row[0])
|
||||
fields = []
|
||||
for f in form.get("fields", {}).values():
|
||||
fields.append({
|
||||
"id": f.get("id"),
|
||||
"type": f.get("type"),
|
||||
"label": f.get("label"),
|
||||
"required": f.get("required") == "1",
|
||||
})
|
||||
return {"id": WPFORMS_ID, "title": form.get("form_title", ""), "fields": fields}
|
||||
|
||||
|
||||
def collect_image_urls(obj, found):
|
||||
if isinstance(obj, str):
|
||||
for m in IMG_URL_RE.findall(obj):
|
||||
found.add(m)
|
||||
elif isinstance(obj, dict):
|
||||
for v in obj.values():
|
||||
collect_image_urls(v, found)
|
||||
elif isinstance(obj, list):
|
||||
for v in obj:
|
||||
collect_image_urls(v, found)
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print("Uso: extract_site.py <wp.sqlite valido> [salida.json]", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
db_path = sys.argv[1]
|
||||
out_path = sys.argv[2] if len(sys.argv) > 2 else "src/content/site.json"
|
||||
|
||||
con = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True)
|
||||
cur = con.cursor()
|
||||
|
||||
cur.execute("SELECT option_value FROM wp_options WHERE option_name='blogname'")
|
||||
name = cur.fetchone()[0]
|
||||
cur.execute("SELECT option_value FROM wp_options WHERE option_name='blogdescription'")
|
||||
description = cur.fetchone()[0]
|
||||
|
||||
menus = extract_menus(cur)
|
||||
# "menu" (compat con Navbar.jsx actual): mismo formato que antes,
|
||||
# tomado del menu "principal".
|
||||
legacy_menu = [
|
||||
{"order": m["order"], "title": m["title"], "type": "custom", "url": m["url"], "object_id": ""}
|
||||
for m in menus["principal"]
|
||||
]
|
||||
|
||||
site_json = {
|
||||
"site": {"name": name, "description": description, "url": "http://remigracion.es", "lang": "es_ES"},
|
||||
"menu": legacy_menu,
|
||||
"menus": menus,
|
||||
"theme": extract_theme(cur),
|
||||
"sections": extract_sections(cur),
|
||||
"pages": extract_pages(cur),
|
||||
"contactForm": extract_contact_form(cur),
|
||||
}
|
||||
|
||||
image_urls = set()
|
||||
collect_image_urls(site_json, image_urls)
|
||||
|
||||
with open(out_path, "w", encoding="utf-8") as f:
|
||||
json.dump(site_json, f, ensure_ascii=False, indent=2)
|
||||
|
||||
urls_path = out_path.replace(".json", "") + ".image-urls.txt"
|
||||
with open(urls_path, "w", encoding="utf-8") as f:
|
||||
for u in sorted(image_urls):
|
||||
f.write(u + "\n")
|
||||
|
||||
print(f"OK -> {out_path}")
|
||||
print(f"Secciones: {len(site_json['sections'])}")
|
||||
for s in site_json["sections"]:
|
||||
print(f" {s['anchor']}: headings={len(s['headings'])} paragraphs={len(s['paragraphs'])} images={len(s['images'])} lists={len(s['lists'])} widgets={len(s['widgets'])}")
|
||||
print(f"Paginas: {list(site_json['pages'].keys())}")
|
||||
print(f"Imagenes referenciadas (unicas): {len(image_urls)} -> {urls_path}")
|
||||
print(f"Formulario: {site_json['contactForm'].get('fields')}")
|
||||
print(f"Tema: {json.dumps(site_json['theme'], ensure_ascii=False)[:300]}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user