diff --git a/scripts/python/create_response.py b/scripts/python/create_response.py new file mode 100644 index 0000000..ccd5495 --- /dev/null +++ b/scripts/python/create_response.py @@ -0,0 +1,17 @@ +import json +from datetime import datetime, timezone + +def create_respone(translated, from_code, to_code, error): +# Creating a dictionary that matches the structure of your JSON + data = { + "from": from_code, + "to": to_code, + "created_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f%z"), + "response": translated, + "error": error + } + json_string = json.dumps(data, indent=4) + + # Printing the JSON string to the console + print(json_string) + diff --git a/scripts/python/main.py b/scripts/python/main.py new file mode 100644 index 0000000..414da83 --- /dev/null +++ b/scripts/python/main.py @@ -0,0 +1,32 @@ +import sys +from translate import translate +from package_mangement.downlaod_languages import install_language +from package_mangement.check_packages import check_languages_installed +from create_response import create_respone + +def main(): + message = sys.argv[1] + from_lang = sys.argv[2] + to_lang = sys.argv[3] + + # Check if languages are installed + is_installed = check_languages_installed(from_lang, to_lang) + + if not is_installed: + # Attempt to install the languages + installed = install_language(from_lang, to_lang) + if installed != 0: + # Installation failed + error = "Error: Installation failed" + create_respone("None", from_lang, to_lang, error) + return # Exit the function early + + # Languages are installed or installation succeeded + translated = translate(message, from_lang, to_lang) + if translated: + create_respone(translated, from_lang, to_lang, error = "None") + else: + create_respone("None", from_lang, to_lang, error = "Could not translate it.") + +if __name__ == "__main__": + main() diff --git a/scripts/python/package_mangement/check_packages.py b/scripts/python/package_mangement/check_packages.py new file mode 100644 index 0000000..6ad9515 --- /dev/null +++ b/scripts/python/package_mangement/check_packages.py @@ -0,0 +1,28 @@ +import csv + +def check_languages_installed(from_code, to_code): + """ + Check if the language codes match the given criteria in a predefined .csv file. + + Parameters: + - from_code (str): The source language code to check. + - to_code (str): The target language code to check. + + Outputs: + - Prints True if a matching row is found, else prints False. + """ + try: + # Assuming the CSV file is named 'languages.csv' and located in the same directory + with open('installed_packages_info.csv', mode='r') as csv_file: + csv_reader = csv.reader(csv_file) + for row in csv_reader: + # Check if the current row matches the criteria + if len(row) >= 2 and row[0] == from_code and row[1] == to_code: + return True + # If the loop completes without finding a match, print False + return False + + except FileNotFoundError: + return 1 + except Exception: + return 1 diff --git a/scripts/python/package_mangement/downlaod_languages.py b/scripts/python/package_mangement/downlaod_languages.py new file mode 100644 index 0000000..93a8bea --- /dev/null +++ b/scripts/python/package_mangement/downlaod_languages.py @@ -0,0 +1,18 @@ +import argostranslate.translate +import argostranslate.package + + +def install_language(from_code, to_code): + try: + argostranslate.package.update_package_index() + available_packages = argostranslate.package.get_available_packages() + available_package = list( + filter( + lambda x: x.from_code == from_code and x.to_code == to_code, available_packages + ) + )[0] + download_path = available_package.download() + argostranslate.package.install_from_path(download_path) + return 0 + except Exception: + return 1 diff --git a/scripts/python/translate.py b/scripts/python/translate.py new file mode 100644 index 0000000..526234b --- /dev/null +++ b/scripts/python/translate.py @@ -0,0 +1,8 @@ +import argostranslate.package +import argostranslate.translate + +def translate(message, from_lang, to_lang): + translation = argostranslate.translate.translate(message, from_lang, to_lang) + return translation + +