mirror of https://gitlab.com/bashrc2/epicyon
Snake case
parent
2fde07c202
commit
c10f83bb21
28
jami.py
28
jami.py
|
@ -42,26 +42,26 @@ def get_jami_address(actor_json: {}) -> str:
|
||||||
def set_jami_address(actor_json: {}, jami_address: str) -> None:
|
def set_jami_address(actor_json: {}, jami_address: str) -> None:
|
||||||
"""Sets an jami address for the given actor
|
"""Sets an jami address for the given actor
|
||||||
"""
|
"""
|
||||||
notJamiAddress = False
|
not_jami_address = False
|
||||||
|
|
||||||
if len(jami_address) < 2:
|
if len(jami_address) < 2:
|
||||||
notJamiAddress = True
|
not_jami_address = True
|
||||||
if '"' in jami_address:
|
if '"' in jami_address:
|
||||||
notJamiAddress = True
|
not_jami_address = True
|
||||||
if ' ' in jami_address:
|
if ' ' in jami_address:
|
||||||
notJamiAddress = True
|
not_jami_address = True
|
||||||
if '.' in jami_address:
|
if '.' in jami_address:
|
||||||
notJamiAddress = True
|
not_jami_address = True
|
||||||
if ',' in jami_address:
|
if ',' in jami_address:
|
||||||
notJamiAddress = True
|
not_jami_address = True
|
||||||
if '<' in jami_address:
|
if '<' in jami_address:
|
||||||
notJamiAddress = True
|
not_jami_address = True
|
||||||
|
|
||||||
if not actor_json.get('attachment'):
|
if not actor_json.get('attachment'):
|
||||||
actor_json['attachment'] = []
|
actor_json['attachment'] = []
|
||||||
|
|
||||||
# remove any existing value
|
# remove any existing value
|
||||||
propertyFound = None
|
property_found = None
|
||||||
for property_value in actor_json['attachment']:
|
for property_value in actor_json['attachment']:
|
||||||
if not property_value.get('name'):
|
if not property_value.get('name'):
|
||||||
continue
|
continue
|
||||||
|
@ -69,11 +69,11 @@ def set_jami_address(actor_json: {}, jami_address: str) -> None:
|
||||||
continue
|
continue
|
||||||
if not property_value['name'].lower().startswith('jami'):
|
if not property_value['name'].lower().startswith('jami'):
|
||||||
continue
|
continue
|
||||||
propertyFound = property_value
|
property_found = property_value
|
||||||
break
|
break
|
||||||
if propertyFound:
|
if property_found:
|
||||||
actor_json['attachment'].remove(propertyFound)
|
actor_json['attachment'].remove(property_found)
|
||||||
if notJamiAddress:
|
if not_jami_address:
|
||||||
return
|
return
|
||||||
|
|
||||||
for property_value in actor_json['attachment']:
|
for property_value in actor_json['attachment']:
|
||||||
|
@ -88,9 +88,9 @@ def set_jami_address(actor_json: {}, jami_address: str) -> None:
|
||||||
property_value['value'] = jami_address
|
property_value['value'] = jami_address
|
||||||
return
|
return
|
||||||
|
|
||||||
newJamiAddress = {
|
new_jami_address = {
|
||||||
"name": "Jami",
|
"name": "Jami",
|
||||||
"type": "PropertyValue",
|
"type": "PropertyValue",
|
||||||
"value": jami_address
|
"value": jami_address
|
||||||
}
|
}
|
||||||
actor_json['attachment'].append(newJamiAddress)
|
actor_json['attachment'].append(new_jami_address)
|
||||||
|
|
169
languages.py
169
languages.py
|
@ -24,37 +24,37 @@ def get_actor_languages(actor_json: {}) -> str:
|
||||||
lang_list = get_actor_languages_list(actor_json)
|
lang_list = get_actor_languages_list(actor_json)
|
||||||
if not lang_list:
|
if not lang_list:
|
||||||
return ''
|
return ''
|
||||||
languagesStr = ''
|
languages_str = ''
|
||||||
for lang in lang_list:
|
for lang in lang_list:
|
||||||
if languagesStr:
|
if languages_str:
|
||||||
languagesStr += ' / ' + lang
|
languages_str += ' / ' + lang
|
||||||
else:
|
else:
|
||||||
languagesStr = lang
|
languages_str = lang
|
||||||
return languagesStr
|
return languages_str
|
||||||
|
|
||||||
|
|
||||||
def set_actor_languages(base_dir: str, actor_json: {},
|
def set_actor_languages(base_dir: str, actor_json: {},
|
||||||
languagesStr: str) -> None:
|
languages_str: str) -> None:
|
||||||
"""Sets the languages used by the given actor
|
"""Sets the languages used by the given actor
|
||||||
"""
|
"""
|
||||||
separator = ','
|
separator = ','
|
||||||
if '/' in languagesStr:
|
if '/' in languages_str:
|
||||||
separator = '/'
|
separator = '/'
|
||||||
elif ',' in languagesStr:
|
elif ',' in languages_str:
|
||||||
separator = ','
|
separator = ','
|
||||||
elif ';' in languagesStr:
|
elif ';' in languages_str:
|
||||||
separator = ';'
|
separator = ';'
|
||||||
elif '+' in languagesStr:
|
elif '+' in languages_str:
|
||||||
separator = '+'
|
separator = '+'
|
||||||
elif ' ' in languagesStr:
|
elif ' ' in languages_str:
|
||||||
separator = ' '
|
separator = ' '
|
||||||
lang_list = languagesStr.lower().split(separator)
|
lang_list = languages_str.lower().split(separator)
|
||||||
lang_list2 = ''
|
lang_list2 = ''
|
||||||
for lang in lang_list:
|
for lang in lang_list:
|
||||||
lang = lang.strip()
|
lang = lang.strip()
|
||||||
if base_dir:
|
if base_dir:
|
||||||
languageFilename = base_dir + '/translations/' + lang + '.json'
|
language_filename = base_dir + '/translations/' + lang + '.json'
|
||||||
if os.path.isfile(languageFilename):
|
if os.path.isfile(language_filename):
|
||||||
if lang_list2:
|
if lang_list2:
|
||||||
lang_list2 += ', ' + lang.strip()
|
lang_list2 += ', ' + lang.strip()
|
||||||
else:
|
else:
|
||||||
|
@ -66,7 +66,7 @@ def set_actor_languages(base_dir: str, actor_json: {},
|
||||||
lang_list2 += lang.strip()
|
lang_list2 += lang.strip()
|
||||||
|
|
||||||
# remove any existing value
|
# remove any existing value
|
||||||
propertyFound = None
|
property_found = None
|
||||||
for property_value in actor_json['attachment']:
|
for property_value in actor_json['attachment']:
|
||||||
if not property_value.get('name'):
|
if not property_value.get('name'):
|
||||||
continue
|
continue
|
||||||
|
@ -74,20 +74,20 @@ def set_actor_languages(base_dir: str, actor_json: {},
|
||||||
continue
|
continue
|
||||||
if not property_value['name'].lower().startswith('languages'):
|
if not property_value['name'].lower().startswith('languages'):
|
||||||
continue
|
continue
|
||||||
propertyFound = property_value
|
property_found = property_value
|
||||||
break
|
break
|
||||||
if propertyFound:
|
if property_found:
|
||||||
actor_json['attachment'].remove(propertyFound)
|
actor_json['attachment'].remove(property_found)
|
||||||
|
|
||||||
if not lang_list2:
|
if not lang_list2:
|
||||||
return
|
return
|
||||||
|
|
||||||
newLanguages = {
|
new_languages = {
|
||||||
"name": "Languages",
|
"name": "Languages",
|
||||||
"type": "PropertyValue",
|
"type": "PropertyValue",
|
||||||
"value": lang_list2
|
"value": lang_list2
|
||||||
}
|
}
|
||||||
actor_json['attachment'].append(newLanguages)
|
actor_json['attachment'].append(new_languages)
|
||||||
|
|
||||||
|
|
||||||
def understood_post_language(base_dir: str, nickname: str, domain: str,
|
def understood_post_language(base_dir: str, nickname: str, domain: str,
|
||||||
|
@ -97,41 +97,42 @@ def understood_post_language(base_dir: str, nickname: str, domain: str,
|
||||||
"""Returns true if the post is written in a language
|
"""Returns true if the post is written in a language
|
||||||
understood by this account
|
understood by this account
|
||||||
"""
|
"""
|
||||||
msgObject = message_json
|
msg_object = message_json
|
||||||
if has_object_dict(message_json):
|
if has_object_dict(message_json):
|
||||||
msgObject = message_json['object']
|
msg_object = message_json['object']
|
||||||
if not msgObject.get('contentMap'):
|
if not msg_object.get('contentMap'):
|
||||||
return True
|
return True
|
||||||
if not isinstance(msgObject['contentMap'], dict):
|
if not isinstance(msg_object['contentMap'], dict):
|
||||||
return True
|
return True
|
||||||
if msgObject['contentMap'].get(system_language):
|
if msg_object['contentMap'].get(system_language):
|
||||||
return True
|
return True
|
||||||
personUrl = local_actor_url(http_prefix, nickname, domain_full)
|
person_url = local_actor_url(http_prefix, nickname, domain_full)
|
||||||
actor_json = \
|
actor_json = \
|
||||||
get_person_from_cache(base_dir, personUrl, person_cache, False)
|
get_person_from_cache(base_dir, person_url, person_cache, False)
|
||||||
if not actor_json:
|
if not actor_json:
|
||||||
print('WARN: unable to load actor to check languages ' + personUrl)
|
print('WARN: unable to load actor to check languages ' + person_url)
|
||||||
return False
|
return False
|
||||||
languages_understood = get_actor_languages_list(actor_json)
|
languages_understood = get_actor_languages_list(actor_json)
|
||||||
if not languages_understood:
|
if not languages_understood:
|
||||||
return True
|
return True
|
||||||
for lang in languages_understood:
|
for lang in languages_understood:
|
||||||
if msgObject['contentMap'].get(lang):
|
if msg_object['contentMap'].get(lang):
|
||||||
return True
|
return True
|
||||||
# is the language for this post supported by libretranslate?
|
# is the language for this post supported by libretranslate?
|
||||||
libretranslateUrl = get_config_param(base_dir, "libretranslateUrl")
|
libretranslate_url = get_config_param(base_dir, "libretranslate_url")
|
||||||
if libretranslateUrl:
|
if libretranslate_url:
|
||||||
libretranslateApiKey = \
|
libretranslate_api_key = \
|
||||||
get_config_param(base_dir, "libretranslateApiKey")
|
get_config_param(base_dir, "libretranslateApiKey")
|
||||||
lang_list = \
|
lang_list = \
|
||||||
libretranslate_languages(libretranslateUrl, libretranslateApiKey)
|
libretranslate_languages(libretranslate_url,
|
||||||
|
libretranslate_api_key)
|
||||||
for lang in lang_list:
|
for lang in lang_list:
|
||||||
if msgObject['contentMap'].get(lang):
|
if msg_object['contentMap'].get(lang):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def libretranslate_languages(url: str, apiKey: str = None) -> []:
|
def libretranslate_languages(url: str, api_key: str = None) -> []:
|
||||||
"""Returns a list of supported languages
|
"""Returns a list of supported languages
|
||||||
"""
|
"""
|
||||||
if not url:
|
if not url:
|
||||||
|
@ -144,12 +145,12 @@ def libretranslate_languages(url: str, apiKey: str = None) -> []:
|
||||||
|
|
||||||
params = dict()
|
params = dict()
|
||||||
|
|
||||||
if apiKey:
|
if api_key:
|
||||||
params["api_key"] = apiKey
|
params["api_key"] = api_key
|
||||||
|
|
||||||
urlParams = parse.urlencode(params)
|
url_params = parse.urlencode(params)
|
||||||
|
|
||||||
req = request.Request(url, data=urlParams.encode())
|
req = request.Request(url, data=url_params.encode())
|
||||||
|
|
||||||
response = request.urlopen(req)
|
response = request.urlopen(req)
|
||||||
|
|
||||||
|
@ -167,10 +168,10 @@ def libretranslate_languages(url: str, apiKey: str = None) -> []:
|
||||||
continue
|
continue
|
||||||
if not lang.get('code'):
|
if not lang.get('code'):
|
||||||
continue
|
continue
|
||||||
langCode = lang['code']
|
lang_code = lang['code']
|
||||||
if len(langCode) != 2:
|
if len(lang_code) != 2:
|
||||||
continue
|
continue
|
||||||
lang_list.append(langCode)
|
lang_list.append(lang_code)
|
||||||
lang_list.sort()
|
lang_list.sort()
|
||||||
return lang_list
|
return lang_list
|
||||||
|
|
||||||
|
@ -193,37 +194,37 @@ def get_links_from_content(content: str) -> {}:
|
||||||
if '://' in url and '.' in url and \
|
if '://' in url and '.' in url and \
|
||||||
'>' in subsection:
|
'>' in subsection:
|
||||||
if url not in links:
|
if url not in links:
|
||||||
linkText = subsection.split('>')[1]
|
link_text = subsection.split('>')[1]
|
||||||
if '<' in linkText:
|
if '<' in link_text:
|
||||||
linkText = linkText.split('<')[0]
|
link_text = link_text.split('<')[0]
|
||||||
links[linkText] = url
|
links[link_text] = url
|
||||||
return links
|
return links
|
||||||
|
|
||||||
|
|
||||||
def add_links_to_content(content: str, links: {}) -> str:
|
def add_links_to_content(content: str, links: {}) -> str:
|
||||||
"""Adds links back into plain text
|
"""Adds links back into plain text
|
||||||
"""
|
"""
|
||||||
for linkText, url in links.items():
|
for link_text, url in links.items():
|
||||||
urlDesc = url
|
url_desc = url
|
||||||
if linkText.startswith('@') and linkText in content:
|
if link_text.startswith('@') and link_text in content:
|
||||||
content = \
|
content = \
|
||||||
content.replace(linkText,
|
content.replace(link_text,
|
||||||
'<a href="' + url +
|
'<a href="' + url +
|
||||||
'" rel="nofollow noopener ' +
|
'" rel="nofollow noopener ' +
|
||||||
'noreferrer" target="_blank">' +
|
'noreferrer" target="_blank">' +
|
||||||
linkText + '</a>')
|
link_text + '</a>')
|
||||||
else:
|
else:
|
||||||
if len(urlDesc) > 40:
|
if len(url_desc) > 40:
|
||||||
urlDesc = urlDesc[:40]
|
url_desc = url_desc[:40]
|
||||||
content += \
|
content += \
|
||||||
'<p><a href="' + url + \
|
'<p><a href="' + url + \
|
||||||
'" rel="nofollow noopener noreferrer" target="_blank">' + \
|
'" rel="nofollow noopener noreferrer" target="_blank">' + \
|
||||||
urlDesc + '</a></p>'
|
url_desc + '</a></p>'
|
||||||
return content
|
return content
|
||||||
|
|
||||||
|
|
||||||
def libretranslate(url: str, text: str,
|
def libretranslate(url: str, text: str,
|
||||||
source: str, target: str, apiKey: str = None) -> str:
|
source: str, target: str, api_key: str = None) -> str:
|
||||||
"""Translate string using libretranslate
|
"""Translate string using libretranslate
|
||||||
"""
|
"""
|
||||||
if not url:
|
if not url:
|
||||||
|
@ -235,7 +236,7 @@ def libretranslate(url: str, text: str,
|
||||||
else:
|
else:
|
||||||
url += "translate"
|
url += "translate"
|
||||||
|
|
||||||
originalText = text
|
original_text = text
|
||||||
|
|
||||||
# get any links from the text
|
# get any links from the text
|
||||||
links = get_links_from_content(text)
|
links = get_links_from_content(text)
|
||||||
|
@ -244,36 +245,36 @@ def libretranslate(url: str, text: str,
|
||||||
text = remove_html(text)
|
text = remove_html(text)
|
||||||
|
|
||||||
# remove any links from plain text version of the content
|
# remove any links from plain text version of the content
|
||||||
for _, url in links.items():
|
for _, url2 in links.items():
|
||||||
text = text.replace(url, '')
|
text = text.replace(url2, '')
|
||||||
|
|
||||||
ltParams = {
|
lt_params = {
|
||||||
"q": text,
|
"q": text,
|
||||||
"source": source,
|
"source": source,
|
||||||
"target": target
|
"target": target
|
||||||
}
|
}
|
||||||
|
|
||||||
if apiKey:
|
if api_key:
|
||||||
ltParams["api_key"] = apiKey
|
lt_params["api_key"] = api_key
|
||||||
|
|
||||||
urlParams = parse.urlencode(ltParams)
|
url_params = parse.urlencode(lt_params)
|
||||||
|
|
||||||
req = request.Request(url, data=urlParams.encode())
|
req = request.Request(url, data=url_params.encode())
|
||||||
try:
|
try:
|
||||||
response = request.urlopen(req)
|
response = request.urlopen(req)
|
||||||
except BaseException:
|
except BaseException:
|
||||||
print('EX: Unable to translate: ' + text)
|
print('EX: Unable to translate: ' + text)
|
||||||
return originalText
|
return original_text
|
||||||
|
|
||||||
response_str = response.read().decode()
|
response_str = response.read().decode()
|
||||||
|
|
||||||
translatedText = \
|
translated_text = \
|
||||||
'<p>' + json.loads(response_str)['translatedText'] + '</p>'
|
'<p>' + json.loads(response_str)['translatedText'] + '</p>'
|
||||||
|
|
||||||
# append links form the original text
|
# append links form the original text
|
||||||
if links:
|
if links:
|
||||||
translatedText = add_links_to_content(translatedText, links)
|
translated_text = add_links_to_content(translated_text, links)
|
||||||
return translatedText
|
return translated_text
|
||||||
|
|
||||||
|
|
||||||
def auto_translate_post(base_dir: str, post_json_object: {},
|
def auto_translate_post(base_dir: str, post_json_object: {},
|
||||||
|
@ -282,31 +283,31 @@ def auto_translate_post(base_dir: str, post_json_object: {},
|
||||||
"""
|
"""
|
||||||
if not has_object_dict(post_json_object):
|
if not has_object_dict(post_json_object):
|
||||||
return ''
|
return ''
|
||||||
msgObject = post_json_object['object']
|
msg_object = post_json_object['object']
|
||||||
if not msgObject.get('contentMap'):
|
if not msg_object.get('contentMap'):
|
||||||
return ''
|
return ''
|
||||||
if not isinstance(msgObject['contentMap'], dict):
|
if not isinstance(msg_object['contentMap'], dict):
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
# is the language for this post supported by libretranslate?
|
# is the language for this post supported by libretranslate?
|
||||||
libretranslateUrl = get_config_param(base_dir, "libretranslateUrl")
|
libretranslate_url = get_config_param(base_dir, "libretranslateUrl")
|
||||||
if not libretranslateUrl:
|
if not libretranslate_url:
|
||||||
return ''
|
return ''
|
||||||
libretranslateApiKey = get_config_param(base_dir, "libretranslateApiKey")
|
libretranslate_api_key = get_config_param(base_dir, "libretranslateApiKey")
|
||||||
lang_list = \
|
lang_list = \
|
||||||
libretranslate_languages(libretranslateUrl, libretranslateApiKey)
|
libretranslate_languages(libretranslate_url, libretranslate_api_key)
|
||||||
for lang in lang_list:
|
for lang in lang_list:
|
||||||
if msgObject['contentMap'].get(lang):
|
if msg_object['contentMap'].get(lang):
|
||||||
content = msgObject['contentMap'][lang]
|
content = msg_object['contentMap'][lang]
|
||||||
translatedText = \
|
translated_text = \
|
||||||
libretranslate(libretranslateUrl, content,
|
libretranslate(libretranslate_url, content,
|
||||||
lang, system_language,
|
lang, system_language,
|
||||||
libretranslateApiKey)
|
libretranslate_api_key)
|
||||||
if translatedText:
|
if translated_text:
|
||||||
if remove_html(translatedText) == remove_html(content):
|
if remove_html(translated_text) == remove_html(content):
|
||||||
return content
|
return content
|
||||||
translatedText = \
|
translated_text = \
|
||||||
'<p>' + translate['Translated'].upper() + '</p>' + \
|
'<p>' + translate['Translated'].upper() + '</p>' + \
|
||||||
translatedText
|
translated_text
|
||||||
return translatedText
|
return translated_text
|
||||||
return ''
|
return ''
|
||||||
|
|
Loading…
Reference in New Issue