added possibility to translate messages with the google
This commit is contained in:
parent
20e0025c26
commit
2f28bf2d05
1 changed files with 25 additions and 6 deletions
|
@ -13,6 +13,7 @@ from typing import Any
|
||||||
from urllib.error import HTTPError
|
from urllib.error import HTTPError
|
||||||
|
|
||||||
import telepot
|
import telepot
|
||||||
|
from googletrans import Translator
|
||||||
from telepot.aio import DelegatorBot
|
from telepot.aio import DelegatorBot
|
||||||
from telepot.aio.delegate import per_chat_id, create_open, pave_event_space
|
from telepot.aio.delegate import per_chat_id, create_open, pave_event_space
|
||||||
from telepot.aio.loop import MessageLoop
|
from telepot.aio.loop import MessageLoop
|
||||||
|
@ -41,6 +42,7 @@ info_str = "*Mensa-Bot der Hochschule Mittweida (beta)*\nDieser Bot versendet je
|
||||||
"[vecteezy.com](https://de.vecteezy.com)"
|
"[vecteezy.com](https://de.vecteezy.com)"
|
||||||
status = ""
|
status = ""
|
||||||
essen = []
|
essen = []
|
||||||
|
essen_eng = []
|
||||||
var = True
|
var = True
|
||||||
datum = None
|
datum = None
|
||||||
|
|
||||||
|
@ -131,11 +133,16 @@ class HSMensaW(telepot.aio.helper.ChatHandler):
|
||||||
write_config()
|
write_config()
|
||||||
|
|
||||||
|
|
||||||
async def send_essen(chat_id: int) -> None:
|
async def send_essen(chat_id: int, deutsch: bool = True) -> None:
|
||||||
global datum, essen
|
global datum, essen, essen_eng
|
||||||
|
if deutsch:
|
||||||
nachricht = "Speiseplan am %s:\n" % datum.strftime("%A, den %d. %B %Y")
|
nachricht = "Speiseplan am %s:\n" % datum.strftime("%A, den %d. %B %Y")
|
||||||
for i in essen:
|
for i in essen:
|
||||||
nachricht += "- " + str(i).replace(".", ",") + "\n\n"
|
nachricht += "- " + str(i).replace(".", ",") + "\n\n"
|
||||||
|
else:
|
||||||
|
nachricht = "Menu on %s:\n" % datum.strftime("%A, %d %B %Y")
|
||||||
|
for i in essen_eng:
|
||||||
|
nachricht += "- " + str(i) + "\n\n"
|
||||||
await send_message(bot_obj=bot, chat_id=chat_id, msg=nachricht, parse_mode="markdown")
|
await send_message(bot_obj=bot, chat_id=chat_id, msg=nachricht, parse_mode="markdown")
|
||||||
|
|
||||||
|
|
||||||
|
@ -183,8 +190,9 @@ async def get_essen(only_today: bool) -> None:
|
||||||
:param only_today: only current day; no download if the mensa is closed
|
:param only_today: only current day; no download if the mensa is closed
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
global datum, essen, var # , ctx
|
global datum, essen, essen_eng, var # , ctx
|
||||||
essen = []
|
essen = []
|
||||||
|
essen_eng = []
|
||||||
try:
|
try:
|
||||||
# response = urllib.request.urlopen(url, context=ctx)
|
# response = urllib.request.urlopen(url, context=ctx)
|
||||||
response = urllib.request.urlopen(url)
|
response = urllib.request.urlopen(url)
|
||||||
|
@ -221,15 +229,24 @@ async def get_essen(only_today: bool) -> None:
|
||||||
datum = date_dt
|
datum = date_dt
|
||||||
menus = et.findall("./menus/day[date='" + datum.isoformat() + "']/menu")
|
menus = et.findall("./menus/day[date='" + datum.isoformat() + "']/menu")
|
||||||
|
|
||||||
|
essen_transl = []
|
||||||
|
preise = []
|
||||||
for i in menus:
|
for i in menus:
|
||||||
kategorie = i.findall("type")[0].text
|
kategorie = i.findall("type")[0].text
|
||||||
|
essen_transl.append(kategorie)
|
||||||
name = i.findall("description")[0].text.rstrip("()1234567890, ")
|
name = i.findall("description")[0].text.rstrip("()1234567890, ")
|
||||||
|
essen_transl.append(name)
|
||||||
try:
|
try:
|
||||||
preis = float(i.findall("./prices/price[label='Studenten']/value")[0].text.replace(",", "."))
|
preis = float(i.findall("./prices/price[label='Studenten']/value")[0].text.replace(",", "."))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
preis = -1
|
preis = -1
|
||||||
|
preise.append(preis)
|
||||||
essen.append(Essen(name, preis, kategorie))
|
essen.append(Essen(name, preis, kategorie))
|
||||||
|
|
||||||
|
translations = translator.translate(essen_transl)
|
||||||
|
for i in range(int(len(translations) / 2)):
|
||||||
|
essen_eng.append(Essen(translations[i * 2 + 1].text, preise[i], translations[i * 2].text))
|
||||||
|
|
||||||
|
|
||||||
def get_bot_id() -> int:
|
def get_bot_id() -> int:
|
||||||
global botID
|
global botID
|
||||||
|
@ -304,6 +321,8 @@ signal.signal(signal.SIGINT, shutdown)
|
||||||
|
|
||||||
locale.setlocale(locale.LC_TIME, 'de_DE.UTF-8')
|
locale.setlocale(locale.LC_TIME, 'de_DE.UTF-8')
|
||||||
|
|
||||||
|
translator = Translator()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(config_filename, 'r') as config_file:
|
with open(config_filename, 'r') as config_file:
|
||||||
config = json.load(config_file)
|
config = json.load(config_file)
|
||||||
|
|
Loading…
Reference in a new issue