Update app.py
Browse files
app.py
CHANGED
|
@@ -521,12 +521,14 @@ class MultilingualTranslator:
|
|
| 521 |
def notify_wordpress_completion_and_charge(request_id: str, character_count: int,
|
| 522 |
translation_length: int, source_lang: str,
|
| 523 |
target_lang: str):
|
| 524 |
-
"""
|
| 525 |
try:
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
|
|
|
|
|
|
| 530 |
|
| 531 |
payload = {
|
| 532 |
'request_id': request_id,
|
|
@@ -534,23 +536,35 @@ def notify_wordpress_completion_and_charge(request_id: str, character_count: int
|
|
| 534 |
'translation_length': translation_length,
|
| 535 |
'source_lang': source_lang,
|
| 536 |
'target_lang': target_lang,
|
| 537 |
-
'completed_at': datetime.now().isoformat()
|
|
|
|
| 538 |
}
|
| 539 |
|
| 540 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 541 |
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
if request_id in translator.translation_requests:
|
| 545 |
-
translator.translation_requests[request_id]['auto_charged'] = True
|
| 546 |
-
print(f"اطلاعرسانی موفق برای درخواست {request_id}")
|
| 547 |
-
return True
|
| 548 |
-
else:
|
| 549 |
-
print(f"خطا در اطلاعرسانی: {response.status_code}")
|
| 550 |
-
return False
|
| 551 |
|
| 552 |
except Exception as e:
|
| 553 |
-
print(f"
|
| 554 |
return False
|
| 555 |
|
| 556 |
def process_heavy_translation_background(text: str, source_lang: str, target_lang: str,
|
|
|
|
| 521 |
def notify_wordpress_completion_and_charge(request_id: str, character_count: int,
|
| 522 |
translation_length: int, source_lang: str,
|
| 523 |
target_lang: str):
|
| 524 |
+
"""Enhanced WordPress notification with multiple URL attempts"""
|
| 525 |
try:
|
| 526 |
+
# Try multiple possible WordPress URLs
|
| 527 |
+
wordpress_urls = [
|
| 528 |
+
os.getenv("WORDPRESS_NOTIFICATION_URL"),
|
| 529 |
+
"http://localhost/wp-json/mlt/v1/notification", # For localhost testing
|
| 530 |
+
"http://127.0.0.1/wp-json/mlt/v1/notification" # Alternative localhost
|
| 531 |
+
]
|
| 532 |
|
| 533 |
payload = {
|
| 534 |
'request_id': request_id,
|
|
|
|
| 536 |
'translation_length': translation_length,
|
| 537 |
'source_lang': source_lang,
|
| 538 |
'target_lang': target_lang,
|
| 539 |
+
'completed_at': datetime.now().isoformat(),
|
| 540 |
+
'status': 'completed'
|
| 541 |
}
|
| 542 |
|
| 543 |
+
for wordpress_url in wordpress_urls:
|
| 544 |
+
if not wordpress_url:
|
| 545 |
+
continue
|
| 546 |
+
|
| 547 |
+
try:
|
| 548 |
+
response = requests.post(wordpress_url, json=payload, timeout=10)
|
| 549 |
+
|
| 550 |
+
if response.status_code == 200:
|
| 551 |
+
# Mark as auto charged
|
| 552 |
+
if request_id in translator.translation_requests:
|
| 553 |
+
translator.translation_requests[request_id]['auto_charged'] = True
|
| 554 |
+
print(f"WordPress notification successful for request {request_id}")
|
| 555 |
+
return True
|
| 556 |
+
else:
|
| 557 |
+
print(f"WordPress notification failed: {response.status_code} - {wordpress_url}")
|
| 558 |
+
|
| 559 |
+
except requests.exceptions.RequestException as e:
|
| 560 |
+
print(f"WordPress notification error for {wordpress_url}: {str(e)}")
|
| 561 |
+
continue
|
| 562 |
|
| 563 |
+
print(f"All WordPress notification attempts failed for request {request_id}")
|
| 564 |
+
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 565 |
|
| 566 |
except Exception as e:
|
| 567 |
+
print(f"WordPress notification error: {str(e)}")
|
| 568 |
return False
|
| 569 |
|
| 570 |
def process_heavy_translation_background(text: str, source_lang: str, target_lang: str,
|