2020-02-19 15:31:39 +00:00
|
|
|
""" PagerMaid features that uses external HTTP APIs other than Telegram. """
|
|
|
|
|
2021-04-13 09:37:02 +00:00
|
|
|
from pygoogletranslation import Translator
|
2020-02-19 15:31:39 +00:00
|
|
|
from os import remove
|
2020-10-01 09:59:52 +00:00
|
|
|
from magic_google import MagicGoogle
|
2020-02-19 15:31:39 +00:00
|
|
|
from gtts import gTTS
|
2021-09-03 11:03:34 +00:00
|
|
|
from gtts.tts import gTTSError
|
2020-02-19 15:31:39 +00:00
|
|
|
from re import compile as regex_compile
|
|
|
|
from pagermaid import log
|
|
|
|
from pagermaid.listener import listener, config
|
2021-06-15 04:31:05 +00:00
|
|
|
from pagermaid.utils import clear_emojis, attach_log, fetch_youtube_audio, lang, alias_command
|
2020-02-19 15:31:39 +00:00
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command('translate'),
|
2021-04-12 16:25:32 +00:00
|
|
|
description=lang('translate_des'),
|
|
|
|
parameters=lang('translate_parameters'))
|
2020-02-19 15:31:39 +00:00
|
|
|
async def translate(context):
|
|
|
|
""" PagerMaid universal translator. """
|
|
|
|
translator = Translator()
|
|
|
|
reply = await context.get_reply_message()
|
|
|
|
message = context.arguments
|
2021-04-13 09:37:02 +00:00
|
|
|
ap_lang = config['application_language']
|
2020-02-19 15:31:39 +00:00
|
|
|
if message:
|
|
|
|
pass
|
|
|
|
elif reply:
|
|
|
|
message = reply.text
|
|
|
|
else:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('arg_error'))
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('translate_processing'))
|
2020-11-28 14:28:33 +00:00
|
|
|
try:
|
2021-04-13 09:37:02 +00:00
|
|
|
result = translator.translate(clear_emojis(message), dest=ap_lang)
|
2020-11-28 14:28:33 +00:00
|
|
|
except:
|
|
|
|
from translate import Translator as trans
|
2021-04-13 09:37:02 +00:00
|
|
|
result = trans(to_lang=ap_lang.replace('zh-cn', 'zh')).translate(clear_emojis(message))
|
2020-02-19 15:31:39 +00:00
|
|
|
except ValueError:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('translate_ValueError'))
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
2021-06-19 07:25:31 +00:00
|
|
|
try:
|
|
|
|
source_lang = result.src
|
|
|
|
source_text = result.origin
|
|
|
|
trans_lang = result.dest
|
|
|
|
except AttributeError:
|
|
|
|
await context.edit(lang('google_connection_error'))
|
|
|
|
return
|
2021-04-13 09:37:02 +00:00
|
|
|
result = f"**{lang('translate_hits')}**\n{lang('translate_original_lang')}: {source_lang}\n{source_text} -> {result.text}"
|
2020-02-19 15:31:39 +00:00
|
|
|
|
|
|
|
if len(result) > 4096:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('translate_tg_limit_uploading_file'))
|
2020-02-19 15:31:39 +00:00
|
|
|
await attach_log(result, context.chat_id, "translation.txt", context.id)
|
|
|
|
return
|
|
|
|
await context.edit(result)
|
|
|
|
if len(result) <= 4096:
|
2021-04-13 09:37:02 +00:00
|
|
|
await log(f"{lang('translate_get')}: `{source_text}` \n{lang('translate_from')} {source_lang} {lang('translate_to')} {trans_lang}")
|
2020-02-19 15:31:39 +00:00
|
|
|
else:
|
2021-04-13 09:37:02 +00:00
|
|
|
await log(f"{lang('translate_get')}{translate('translate_from')} {source_lang} {lang('translate_to')} {trans_lang}.")
|
2020-02-19 15:31:39 +00:00
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command('tts'),
|
2021-04-12 16:25:32 +00:00
|
|
|
description=lang('tts_des'),
|
2020-02-19 15:31:39 +00:00
|
|
|
parameters="<string>")
|
|
|
|
async def tts(context):
|
|
|
|
""" Send TTS stuff as voice message. """
|
|
|
|
reply = await context.get_reply_message()
|
|
|
|
message = context.arguments
|
2021-04-13 09:37:02 +00:00
|
|
|
ap_lang = config['application_tts']
|
2020-02-19 15:31:39 +00:00
|
|
|
if message:
|
|
|
|
pass
|
|
|
|
elif reply:
|
|
|
|
message = reply.text
|
|
|
|
else:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('arg_error'))
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('tts_processing'))
|
2021-04-13 09:37:02 +00:00
|
|
|
gTTS(message, lang=ap_lang)
|
2020-02-19 15:31:39 +00:00
|
|
|
except AssertionError:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('tts_AssertionError'))
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
|
|
|
except ValueError:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('tts_ValueError'))
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
|
|
|
except RuntimeError:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('tts_RuntimeError'))
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
2021-04-13 09:37:02 +00:00
|
|
|
google_tts = gTTS(message, lang=ap_lang)
|
2021-06-19 07:25:31 +00:00
|
|
|
try:
|
|
|
|
google_tts.save("vocals.mp3")
|
|
|
|
except AssertionError:
|
|
|
|
await context.edit(lang('tts_AssertionError'))
|
|
|
|
return
|
2021-08-27 11:33:45 +00:00
|
|
|
except ConnectionError:
|
|
|
|
await context.edit(lang('tts_RuntimeError'))
|
|
|
|
return
|
2021-09-03 11:03:34 +00:00
|
|
|
except gTTSError:
|
|
|
|
await context.edit(lang('tts_RuntimeError'))
|
|
|
|
return
|
2020-02-19 15:31:39 +00:00
|
|
|
with open("vocals.mp3", "rb") as audio:
|
|
|
|
line_list = list(audio)
|
|
|
|
line_count = len(line_list)
|
|
|
|
if line_count == 1:
|
2021-04-13 09:37:02 +00:00
|
|
|
google_tts = gTTS(message, lang=ap_lang)
|
2020-02-19 15:31:39 +00:00
|
|
|
google_tts.save("vocals.mp3")
|
|
|
|
with open("vocals.mp3", "r"):
|
2021-06-19 07:25:31 +00:00
|
|
|
try:
|
|
|
|
await context.client.send_file(context.chat_id, "vocals.mp3", voice_note=True)
|
|
|
|
remove("vocals.mp3")
|
|
|
|
except:
|
|
|
|
pass
|
2020-02-19 15:31:39 +00:00
|
|
|
if len(message) <= 4096:
|
2021-04-12 16:25:32 +00:00
|
|
|
await log(f"{lang('tts_success')}: `{message}`.")
|
2020-02-19 15:31:39 +00:00
|
|
|
else:
|
2021-04-12 16:25:32 +00:00
|
|
|
await log(lang('tts_success'))
|
2020-02-19 15:31:39 +00:00
|
|
|
await context.delete()
|
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command('google'),
|
2021-04-12 16:25:32 +00:00
|
|
|
description=lang('google_des'),
|
2020-02-19 15:31:39 +00:00
|
|
|
parameters="<query>")
|
2020-07-24 12:04:29 +00:00
|
|
|
async def googletest(context):
|
2020-02-19 15:31:39 +00:00
|
|
|
""" Searches Google for a string. """
|
2020-10-01 09:59:52 +00:00
|
|
|
mg = MagicGoogle()
|
2020-08-07 05:38:31 +00:00
|
|
|
reply = await context.get_reply_message()
|
|
|
|
query = context.arguments
|
|
|
|
if query:
|
|
|
|
pass
|
|
|
|
elif reply:
|
|
|
|
query = reply.text
|
|
|
|
else:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('arg_error'))
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
2020-08-07 05:38:31 +00:00
|
|
|
|
2020-07-24 12:04:29 +00:00
|
|
|
query = query.replace(' ', '+')
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('google_processing'))
|
2020-08-01 23:21:14 +00:00
|
|
|
results = ""
|
2020-10-01 09:59:52 +00:00
|
|
|
for i in mg.search(query=query, num=int(config['result_length'])):
|
|
|
|
try:
|
|
|
|
title = i['text'][0:30] + '...'
|
|
|
|
link = i['url']
|
2020-08-01 23:21:14 +00:00
|
|
|
results += f"\n[{title}]({link}) \n"
|
2020-10-01 09:59:52 +00:00
|
|
|
except:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('google_connection_error'))
|
2020-10-01 09:59:52 +00:00
|
|
|
return
|
2020-08-01 23:21:14 +00:00
|
|
|
await context.edit(f"**Google** |`{query}`| 🎙 🔍 \n"
|
|
|
|
f"{results}",
|
|
|
|
link_preview=False)
|
2021-04-12 16:25:32 +00:00
|
|
|
await log(f"{lang('google_success')} `{query}`")
|
2020-02-19 15:31:39 +00:00
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command('fetchaudio'),
|
2021-04-12 16:25:32 +00:00
|
|
|
description=lang('fetchaudio_des'),
|
2020-02-20 13:37:19 +00:00
|
|
|
parameters="<url>,<string>")
|
2020-02-19 15:31:39 +00:00
|
|
|
async def fetchaudio(context):
|
2020-02-20 13:37:19 +00:00
|
|
|
if context.arguments:
|
|
|
|
if ',' in context.arguments:
|
|
|
|
url, string_2 = context.arguments.split(',', 1)
|
|
|
|
else:
|
|
|
|
url = context.arguments
|
|
|
|
string_2 = "#audio "
|
|
|
|
else:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('fetchaudio_error_grammer'))
|
2020-02-20 13:37:19 +00:00
|
|
|
return
|
2020-02-19 15:31:39 +00:00
|
|
|
""" Fetches audio from provided URL. """
|
|
|
|
reply = await context.get_reply_message()
|
|
|
|
reply_id = None
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('fetchaudio_processing'))
|
2020-02-19 15:31:39 +00:00
|
|
|
if reply:
|
|
|
|
reply_id = reply.id
|
|
|
|
if url is None:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('arg_error'))
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
|
|
|
youtube_pattern = regex_compile(r"^(http(s)?://)?((w){3}.)?youtu(be|.be)?(\.com)?/.+")
|
|
|
|
if youtube_pattern.match(url):
|
2020-02-20 13:37:19 +00:00
|
|
|
if not await fetch_youtube_audio(url, context.chat_id, reply_id, string_2):
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('fetchaudio_error_downloading'))
|
|
|
|
await log(f"{lang('fetchaudio_success')}, {lang('fetchaudio_link')}: {url}.")
|
2020-02-20 13:56:11 +00:00
|
|
|
await context.delete()
|