-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtranslator.py
70 lines (54 loc) · 2.28 KB
/
translator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import json
import requests
from pyrogram import Client, filters
from utils.misc import modules_help, prefix
from utils.scripts import format_small_module_help
@Client.on_message(filters.command(["trans", "tr"], prefix) & filters.me)
async def translatedl(_client, message):
try:
# Parse command arguments
if len(message.command) > 1:
dtarget = message.command[1]
else:
dtarget = "en"
if len(message.command) > 2:
dtext = message.text.split(None, 2)[2]
elif message.reply_to_message:
dtext = message.reply_to_message.text
else:
await message.edit_text(format_small_module_help("translator"))
return
await message.edit_text("<b>Translating</b>")
# Use the Google Translate API endpoint
url = "https://clients5.google.com/translate_a/t"
params = {
"client": "dict-chrome-ex",
"sl": "auto",
"tl": dtarget,
"q": dtext
}
response = requests.get(url, params=params)
if response.status_code != 200:
await message.edit_text(f"<b>Error:</b> API returned status code {response.status_code}")
return
# Parse the JSON response
data = json.loads(response.text)
# Based on actual response format [["translated_text", "detected_language"]]
if isinstance(data, list) and len(data) > 0 and isinstance(data[0], list) and len(data[0]) > 0:
translated_text = data[0][0]
# Check if language detection is included
source_lang = data[0][1] if len(data[0]) > 1 else "auto"
else:
translated_text = "Translation failed"
source_lang = "unknown"
await message.edit_text(
f"<b>Translated</b> from <code>{source_lang}</code> to <code>{dtarget}</code>:\n\n"
+ "{}".format(translated_text)
)
except Exception as err:
await message.edit_text(f"<b>Error:</b> <code>{str(err)}</code>")
return
modules_help["translator"] = {
"tr": "[lang] [text/reply] translate message",
"trans": "[lang] [text/reply] translate message \n\nIf lang not given it'll use default(en)",
}