danicor commited on
Commit
e2bf989
·
verified ·
1 Parent(s): c9d69e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -49
app.py CHANGED
@@ -523,19 +523,18 @@ class MultilingualTranslator:
523
  def notify_wordpress_completion_and_charge(request_id: str, character_count: int,
524
  translation_length: int, source_lang: str,
525
  target_lang: str):
526
- """Enhanced WordPress notification with redirect handling"""
527
  try:
528
- import urllib3
529
- from urllib3.exceptions import InsecureRequestWarning
530
- urllib3.disable_warnings(InsecureRequestWarning)
 
 
 
531
 
532
  # استفاده از URL از تنظیمات محیطی
533
  wordpress_base_url = os.getenv("WORDPRESS_NOTIFICATION_URL", "https://echovizio.us.to")
534
 
535
- # اگر آدرس با http شروع شده، آن را به https تبدیل کن
536
- if wordpress_base_url.startswith('http://'):
537
- wordpress_base_url = wordpress_base_url.replace('http://', 'https://')
538
-
539
  payload = {
540
  'request_id': request_id,
541
  'character_count': character_count,
@@ -543,72 +542,45 @@ def notify_wordpress_completion_and_charge(request_id: str, character_count: int
543
  'source_lang': source_lang,
544
  'target_lang': target_lang,
545
  'completed_at': datetime.now().isoformat(),
546
- 'status': 'completed'
 
 
 
 
547
  }
548
 
549
  headers = {
550
  'Content-Type': 'application/json',
551
- 'User-Agent': 'MLT-Server/1.0',
552
- 'Accept': 'application/json'
553
  }
554
 
555
  notification_url = wordpress_base_url.rstrip('/') + '/wp-json/mlt/v1/notification'
556
 
557
- print(f"🔔 Attempting WordPress notification to: {notification_url}")
558
 
559
  try:
560
- # ایجاد session برای مدیریت redirectها
561
- session = requests.Session()
562
- session.verify = False # غیرفعال کردن verify SSL
563
- session.max_redirects = 5 # محدودیت redirect
564
-
565
- # عدم پیروی از redirectها برای دیباگ
566
- response = session.post(
567
  notification_url,
568
  json=payload,
569
  headers=headers,
570
  timeout=30,
571
- allow_redirects=False # ابتدا بدون redirect امتحان کن
572
  )
573
 
574
- print(f"🔔 Initial response status: {response.status_code}")
575
- print(f"🔔 Response headers: {dict(response.headers)}")
576
-
577
- # اگر redirect بود، آدرس جدید را دنبال کن
578
- if response.status_code in [301, 302, 307]:
579
- redirect_url = response.headers.get('Location', '')
580
- print(f"🔔 Redirect detected to: {redirect_url}")
581
-
582
- if redirect_url:
583
- # امتحان کردن آدرس redirect
584
- response = session.post(
585
- redirect_url,
586
- json=payload,
587
- headers=headers,
588
- timeout=30,
589
- allow_redirects=True
590
- )
591
- print(f"🔔 Redirect response status: {response.status_code}")
592
-
593
- # بررسی نهایی نتیجه
594
- if response.status_code in [200, 201]:
595
- print(f"✅ WordPress notification successful for request {request_id}")
596
- print(f"✅ Response: {response.text[:200]}")
597
  return True
598
  else:
599
  print(f"❌ WordPress notification failed: {response.status_code}")
600
- print(f"❌ Response body: {response.text[:500]}")
601
  return False
602
 
603
- except requests.exceptions.TooManyRedirects as e:
604
- print(f"❌ Too many redirects: {str(e)}")
605
- return False
606
- except requests.exceptions.RequestException as e:
607
  print(f"❌ Request error: {str(e)}")
608
  return False
609
 
610
  except Exception as e:
611
- print(f"❌ WordPress notification setup error: {str(e)}")
612
  return False
613
 
614
  def process_heavy_translation_background(text: str, source_lang: str, target_lang: str,
 
523
  def notify_wordpress_completion_and_charge(request_id: str, character_count: int,
524
  translation_length: int, source_lang: str,
525
  target_lang: str):
526
+ """ارسال مستقیم نتیجه ترجمه به وردپرس"""
527
  try:
528
+ # ابتدا نتیجه ترجمه را پیدا کن
529
+ if request_id not in translator.completed_translations:
530
+ print(f"❌ Translation result not found for {request_id}")
531
+ return False
532
+
533
+ translation_data = translator.completed_translations[request_id]
534
 
535
  # استفاده از URL از تنظیمات محیطی
536
  wordpress_base_url = os.getenv("WORDPRESS_NOTIFICATION_URL", "https://echovizio.us.to")
537
 
 
 
 
 
538
  payload = {
539
  'request_id': request_id,
540
  'character_count': character_count,
 
542
  'source_lang': source_lang,
543
  'target_lang': target_lang,
544
  'completed_at': datetime.now().isoformat(),
545
+ 'status': 'completed',
546
+ # ارسال مستقیم نتیجه ترجمه
547
+ 'translated_text': translation_data['result']['translated_text'],
548
+ 'processing_time': translation_data['result']['processing_time'],
549
+ 'from_cache': translation_data['result'].get('from_cache', False)
550
  }
551
 
552
  headers = {
553
  'Content-Type': 'application/json',
554
+ 'User-Agent': 'MLT-Server/1.0'
 
555
  }
556
 
557
  notification_url = wordpress_base_url.rstrip('/') + '/wp-json/mlt/v1/notification'
558
 
559
+ print(f"🔔 Sending translation result to: {notification_url}")
560
 
561
  try:
562
+ response = requests.post(
 
 
 
 
 
 
563
  notification_url,
564
  json=payload,
565
  headers=headers,
566
  timeout=30,
567
+ verify=False
568
  )
569
 
570
+ if response.status_code == 200:
571
+ print(f" WordPress notification successful for {request_id}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
572
  return True
573
  else:
574
  print(f"❌ WordPress notification failed: {response.status_code}")
575
+ print(f"❌ Response: {response.text}")
576
  return False
577
 
578
+ except Exception as e:
 
 
 
579
  print(f"❌ Request error: {str(e)}")
580
  return False
581
 
582
  except Exception as e:
583
+ print(f"❌ Notification setup error: {str(e)}")
584
  return False
585
 
586
  def process_heavy_translation_background(text: str, source_lang: str, target_lang: str,