Add translation
This commit is contained in:
parent
d5aac011a1
commit
f2af0b89f0
1 changed files with 67 additions and 0 deletions
|
@ -74,6 +74,12 @@ class MastodonEmailBridge:
|
|||
loader=FileSystemLoader(templates_folder, followlinks=True),
|
||||
autoescape=select_autoescape()
|
||||
)
|
||||
self.translate_session = requests.Session()
|
||||
headers = [
|
||||
'accept: application/json',
|
||||
'Content-Type: application/x-www-form-urlencoded',
|
||||
]
|
||||
self.translate_session.headers.update(headers)
|
||||
self.session = requests.Session()
|
||||
self.session.headers.update({'Authorization': f"Bearer {self.config['token']}"})
|
||||
count=1
|
||||
|
@ -146,6 +152,7 @@ class MastodonEmailBridge:
|
|||
data['reblog']['in_reply_to_id']
|
||||
)
|
||||
data['meb_reply_to'].append(self.get_post(data['reblog']['in_reply_to_id']))
|
||||
data = self._translate_data(data)
|
||||
if int(data['id']) not in self.sent_items:
|
||||
self.send_mail(data)
|
||||
else:
|
||||
|
@ -157,6 +164,26 @@ class MastodonEmailBridge:
|
|||
next_url = slink[0].replace('<', '').replace('>', '')
|
||||
return next_url
|
||||
|
||||
def _translate_data(self, data):
|
||||
if self.config['libretranslate_url'] is None or self.config['libretranslate_url'] = '':
|
||||
return data
|
||||
new_data = data
|
||||
if 'language' not in data:
|
||||
source_language = 'auto'
|
||||
else:
|
||||
source_language = data['language']
|
||||
if source_language in self.config['not_translate_language']:
|
||||
return data
|
||||
fields_to_translate = [
|
||||
'subject',
|
||||
'spoiler',
|
||||
'content'
|
||||
]
|
||||
for field in fields_to_translate:
|
||||
if field in data:
|
||||
new_data[f"translated_{field}"] = self._translate(data[field], source_language=source_language)
|
||||
return new_data
|
||||
|
||||
def get_post(self, post_id):
|
||||
'''Get a single post'''
|
||||
post = {}
|
||||
|
@ -230,6 +257,23 @@ class MastodonEmailBridge:
|
|||
result = template.render(data=data)
|
||||
return result
|
||||
|
||||
def _translate(self, text, source_language='auto', destination_language=None):
|
||||
if destination_language is None:
|
||||
destination_language = self.config['destination_language']
|
||||
data = {
|
||||
"q": text,
|
||||
"source": source_language,
|
||||
"target": destination_language,
|
||||
"api_key": self.config['libretranslate_token'],
|
||||
"format": "text",
|
||||
}
|
||||
response = self.translate_session.post(
|
||||
url=f"{self.config['libretranslate_url']}/",
|
||||
data=data,
|
||||
)
|
||||
translation = response.json()['translatedText']
|
||||
return translation
|
||||
|
||||
def _init_log(self):
|
||||
''' Initialize log object '''
|
||||
self._log = logging.getLogger("mastodon_email_bridge")
|
||||
|
@ -344,6 +388,29 @@ class MastodonEmailBridge:
|
|||
'-T',
|
||||
help='Folder with the templates to generate HTML and text files to be sent.'
|
||||
)
|
||||
@click.option(
|
||||
'--libretranslate-url',
|
||||
'-U',
|
||||
help='LibreTranslate instance URL to use like: https://translate.example.org/translate'
|
||||
)
|
||||
@click.option(
|
||||
'--libretranslate-token',
|
||||
'-o',
|
||||
default='',
|
||||
help='LibreTranslate token to use'
|
||||
)
|
||||
@click.option(
|
||||
'--not-translate-language',
|
||||
'-n',
|
||||
multiple=True,
|
||||
help='Languages that you don\'t want to translate with LibreTranslate'
|
||||
)
|
||||
@click.option(
|
||||
'--destination-language',
|
||||
'-D',
|
||||
default='en',
|
||||
help='Language destination for the translations'
|
||||
)
|
||||
|
||||
@click.option('--log-file', '-l', help="File to store all debug messages.")
|
||||
# @click.option("--dummy","-n", is_flag=True,
|
||||
|
|
Loading…
Reference in a new issue