Update app.py
Browse files
app.py
CHANGED
|
@@ -149,48 +149,179 @@ def upload_to_codeberg(output_dir, repo_name, codeberg_token, username, batch_si
|
|
| 149 |
|
| 150 |
def upload_to_cloudflare_pages(output_dir, project_name, cf_token, cf_account_id, stream_format, logs):
|
| 151 |
try:
|
| 152 |
-
msg, logs = log_generator("☁️
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
yield msg, logs, None
|
| 154 |
|
| 155 |
-
headers = {"Authorization": f"Bearer {cf_token}"}
|
| 156 |
project_url = f"https://api.cloudflare.com/client/v4/accounts/{cf_account_id}/pages/projects"
|
|
|
|
| 157 |
|
| 158 |
try:
|
| 159 |
r = requests.get(project_url, headers=headers, timeout=30)
|
| 160 |
if r.status_code == 200:
|
| 161 |
projects = r.json().get('result', [])
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
|
| 191 |
except Exception as e:
|
| 192 |
-
msg, logs = log_generator(f"❌ Error Cloudflare: {str(e)}", logs)
|
| 193 |
yield msg, logs, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
# --- Procesamiento Principal ---
|
| 196 |
|
|
|
|
| 149 |
|
| 150 |
def upload_to_cloudflare_pages(output_dir, project_name, cf_token, cf_account_id, stream_format, logs):
|
| 151 |
try:
|
| 152 |
+
msg, logs = log_generator("☁️ Iniciando subida a Cloudflare Pages...", logs)
|
| 153 |
+
yield msg, logs, None
|
| 154 |
+
|
| 155 |
+
headers = {
|
| 156 |
+
"Authorization": f"Bearer {cf_token}",
|
| 157 |
+
"Content-Type": "application/json"
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
# PASO 1: Crear el proyecto en Cloudflare Pages si no existe
|
| 161 |
+
msg, logs = log_generator("📦 Verificando proyecto en Cloudflare...", logs)
|
| 162 |
yield msg, logs, None
|
| 163 |
|
|
|
|
| 164 |
project_url = f"https://api.cloudflare.com/client/v4/accounts/{cf_account_id}/pages/projects"
|
| 165 |
+
project_exists = False
|
| 166 |
|
| 167 |
try:
|
| 168 |
r = requests.get(project_url, headers=headers, timeout=30)
|
| 169 |
if r.status_code == 200:
|
| 170 |
projects = r.json().get('result', [])
|
| 171 |
+
project_exists = any(p['name'] == project_name for p in projects)
|
| 172 |
+
|
| 173 |
+
if not project_exists:
|
| 174 |
+
msg, logs = log_generator(f"📦 Creando proyecto: {project_name}", logs)
|
| 175 |
+
yield msg, logs, None
|
| 176 |
+
|
| 177 |
+
project_data = {
|
| 178 |
+
"name": project_name,
|
| 179 |
+
"production_branch": "main"
|
| 180 |
+
}
|
| 181 |
+
r = requests.post(project_url, headers=headers, json=project_data, timeout=30)
|
| 182 |
+
|
| 183 |
+
if r.status_code in [200, 201]:
|
| 184 |
+
msg, logs = log_generator(f"✅ Proyecto creado exitosamente", logs)
|
| 185 |
+
yield msg, logs, None
|
| 186 |
+
project_exists = True
|
| 187 |
+
else:
|
| 188 |
+
error_msg = r.json().get('errors', [{}])[0].get('message', 'Error desconocido')
|
| 189 |
+
raise Exception(f"Error creando proyecto: {error_msg}")
|
| 190 |
+
else:
|
| 191 |
+
msg, logs = log_generator(f"✅ Proyecto ya existe", logs)
|
| 192 |
+
yield msg, logs, None
|
| 193 |
+
else:
|
| 194 |
+
raise Exception(f"Error verificando proyectos: {r.status_code}")
|
| 195 |
+
except Exception as e:
|
| 196 |
+
msg, logs = log_generator(f"❌ Error en paso 1: {str(e)}", logs)
|
| 197 |
+
yield msg, logs, None
|
| 198 |
+
raise
|
| 199 |
+
|
| 200 |
+
if not project_exists:
|
| 201 |
+
raise Exception("No se pudo crear o verificar el proyecto")
|
| 202 |
+
|
| 203 |
+
# PASO 2: Iniciar deployment y subir archivos
|
| 204 |
+
msg, logs = log_generator("📦 Preparando deployment...", logs)
|
| 205 |
+
yield msg, logs, None
|
| 206 |
+
|
| 207 |
+
# Listar archivos a subir
|
| 208 |
+
files_to_upload = []
|
| 209 |
+
for file in output_dir.iterdir():
|
| 210 |
+
if file.is_file():
|
| 211 |
+
files_to_upload.append(file)
|
| 212 |
|
| 213 |
+
msg, logs = log_generator(f"📁 {len(files_to_upload)} archivos para subir", logs)
|
| 214 |
+
yield msg, logs, None
|
| 215 |
+
|
| 216 |
+
# Crear manifest con hashes de archivos
|
| 217 |
+
import hashlib
|
| 218 |
+
manifest = {}
|
| 219 |
+
|
| 220 |
+
for file in files_to_upload:
|
| 221 |
+
with open(file, 'rb') as f:
|
| 222 |
+
file_hash = hashlib.sha256(f.read()).hexdigest()
|
| 223 |
+
manifest[f"/{file.name}"] = file_hash
|
| 224 |
|
| 225 |
+
# Iniciar deployment con Direct Upload
|
| 226 |
+
deployment_url = f"https://api.cloudflare.com/client/v4/accounts/{cf_account_id}/pages/projects/{project_name}/deployments"
|
| 227 |
+
|
| 228 |
+
deployment_data = {
|
| 229 |
+
"branch": "main",
|
| 230 |
+
"manifest": manifest
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
msg, logs = log_generator("🚀 Iniciando deployment...", logs)
|
| 234 |
+
yield msg, logs, None
|
| 235 |
|
| 236 |
+
r = requests.post(deployment_url, headers=headers, json=deployment_data, timeout=60)
|
| 237 |
+
|
| 238 |
+
if r.status_code not in [200, 201]:
|
| 239 |
+
error_detail = r.json() if r.content else "Sin detalles"
|
| 240 |
+
raise Exception(f"Error iniciando deployment ({r.status_code}): {error_detail}")
|
| 241 |
+
|
| 242 |
+
deployment = r.json().get('result', {})
|
| 243 |
+
deployment_id = deployment.get('id')
|
| 244 |
+
|
| 245 |
+
if not deployment_id:
|
| 246 |
+
raise Exception("No se recibió deployment_id")
|
| 247 |
+
|
| 248 |
+
msg, logs = log_generator(f"✅ Deployment iniciado: {deployment_id[:8]}...", logs)
|
| 249 |
+
yield msg, logs, None
|
| 250 |
+
|
| 251 |
+
# PASO 3: Subir cada archivo
|
| 252 |
+
msg, logs = log_generator(f"⬆️ Subiendo archivos...", logs)
|
| 253 |
+
yield msg, logs, None
|
| 254 |
+
|
| 255 |
+
uploaded_count = 0
|
| 256 |
+
for idx, file in enumerate(files_to_upload):
|
| 257 |
+
try:
|
| 258 |
+
# URL para subir archivo individual
|
| 259 |
+
file_upload_url = f"https://api.cloudflare.com/client/v4/accounts/{cf_account_id}/pages/projects/{project_name}/deployments/{deployment_id}/files/{file.name}"
|
| 260 |
+
|
| 261 |
+
with open(file, 'rb') as f:
|
| 262 |
+
file_content = f.read()
|
| 263 |
+
|
| 264 |
+
upload_headers = {
|
| 265 |
+
"Authorization": f"Bearer {cf_token}",
|
| 266 |
+
"Content-Type": "application/octet-stream"
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
r = requests.put(file_upload_url, headers=upload_headers, data=file_content, timeout=120)
|
| 270 |
+
|
| 271 |
+
if r.status_code in [200, 201]:
|
| 272 |
+
uploaded_count += 1
|
| 273 |
+
if (idx + 1) % 10 == 0:
|
| 274 |
+
msg, logs = log_generator(f" ⬆️ {uploaded_count}/{len(files_to_upload)} archivos subidos", logs)
|
| 275 |
+
yield msg, logs, None
|
| 276 |
+
else:
|
| 277 |
+
msg, logs = log_generator(f" ⚠️ Error subiendo {file.name}: {r.status_code}", logs)
|
| 278 |
+
yield msg, logs, None
|
| 279 |
+
|
| 280 |
+
except Exception as e:
|
| 281 |
+
msg, logs = log_generator(f" ⚠️ Error con {file.name}: {str(e)}", logs)
|
| 282 |
+
yield msg, logs, None
|
| 283 |
+
continue
|
| 284 |
+
|
| 285 |
+
msg, logs = log_generator(f"✅ {uploaded_count}/{len(files_to_upload)} archivos subidos", logs)
|
| 286 |
+
yield msg, logs, None
|
| 287 |
+
|
| 288 |
+
# PASO 4: Finalizar deployment
|
| 289 |
+
msg, logs = log_generator("🏁 Finalizando deployment...", logs)
|
| 290 |
+
yield msg, logs, None
|
| 291 |
+
|
| 292 |
+
finalize_url = f"https://api.cloudflare.com/client/v4/accounts/{cf_account_id}/pages/projects/{project_name}/deployments/{deployment_id}/finalize"
|
| 293 |
+
|
| 294 |
+
r = requests.post(finalize_url, headers=headers, timeout=60)
|
| 295 |
+
|
| 296 |
+
if r.status_code in [200, 201]:
|
| 297 |
+
result = r.json().get('result', {})
|
| 298 |
+
cf_url = result.get('url', f"https://{project_name}.pages.dev")
|
| 299 |
+
manifest_name = "manifest.mpd" if stream_format == "DASH (MPD)" else "master.m3u8"
|
| 300 |
+
final_url = f"{cf_url}/{manifest_name}"
|
| 301 |
|
| 302 |
+
msg, logs = log_generator(f"✅ Cloudflare Pages: {final_url}", logs)
|
| 303 |
+
yield msg, logs, final_url
|
| 304 |
+
else:
|
| 305 |
+
# Aunque falle la finalización, el proyecto está creado
|
| 306 |
+
manifest_name = "manifest.mpd" if stream_format == "DASH (MPD)" else "master.m3u8"
|
| 307 |
+
final_url = f"https://{project_name}.pages.dev/{manifest_name}"
|
| 308 |
+
msg, logs = log_generator(f"⚠️ Deployment puede estar procesando", logs)
|
| 309 |
+
yield msg, logs, None
|
| 310 |
+
msg, logs = log_generator(f"📋 URL esperada: {final_url}", logs)
|
| 311 |
+
yield msg, logs, final_url
|
| 312 |
|
| 313 |
except Exception as e:
|
| 314 |
+
msg, logs = log_generator(f"❌ Error Cloudflare Pages: {str(e)}", logs)
|
| 315 |
yield msg, logs, None
|
| 316 |
+
|
| 317 |
+
# Proporcionar URL esperada como fallback
|
| 318 |
+
try:
|
| 319 |
+
manifest_name = "manifest.mpd" if stream_format == "DASH (MPD)" else "master.m3u8"
|
| 320 |
+
final_url = f"https://{project_name}.pages.dev/{manifest_name}"
|
| 321 |
+
msg, logs = log_generator(f"📋 Si el proyecto se creó, la URL será: {final_url}", logs)
|
| 322 |
+
yield msg, logs, None
|
| 323 |
+
except:
|
| 324 |
+
pass
|
| 325 |
|
| 326 |
# --- Procesamiento Principal ---
|
| 327 |
|