#!/usr/bin/env python # -*- coding: utf-8 -*- from os import popen as execute_command from json import loads as json_decode from json import dumps as json_encode from re import compile as re_compile from traceback import format_stack as trace_format_stack from urllib.parse import quote_plus as uri_encode # cd ~/.local/lib/python3.9/site-packages/libretranslate # python3 main.py --host 192.168.0.173 --port 5000 class Com: api_url = "http://192.168.0.173:5000/translate" re_uri_reencode = re_compile(r'[\'\%]') re_uri_redecode = re_compile(r'\%2[57]') re_duplicated_semicolon = re_compile(r'\'\%27') re_variables = re_compile(r'[\'"`]?\{([a-z0-9A-Z_]+)\}[\'"`]?') def uri_reencode(self, matches): return { "%" : "%25", "'" : "`" }[matches.group(0)] def url_redecode(self, matches): return { "%25" : "%", "`" : "'" }[matches.group(0)] name_key = {} key_name = "Miguel" key_number = 9999 variables_keys_names = ("code", "id", "i") # def __init__(self): # self.re_keys = re_compile(r'' + self.name_key[_to] + r'|' + str(self.key_number)) def load_api(self, data): uri_data = "" response = None for key in ("q", "source", "target", "format", "api_key"): if key in data: uri_data += ("&" if uri_data else "") + key + "=" + (uri_encode(data[key]) if key in ("q",) else data[key]) # command = "curl -s -H 'accept: application/json' -H 'Context-Type: application/json' -d '" + json_encode(data) + "' -X POST " + self.api_url command = "curl -s -H 'accept: application/json' -H 'Context-Type: application/x-www-form-urlencoded' -d '" + uri_data + "' -X POST " + self.api_url print(command) try: response = json_decode(execute_command(command).read()) except: trace_format_stack if "translatedText" in response: response = response["translatedText"] elif "error": print(response) response = None return response def reformating(self, matches): string = self.variables[self.i]["string"] self.i += 1 return string def translate(self, text, _from, _to): self.variables = [] self.i = 0 def set_variables(matches): key = matches.group(1) self.variables += [{ "string" : matches.group(0), "key" : key, "i" : len(self.variables) }] return str(self.key_number if key in self.variables_keys_names else self.key_name) print("Verificar que exista clave de nombre en el idioma final.") if _to not in self.name_key: self.name_key[_to] = self.load_api({ "q" : self.key_name, "source" : _from, "target" : _to, "format" : "text", "api_key" : "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" }) print("Clave de nombre: " + self.name_key[_to]) print("Fase de codificar las variables.") text = self.re_variables.sub(set_variables, text) print(text) print("Traduciendo el texto.") translated = self.load_api({ "q" : text, "source" : _from, "target" : _to, "format" : "text", "api_key" : "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" }) print("Texto crudo: " + translated) print("Reformateando el texto.") print(r'' + self.name_key[_to] + r'|' + str(self.key_number)) translated = re_compile(r'' + self.name_key[_to] + r'|' + str(self.key_number)).sub(self.reformating, translated) print("Texto reformateado: " + translated) return translated com = Com() com.translate("Hubo un error con código '{code}' en 'LibreTranslatePlus' al intentar eliminar el hilo de procesos '{i}'. {list}", "es", "ja")