Skip to content

Commit

Permalink
fix: [Language] fix language source
Browse files Browse the repository at this point in the history
  • Loading branch information
Terrtia committed Jan 16, 2024
1 parent 1c46bb4 commit f586baa
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 41 deletions.
10 changes: 7 additions & 3 deletions bin/lib/Language.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ def detect(self, content):
return language

def translate(self, content, source=None, target="en"): # TODO source target
if target not in LIST_LANGUAGES:
return None
translation = None
if content:
if not source:
Expand All @@ -407,15 +409,17 @@ def translate(self, content, source=None, target="en"): # TODO source target
return translation


LIST_LANGUAGES = []
LIST_LANGUAGES = {}
def get_translation_languages():
global LIST_LANGUAGES
if not LIST_LANGUAGES:
try:
LIST_LANGUAGES = LanguageTranslator().languages()
LIST_LANGUAGES = {}
for lang in LanguageTranslator().languages():
LIST_LANGUAGES[lang['iso']] = lang['language']
except Exception as e:
print(e)
LIST_LANGUAGES = []
LIST_LANGUAGES = {}
return LIST_LANGUAGES


Expand Down
3 changes: 3 additions & 0 deletions bin/lib/chats_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from lib.objects import Messages
from lib.objects import UsersAccount
from lib.objects import Usernames
from lib import Language

config_loader = ConfigLoader()
r_db = config_loader.get_db_conn("Kvrocks_DB")
Expand Down Expand Up @@ -341,6 +342,8 @@ def api_get_chat(chat_id, chat_instance_uuid, translation_target=None, nb=-1, pa
if meta['subchannels']:
meta['subchannels'] = get_subchannels_meta_from_global_id(meta['subchannels'])
else:
if translation_target not in Language.LIST_LANGUAGES:
translation_target = None
meta['messages'], meta['pagination'], meta['tags_messages'] = chat.get_messages(translation_target=translation_target, nb=nb, page=page)
return meta, 200

Expand Down
37 changes: 0 additions & 37 deletions bin/lib/objects/Items.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,43 +629,6 @@ def get_item_metadata(item_id, item_content=None):
def get_item_content(item_id):
return item_basic.get_item_content(item_id)

def get_item_content_html2text(item_id, item_content=None, ignore_links=False):
if not item_content:
item_content = get_item_content(item_id)
h = html2text.HTML2Text()
h.ignore_links = ignore_links
h.ignore_images = ignore_links
return h.handle(item_content)

def remove_all_urls_from_content(item_id, item_content=None):
if not item_content:
item_content = get_item_content(item_id)
regex = r'\b(?:http://|https://)?(?:[a-zA-Z\d-]{,63}(?:\.[a-zA-Z\d-]{,63})+)(?:\:[0-9]+)*(?:/(?:$|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*\b'
url_regex = re.compile(regex)
urls = url_regex.findall(item_content)
urls = sorted(urls, key=len, reverse=True)
for url in urls:
item_content = item_content.replace(url, '')

regex_pgp_public_blocs = r'-----BEGIN PGP PUBLIC KEY BLOCK-----[\s\S]+?-----END PGP PUBLIC KEY BLOCK-----'
regex_pgp_signature = r'-----BEGIN PGP SIGNATURE-----[\s\S]+?-----END PGP SIGNATURE-----'
regex_pgp_message = r'-----BEGIN PGP MESSAGE-----[\s\S]+?-----END PGP MESSAGE-----'
re.compile(regex_pgp_public_blocs)
re.compile(regex_pgp_signature)
re.compile(regex_pgp_message)

res = re.findall(regex_pgp_public_blocs, item_content)
for it in res:
item_content = item_content.replace(it, '')
res = re.findall(regex_pgp_signature, item_content)
for it in res:
item_content = item_content.replace(it, '')
res = re.findall(regex_pgp_message, item_content)
for it in res:
item_content = item_content.replace(it, '')

return item_content


# API
# def get_item(request_dict):
Expand Down
6 changes: 6 additions & 0 deletions var/www/blueprints/chats_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def chats_explorer_chat():
chat_id = request.args.get('id')
instance_uuid = request.args.get('uuid')
target = request.args.get('target')
if target == "Don't Translate":
target = None
nb_messages = request.args.get('nb')
page = request.args.get('page')
chat = chats_viewer.api_get_chat(chat_id, instance_uuid, translation_target=target, nb=nb_messages, page=page)
Expand Down Expand Up @@ -111,6 +113,8 @@ def objects_subchannel_messages():
subchannel_id = request.args.get('id')
instance_uuid = request.args.get('uuid')
target = request.args.get('target')
if target == "Don't Translate":
target = None
nb_messages = request.args.get('nb')
page = request.args.get('page')
subchannel = chats_viewer.api_get_subchannel(subchannel_id, instance_uuid, translation_target=target, nb=nb_messages, page=page)
Expand All @@ -128,6 +132,8 @@ def objects_thread_messages():
thread_id = request.args.get('id')
instance_uuid = request.args.get('uuid')
target = request.args.get('target')
if target == "Don't Translate":
target = None
nb_messages = request.args.get('nb')
page = request.args.get('page')
thread = chats_viewer.api_get_thread(thread_id, instance_uuid, translation_target=target, nb=nb_messages, page=page)
Expand Down
2 changes: 1 addition & 1 deletion var/www/templates/chats_explorer/block_translation.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<option selected value="{{ translation_target }}">{{ translation_target }}</option>
{% endif %}
{% for language in translation_languages %}
<option value="{{ language['iso'] }}">{{ language['language'] }}</option>
<option value="{{ language }}">{{ translation_languages[language] }}</option>
{% endfor %}
</select>
</div>
Expand Down

0 comments on commit f586baa

Please sign in to comment.