Update main.py
Browse files
main.py
CHANGED
|
@@ -446,12 +446,14 @@ EXEMPLOS
|
|
| 446 |
|
| 447 |
result_json = titles_data if isinstance(titles_data, list) else [titles_data]
|
| 448 |
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
|
|
|
|
|
|
| 455 |
# Usar o vídeo cortado se disponível, senão o original
|
| 456 |
video_for_export = cropped_video_path if cropped_video_path and os.path.exists(cropped_video_path) else temp_file.name
|
| 457 |
|
|
@@ -545,20 +547,45 @@ EXEMPLOS
|
|
| 545 |
)
|
| 546 |
export_resp.raise_for_status()
|
| 547 |
export_data = export_resp.json()
|
| 548 |
-
|
| 549 |
-
print(f"✅ Vídeo exportado: {
|
| 550 |
else:
|
| 551 |
print("⚠️ Falha ao obter URL do screenshot enviado.")
|
| 552 |
-
|
| 553 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 554 |
|
| 555 |
# Atualizar no Supabase
|
| 556 |
update_url = f"{supabase_url}/rest/v1/posts?id=eq.{record_id}"
|
| 557 |
patch_payload = {
|
| 558 |
"result": result_json
|
| 559 |
}
|
| 560 |
-
if
|
| 561 |
-
patch_payload["
|
| 562 |
res_patch = requests.patch(update_url, headers=headers, json=patch_payload, timeout=10)
|
| 563 |
if not res_patch.ok:
|
| 564 |
print(f"⚠️ Erro ao atualizar Supabase: {res_patch.text}")
|
|
@@ -569,8 +596,8 @@ EXEMPLOS
|
|
| 569 |
"record_id": record_id,
|
| 570 |
"result": result_json
|
| 571 |
}
|
| 572 |
-
if
|
| 573 |
-
response_data["
|
| 574 |
return response_data
|
| 575 |
except Exception as e:
|
| 576 |
raise HTTPException(status_code=500, detail=f"Erro interno: {str(e)}")
|
|
|
|
| 446 |
|
| 447 |
result_json = titles_data if isinstance(titles_data, list) else [titles_data]
|
| 448 |
|
| 449 |
+
final_content_url = None
|
| 450 |
+
if result_json:
|
| 451 |
+
result_data = result_json[0] if isinstance(result_json[0], dict) else {}
|
| 452 |
+
title_text = result_data.get("title", "")
|
| 453 |
+
|
| 454 |
+
# Se for vídeo, chamar a API de video export
|
| 455 |
+
if 'image' not in content_type and title_text:
|
| 456 |
+
try:
|
| 457 |
# Usar o vídeo cortado se disponível, senão o original
|
| 458 |
video_for_export = cropped_video_path if cropped_video_path and os.path.exists(cropped_video_path) else temp_file.name
|
| 459 |
|
|
|
|
| 547 |
)
|
| 548 |
export_resp.raise_for_status()
|
| 549 |
export_data = export_resp.json()
|
| 550 |
+
final_content_url = export_data.get("video_url")
|
| 551 |
+
print(f"✅ Vídeo exportado: {final_content_url}")
|
| 552 |
else:
|
| 553 |
print("⚠️ Falha ao obter URL do screenshot enviado.")
|
| 554 |
+
except Exception as ve:
|
| 555 |
+
print(f"⚠️ Erro no video export: {ve}")
|
| 556 |
+
|
| 557 |
+
# Se for imagem e result_type == meme
|
| 558 |
+
elif 'image' in content_type and title_text and result_data.get("result_type") == "meme":
|
| 559 |
+
try:
|
| 560 |
+
img_for_meme = cropped_file_path if cropped_file_path and os.path.exists(cropped_file_path) else temp_file.name
|
| 561 |
+
print("📸 Enviando imagem base do meme para recurve-save...")
|
| 562 |
+
with open(img_for_meme, 'rb') as img_f:
|
| 563 |
+
upload_resp = requests.post(
|
| 564 |
+
"https://habulaj-recurve-save.hf.space/upload",
|
| 565 |
+
files={'files': ('meme_base.jpg', img_f, 'image/jpeg')},
|
| 566 |
+
timeout=60
|
| 567 |
+
)
|
| 568 |
+
upload_resp.raise_for_status()
|
| 569 |
+
uploaded_img_url = upload_resp.json().get("url", "")
|
| 570 |
+
|
| 571 |
+
if uploaded_img_url:
|
| 572 |
+
import urllib.parse
|
| 573 |
+
meme_params = urllib.parse.urlencode({
|
| 574 |
+
"text": title_text,
|
| 575 |
+
"image_url": uploaded_img_url
|
| 576 |
+
})
|
| 577 |
+
final_content_url = f"https://habulaj-recurve-api-img.hf.space/meme?{meme_params}"
|
| 578 |
+
print(f"✅ Meme API URL gerada: {final_content_url}")
|
| 579 |
+
except Exception as e:
|
| 580 |
+
print(f"⚠️ Erro ao gerar URL do meme: {e}")
|
| 581 |
|
| 582 |
# Atualizar no Supabase
|
| 583 |
update_url = f"{supabase_url}/rest/v1/posts?id=eq.{record_id}"
|
| 584 |
patch_payload = {
|
| 585 |
"result": result_json
|
| 586 |
}
|
| 587 |
+
if final_content_url:
|
| 588 |
+
patch_payload["final_content_url"] = final_content_url
|
| 589 |
res_patch = requests.patch(update_url, headers=headers, json=patch_payload, timeout=10)
|
| 590 |
if not res_patch.ok:
|
| 591 |
print(f"⚠️ Erro ao atualizar Supabase: {res_patch.text}")
|
|
|
|
| 596 |
"record_id": record_id,
|
| 597 |
"result": result_json
|
| 598 |
}
|
| 599 |
+
if final_content_url:
|
| 600 |
+
response_data["final_content_url"] = final_content_url
|
| 601 |
return response_data
|
| 602 |
except Exception as e:
|
| 603 |
raise HTTPException(status_code=500, detail=f"Erro interno: {str(e)}")
|