habulaj commited on
Commit
2543039
·
verified ·
1 Parent(s): a0af0d5

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +70 -1
main.py CHANGED
@@ -1177,6 +1177,75 @@ async def publish_girlsmoodaily_endpoint():
1177
  discord_id = 3
1178
  agent_name = "Amanda"
1179
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1180
  # Notificação de início no Discord
1181
  try:
1182
  import urllib.parse
@@ -1459,7 +1528,7 @@ Rejeitando:
1459
  try:
1460
  update_url = f"{supabase_url}/rest/v1/posts?id=eq.{record_id}"
1461
  patch_payload = {
1462
- "publish_message": f"Erro interno: {err_msg}",
1463
  "published": False,
1464
  "superior_needs_verification": True
1465
  }
 
1177
  discord_id = 3
1178
  agent_name = "Amanda"
1179
 
1180
+ publish_message = record.get("publish_message")
1181
+ if publish_message and not record.get("published", False):
1182
+ print(f"🔄 Post #{record_id} já possui publish_message. Tentando publicar direto no Instagram...")
1183
+ try:
1184
+ head_resp = requests.head(final_content_url, timeout=15)
1185
+ content_type = head_resp.headers.get('content-type', '').lower()
1186
+ is_video = 'image' not in content_type
1187
+
1188
+ r0 = result_data[0] if isinstance(result_data, list) and len(result_data) > 0 else {}
1189
+ caption_text = r0.get("description", "")
1190
+
1191
+ publish_payload = {
1192
+ "account": "girlsmoodaily",
1193
+ "caption": caption_text
1194
+ }
1195
+ if is_video:
1196
+ publish_payload["video_url"] = final_content_url
1197
+ else:
1198
+ publish_payload["image_urls"] = [final_content_url]
1199
+
1200
+ print(f"🚀 Enviando post #{record_id} para API de publicação (RETRY)...")
1201
+ pub_resp = requests.post(
1202
+ "https://igpublish.onrecurve.com/",
1203
+ json=publish_payload,
1204
+ timeout=300
1205
+ )
1206
+
1207
+ is_published = False
1208
+ needs_verification = False
1209
+ sys_end_msg = ""
1210
+
1211
+ if pub_resp.ok:
1212
+ pub_json = pub_resp.json()
1213
+ if pub_json.get("success"):
1214
+ post_url = pub_json.get("instagram", {}).get("post_url", "[URL não extraída]")
1215
+ sys_end_msg = f"✅ **{agent_name}** PUBLICOU a postagem #{record_id} com sucesso (RETRY)!\n\n🔗 Link: {post_url}"
1216
+ is_published = True
1217
+ else:
1218
+ err_det = pub_json.get("error", pub_json.get("instagram", {}).get("error", "Erro desconhecido"))
1219
+ sys_end_msg = f"⚠️ A publicação do post #{record_id} falhou novamente (RETRY):\n`{err_det}`"
1220
+ needs_verification = True
1221
+ else:
1222
+ sys_end_msg = f"⚠️ API de publicação retornou status {pub_resp.status_code} no RETRY:\n`{pub_resp.text}`"
1223
+ needs_verification = True
1224
+
1225
+ if sys_end_msg:
1226
+ import urllib.parse
1227
+ sys_end_url = "https://discordmsg.arthurmribeiro51.workers.dev/?" + urllib.parse.urlencode({
1228
+ "mensagem": sys_end_msg,
1229
+ "id": 0
1230
+ })
1231
+ requests.get("https://proxy.onrecurve.com/", params={"quest": sys_end_url}, timeout=5)
1232
+
1233
+ update_url = f"{supabase_url}/rest/v1/posts?id=eq.{record_id}"
1234
+ patch_payload = {
1235
+ "published": is_published,
1236
+ "superior_needs_verification": needs_verification
1237
+ }
1238
+ requests.patch(update_url, headers=headers, json=patch_payload, timeout=10)
1239
+
1240
+ return {
1241
+ "success": True,
1242
+ "record_id": record_id,
1243
+ "retry_published": is_published
1244
+ }
1245
+ except Exception as retry_e:
1246
+ print(f"⚠️ Erro no retry de publicação: {retry_e}")
1247
+ raise
1248
+
1249
  # Notificação de início no Discord
1250
  try:
1251
  import urllib.parse
 
1528
  try:
1529
  update_url = f"{supabase_url}/rest/v1/posts?id=eq.{record_id}"
1530
  patch_payload = {
1531
+ "publish_message": None,
1532
  "published": False,
1533
  "superior_needs_verification": True
1534
  }