Update main.py
Browse files
main.py
CHANGED
|
@@ -239,7 +239,8 @@ async def process_girlsmoodaily_endpoint():
|
|
| 239 |
|
| 240 |
records = res_get.json()
|
| 241 |
if not records:
|
| 242 |
-
|
|
|
|
| 243 |
|
| 244 |
record = records[0]
|
| 245 |
record_id = record.get("id")
|
|
@@ -792,6 +793,8 @@ LEGENDA ORIGINAL:
|
|
| 792 |
except Exception as _e:
|
| 793 |
print(f"⚠️ Erro ao enviar notificação de conclusão para o Discord: {_e}")
|
| 794 |
|
|
|
|
|
|
|
| 795 |
return response_data
|
| 796 |
except Exception as e:
|
| 797 |
import traceback
|
|
@@ -815,7 +818,8 @@ LEGENDA ORIGINAL:
|
|
| 815 |
except Exception as sup_e:
|
| 816 |
print(f"⚠️ Erro ao atualizar falha no Supabase: {sup_e}")
|
| 817 |
|
| 818 |
-
|
|
|
|
| 819 |
finally:
|
| 820 |
if temp_file and os.path.exists(temp_file.name): os.unlink(temp_file.name)
|
| 821 |
if cropped_file_path and os.path.exists(cropped_file_path): os.unlink(cropped_file_path)
|
|
@@ -823,6 +827,30 @@ LEGENDA ORIGINAL:
|
|
| 823 |
if screenshot_path and os.path.exists(screenshot_path): os.unlink(screenshot_path)
|
| 824 |
|
| 825 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 826 |
@app.api_route("/filter-girlsmoodaily", methods=["GET", "POST"])
|
| 827 |
async def filter_endpoint():
|
| 828 |
if not client:
|
|
@@ -849,7 +877,8 @@ async def filter_endpoint():
|
|
| 849 |
|
| 850 |
records = res_get.json()
|
| 851 |
if not records:
|
| 852 |
-
|
|
|
|
| 853 |
|
| 854 |
record = records[0]
|
| 855 |
record_id = record.get("id")
|
|
@@ -1058,10 +1087,12 @@ Reprovando (exemplo 2):
|
|
| 1058 |
if not res_patch.ok:
|
| 1059 |
print(f"⚠️ Erro ao atualizar Supabase: {res_patch.text}")
|
| 1060 |
|
|
|
|
| 1061 |
return {
|
| 1062 |
"success": True,
|
| 1063 |
"record_id": record_id,
|
| 1064 |
-
"filter_data": filter_data
|
|
|
|
| 1065 |
}
|
| 1066 |
except Exception as e:
|
| 1067 |
import traceback
|
|
@@ -1089,7 +1120,8 @@ Reprovando (exemplo 2):
|
|
| 1089 |
except Exception as sup_e:
|
| 1090 |
print(f"⚠️ Erro ao atualizar falha no Supabase: {sup_e}")
|
| 1091 |
|
| 1092 |
-
|
|
|
|
| 1093 |
finally:
|
| 1094 |
if temp_file and os.path.exists(temp_file.name): os.unlink(temp_file.name)
|
| 1095 |
if 'cropped_file_path' in locals() and cropped_file_path and os.path.exists(cropped_file_path):
|
|
@@ -1320,23 +1352,59 @@ Rejeitando:
|
|
| 1320 |
timeout=5
|
| 1321 |
)
|
| 1322 |
|
| 1323 |
-
# 2) Aviso final pro Painel de Sistema (ID 0)
|
| 1324 |
is_published = publish_data.get("published", False)
|
| 1325 |
needs_verification = publish_data.get("superior_needs_verification", False)
|
| 1326 |
|
|
|
|
|
|
|
| 1327 |
if is_published:
|
| 1328 |
-
|
| 1329 |
-
|
| 1330 |
-
|
| 1331 |
-
|
| 1332 |
-
|
| 1333 |
-
|
| 1334 |
-
|
| 1335 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1336 |
|
| 1337 |
-
|
| 1338 |
-
|
| 1339 |
-
|
|
|
|
|
|
|
| 1340 |
|
| 1341 |
sys_end_url = "https://discordmsg.arthurmribeiro51.workers.dev/?" + urllib.parse.urlencode({
|
| 1342 |
"mensagem": sys_end_msg,
|
|
@@ -1354,8 +1422,8 @@ Rejeitando:
|
|
| 1354 |
update_url = f"{supabase_url}/rest/v1/posts?id=eq.{record_id}"
|
| 1355 |
patch_payload = {
|
| 1356 |
"publish_message": publish_data.get("publish_message"),
|
| 1357 |
-
"published":
|
| 1358 |
-
"superior_needs_verification":
|
| 1359 |
}
|
| 1360 |
res_patch = requests.patch(update_url, headers=headers, json=patch_payload, timeout=10)
|
| 1361 |
if not res_patch.ok:
|
|
@@ -1391,7 +1459,7 @@ Rejeitando:
|
|
| 1391 |
except Exception as sup_e:
|
| 1392 |
print(f"⚠️ Erro ao atualizar falha no Supabase: {sup_e}")
|
| 1393 |
|
| 1394 |
-
|
| 1395 |
finally:
|
| 1396 |
if temp_file and os.path.exists(temp_file.name): os.unlink(temp_file.name)
|
| 1397 |
|
|
|
|
| 239 |
|
| 240 |
records = res_get.json()
|
| 241 |
if not records:
|
| 242 |
+
publish_res = await safe_call_publish()
|
| 243 |
+
return {"status": "ok", "message": "Nenhuma postagem pendente para ser processada.", "publish_result": publish_res}
|
| 244 |
|
| 245 |
record = records[0]
|
| 246 |
record_id = record.get("id")
|
|
|
|
| 793 |
except Exception as _e:
|
| 794 |
print(f"⚠️ Erro ao enviar notificação de conclusão para o Discord: {_e}")
|
| 795 |
|
| 796 |
+
publish_res = await safe_call_publish()
|
| 797 |
+
response_data["publish_result"] = publish_res
|
| 798 |
return response_data
|
| 799 |
except Exception as e:
|
| 800 |
import traceback
|
|
|
|
| 818 |
except Exception as sup_e:
|
| 819 |
print(f"⚠️ Erro ao atualizar falha no Supabase: {sup_e}")
|
| 820 |
|
| 821 |
+
publish_res = await safe_call_publish()
|
| 822 |
+
return JSONResponse(status_code=500, content={"error": f"Erro interno: {err_msg}", "publish_result": publish_res})
|
| 823 |
finally:
|
| 824 |
if temp_file and os.path.exists(temp_file.name): os.unlink(temp_file.name)
|
| 825 |
if cropped_file_path and os.path.exists(cropped_file_path): os.unlink(cropped_file_path)
|
|
|
|
| 827 |
if screenshot_path and os.path.exists(screenshot_path): os.unlink(screenshot_path)
|
| 828 |
|
| 829 |
|
| 830 |
+
async def safe_call_process():
|
| 831 |
+
try:
|
| 832 |
+
res = await process_girlsmoodaily_endpoint()
|
| 833 |
+
if isinstance(res, JSONResponse):
|
| 834 |
+
import json
|
| 835 |
+
return json.loads(res.body.decode('utf-8'))
|
| 836 |
+
return res
|
| 837 |
+
except HTTPException as e:
|
| 838 |
+
return {"error": e.detail, "status_code": e.status_code}
|
| 839 |
+
except Exception as e:
|
| 840 |
+
return {"error": str(e), "status_code": 500}
|
| 841 |
+
|
| 842 |
+
async def safe_call_publish():
|
| 843 |
+
try:
|
| 844 |
+
res = await publish_girlsmoodaily_endpoint()
|
| 845 |
+
if isinstance(res, JSONResponse):
|
| 846 |
+
import json
|
| 847 |
+
return json.loads(res.body.decode('utf-8'))
|
| 848 |
+
return res
|
| 849 |
+
except HTTPException as e:
|
| 850 |
+
return {"error": e.detail, "status_code": e.status_code}
|
| 851 |
+
except Exception as e:
|
| 852 |
+
return {"error": str(e), "status_code": 500}
|
| 853 |
+
|
| 854 |
@app.api_route("/filter-girlsmoodaily", methods=["GET", "POST"])
|
| 855 |
async def filter_endpoint():
|
| 856 |
if not client:
|
|
|
|
| 877 |
|
| 878 |
records = res_get.json()
|
| 879 |
if not records:
|
| 880 |
+
process_res = await safe_call_process()
|
| 881 |
+
return {"status": "ok", "message": "Nenhuma postagem pendente para ser filtrada.", "next_steps": {"process": process_res}}
|
| 882 |
|
| 883 |
record = records[0]
|
| 884 |
record_id = record.get("id")
|
|
|
|
| 1087 |
if not res_patch.ok:
|
| 1088 |
print(f"⚠️ Erro ao atualizar Supabase: {res_patch.text}")
|
| 1089 |
|
| 1090 |
+
process_res = await safe_call_process()
|
| 1091 |
return {
|
| 1092 |
"success": True,
|
| 1093 |
"record_id": record_id,
|
| 1094 |
+
"filter_data": filter_data,
|
| 1095 |
+
"next_steps": {"process": process_res}
|
| 1096 |
}
|
| 1097 |
except Exception as e:
|
| 1098 |
import traceback
|
|
|
|
| 1120 |
except Exception as sup_e:
|
| 1121 |
print(f"⚠️ Erro ao atualizar falha no Supabase: {sup_e}")
|
| 1122 |
|
| 1123 |
+
process_res = await safe_call_process()
|
| 1124 |
+
return JSONResponse(status_code=500, content={"error": f"Erro interno: {err_msg}", "next_steps": {"process": process_res}})
|
| 1125 |
finally:
|
| 1126 |
if temp_file and os.path.exists(temp_file.name): os.unlink(temp_file.name)
|
| 1127 |
if 'cropped_file_path' in locals() and cropped_file_path and os.path.exists(cropped_file_path):
|
|
|
|
| 1352 |
timeout=5
|
| 1353 |
)
|
| 1354 |
|
| 1355 |
+
# 2) Processo de Publicação e Aviso final pro Painel de Sistema (ID 0)
|
| 1356 |
is_published = publish_data.get("published", False)
|
| 1357 |
needs_verification = publish_data.get("superior_needs_verification", False)
|
| 1358 |
|
| 1359 |
+
sys_end_msg = ""
|
| 1360 |
+
|
| 1361 |
if is_published:
|
| 1362 |
+
# Tenta publicar de fato
|
| 1363 |
+
try:
|
| 1364 |
+
r0 = result_data[0] if isinstance(result_data, list) and len(result_data) > 0 else {}
|
| 1365 |
+
caption_text = r0.get("description", "")
|
| 1366 |
+
|
| 1367 |
+
publish_payload = {
|
| 1368 |
+
"account": "girlsmoodaily",
|
| 1369 |
+
"caption": caption_text
|
| 1370 |
+
}
|
| 1371 |
+
if is_video:
|
| 1372 |
+
publish_payload["video_url"] = final_content_url
|
| 1373 |
+
else:
|
| 1374 |
+
publish_payload["image_urls"] = [final_content_url]
|
| 1375 |
+
|
| 1376 |
+
print(f"🚀 Enviando post #{record_id} para API de publicação com proxy...")
|
| 1377 |
+
pub_resp = requests.post(
|
| 1378 |
+
"https://proxy.onrecurve.com/",
|
| 1379 |
+
params={"quest": "https://ig-publisher.arthurmribeiro51.workers.dev/"},
|
| 1380 |
+
json=publish_payload,
|
| 1381 |
+
timeout=300
|
| 1382 |
+
)
|
| 1383 |
+
|
| 1384 |
+
if pub_resp.ok:
|
| 1385 |
+
pub_json = pub_resp.json()
|
| 1386 |
+
if pub_json.get("success"):
|
| 1387 |
+
post_url = pub_json.get("instagram", {}).get("post_url", "[URL não extraída]")
|
| 1388 |
+
sys_end_msg = f"✅ **{agent_name}** APROVOU e PUBLICOU a postagem #{record_id}!\n\n🔗 Link: {post_url}"
|
| 1389 |
+
else:
|
| 1390 |
+
err_det = pub_json.get("error", pub_json.get("instagram", {}).get("error", "Erro desconhecido"))
|
| 1391 |
+
sys_end_msg = f"⚠️ **{agent_name}** APROVOU a postagem #{record_id}, mas a publicação falhou:\n`{err_det}`"
|
| 1392 |
+
is_published = False
|
| 1393 |
+
needs_verification = True
|
| 1394 |
+
else:
|
| 1395 |
+
sys_end_msg = f"⚠️ **{agent_name}** APROVOU a postagem #{record_id}, mas a API de publicação retornou status {pub_resp.status_code}:\n`{pub_resp.text}`"
|
| 1396 |
+
is_published = False
|
| 1397 |
+
needs_verification = True
|
| 1398 |
+
except Exception as pub_e:
|
| 1399 |
+
sys_end_msg = f"⚠️ **{agent_name}** APROVOU a postagem #{record_id}, mas ocorreu uma exceção ao tentar publicar:\n`{str(pub_e)}`"
|
| 1400 |
+
is_published = False
|
| 1401 |
+
needs_verification = True
|
| 1402 |
|
| 1403 |
+
if not sys_end_msg:
|
| 1404 |
+
if needs_verification:
|
| 1405 |
+
sys_end_msg = f"@everyone\n\n🔍 **{agent_name}** SOLICITOU verificação de um superior da postagem #{record_id}."
|
| 1406 |
+
else:
|
| 1407 |
+
sys_end_msg = f"❌ **{agent_name}** REJEITOU a postagem #{record_id}."
|
| 1408 |
|
| 1409 |
sys_end_url = "https://discordmsg.arthurmribeiro51.workers.dev/?" + urllib.parse.urlencode({
|
| 1410 |
"mensagem": sys_end_msg,
|
|
|
|
| 1422 |
update_url = f"{supabase_url}/rest/v1/posts?id=eq.{record_id}"
|
| 1423 |
patch_payload = {
|
| 1424 |
"publish_message": publish_data.get("publish_message"),
|
| 1425 |
+
"published": is_published,
|
| 1426 |
+
"superior_needs_verification": needs_verification
|
| 1427 |
}
|
| 1428 |
res_patch = requests.patch(update_url, headers=headers, json=patch_payload, timeout=10)
|
| 1429 |
if not res_patch.ok:
|
|
|
|
| 1459 |
except Exception as sup_e:
|
| 1460 |
print(f"⚠️ Erro ao atualizar falha no Supabase: {sup_e}")
|
| 1461 |
|
| 1462 |
+
return JSONResponse(status_code=500, content={"error": f"Erro interno: {err_msg}"})
|
| 1463 |
finally:
|
| 1464 |
if temp_file and os.path.exists(temp_file.name): os.unlink(temp_file.name)
|
| 1465 |
|