|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import json |
| 3 | +import os |
| 4 | +import sys |
| 5 | + |
| 6 | +from jinja2 import Environment, FileSystemLoader |
| 7 | + |
| 8 | + |
| 9 | +def create_meetup_file(json_file): |
| 10 | + """Crea un archivo markdown para un meetup desde JSON usando templates Jinja2.""" |
| 11 | + |
| 12 | + # Configurar Jinja2 |
| 13 | + env = Environment(loader=FileSystemLoader("templates")) |
| 14 | + |
| 15 | + # Cargar datos |
| 16 | + with open(json_file, "r", encoding="utf-8") as f: |
| 17 | + data = json.load(f) |
| 18 | + |
| 19 | + # Crear directorio |
| 20 | + year = data["id"][:4] |
| 21 | + output_dir = f"docs/meetups/{year}" |
| 22 | + os.makedirs(output_dir, exist_ok=True) |
| 23 | + |
| 24 | + # Generar archivo |
| 25 | + filename = f"{data['id']}.md" |
| 26 | + output_path = os.path.join(output_dir, filename) |
| 27 | + |
| 28 | + # Determinar qué template usar basado en el número de charlas |
| 29 | + if len(data.get("talks", [])) > 1: |
| 30 | + template = env.get_template("meetup-template-multiple-talks.md.j2") |
| 31 | + else: |
| 32 | + template = env.get_template("meetup-template.md.j2") |
| 33 | + |
| 34 | + # Preparar datos para el template |
| 35 | + template_data = { |
| 36 | + "event_title": data["event_title"], |
| 37 | + "event_subtitle": data["event_subtitle"], |
| 38 | + "event_date": data["event_date"], |
| 39 | + "event_time": data["event_time"], |
| 40 | + "event_location": data["event_location"], |
| 41 | + "event_rsvp_link": data.get("event_rsvp_link", "#"), |
| 42 | + "event_banner_image": data.get( |
| 43 | + "event_banner_image", "/assets/images/default-banner.jpg" |
| 44 | + ), |
| 45 | + "event_month_year": f"{data['event_date'].split()[1]} {data['event_date'].split()[-1]}", |
| 46 | + "tags": data["tags"], |
| 47 | + "last_update": data.get("last_update", "Generado automáticamente"), |
| 48 | + } |
| 49 | + |
| 50 | + # Agregar datos específicos según el tipo de template |
| 51 | + if len(data.get("talks", [])) > 1: |
| 52 | + template_data["talks"] = data["talks"] |
| 53 | + else: |
| 54 | + # Para template de una sola charla |
| 55 | + if data.get("talks"): |
| 56 | + talk = data["talks"][0] |
| 57 | + template_data["talk"] = talk |
| 58 | + template_data["speaker"] = talk.get("speaker", {}) |
| 59 | + else: |
| 60 | + # Fallback si no hay charlas definidas |
| 61 | + template_data["talk"] = { |
| 62 | + "title": data["event_title"], |
| 63 | + "description": data["event_subtitle"], |
| 64 | + "conclusion": "Más detalles próximamente...", |
| 65 | + "tech_stack": [], |
| 66 | + } |
| 67 | + template_data["speaker"] = { |
| 68 | + "name": "Ponente por confirmar", |
| 69 | + "title": "TBD", |
| 70 | + "bio": "Información del ponente próximamente...", |
| 71 | + "photo": "/assets/images/default-speaker.jpg", |
| 72 | + } |
| 73 | + |
| 74 | + # Agregar video si existe |
| 75 | + if data.get("video"): |
| 76 | + template_data["video"] = data["video"] |
| 77 | + |
| 78 | + # Renderizar template |
| 79 | + content = template.render(**template_data) |
| 80 | + |
| 81 | + # Escribir archivo |
| 82 | + with open(output_path, "w", encoding="utf-8") as f: |
| 83 | + f.write(content) |
| 84 | + |
| 85 | + print(f"✅ {output_path}") |
| 86 | + return output_path |
| 87 | + |
| 88 | + |
| 89 | +def main(): |
| 90 | + """Genera meetups específicos pasados como parámetros.""" |
| 91 | + |
| 92 | + if len(sys.argv) < 2: |
| 93 | + print("🚀 Generador de Meetups PythonCDMX") |
| 94 | + print("\nUso:") |
| 95 | + print(" python generate_meetups.py <archivo1.json> [archivo2.json] ...") |
| 96 | + print(" python generate_meetups.py --all") |
| 97 | + print("\nEjemplos:") |
| 98 | + print(" python generate_meetups.py metadata_json/meetup-202407.json") |
| 99 | + print( |
| 100 | + " python generate_meetups.py metadata_json/meetup-202407.json metadata_json/meetup-202408.json" |
| 101 | + ) |
| 102 | + print(" python generate_meetups.py --all") |
| 103 | + print("\nArchivos JSON disponibles:") |
| 104 | + |
| 105 | + # Listar archivos JSON disponibles |
| 106 | + import glob |
| 107 | + |
| 108 | + json_files = glob.glob("metadata_json/meetup-*.json") |
| 109 | + json_files.sort() |
| 110 | + |
| 111 | + for json_file in json_files: |
| 112 | + print(f" - {json_file}") |
| 113 | + |
| 114 | + sys.exit(1) |
| 115 | + |
| 116 | + # Verificar si se quiere generar todos |
| 117 | + if sys.argv[1] == "--all": |
| 118 | + print("🚀 Generando todos los meetups...") |
| 119 | + import glob |
| 120 | + |
| 121 | + json_files = glob.glob("metadata_json/meetup-*.json") |
| 122 | + json_files.sort() |
| 123 | + else: |
| 124 | + json_files = sys.argv[1:] |
| 125 | + |
| 126 | + print(f"📁 Procesando {len(json_files)} archivos...") |
| 127 | + |
| 128 | + count = 0 |
| 129 | + errors = 0 |
| 130 | + |
| 131 | + for json_file in json_files: |
| 132 | + try: |
| 133 | + # Verificar que el archivo existe |
| 134 | + if not os.path.exists(json_file): |
| 135 | + print(f"❌ Archivo no encontrado: {json_file}") |
| 136 | + errors += 1 |
| 137 | + continue |
| 138 | + |
| 139 | + create_meetup_file(json_file) |
| 140 | + count += 1 |
| 141 | + except Exception as e: |
| 142 | + print(f"❌ Error en {json_file}: {e}") |
| 143 | + errors += 1 |
| 144 | + |
| 145 | + print(f"\n🎉 Resultado:") |
| 146 | + print(f" ✅ Exitosos: {count}") |
| 147 | + if errors > 0: |
| 148 | + print(f" ❌ Errores: {errors}") |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == "__main__": |
| 152 | + main() |
0 commit comments