Browse Source

Added support for all Deepl-supported languages

master
Illia Chub 2 months ago
parent
commit
796889c077
No known key found for this signature in database GPG Key ID: 5D3581534C02467A
  1. 1
      constants.py
  2. 40
      main.py

1
constants.py

@ -1,3 +1,4 @@
TRANSLATE_API_ENDPOINT_URL = "https://api-free.deepl.com/v2/translate"
USAGE_API_ENDPOINT_URL = "https://api-free.deepl.com/v2/usage"
LANGUAGES_API_ENDPOINT_URL = "https://api-free.deepl.com/v2/languages"
API_ACCESS_KEY = ""

40
main.py

@ -7,7 +7,19 @@ import constants
def main():
# Impl
print("[INFO][PREFLIGHT] Initializing translator...")
print("[INFO][PREFLIGHT] Initializing translator...", end="")
availableLanguages = list()
params = {
"auth_key": constants.API_ACCESS_KEY,
"type": "target"
}
availableLanguagesRequest = requests.get(constants.LANGUAGES_API_ENDPOINT_URL, data=params)
responseInJSON = json.loads(str(availableLanguagesRequest.text))
for arrayMember in responseInJSON:
availableLanguages.append(arrayMember["language"])
print("done")
argumentParser = argparse.ArgumentParser(description="Translate text using Deepl service")
@ -23,8 +35,30 @@ def main():
type=str
)
argumentParser.add_argument(
"--target_lang",
help="Language to translate text to",
type=str
)
arguments = argumentParser.parse_args()
languageIsAvailable = bool(False)
for language in availableLanguages:
if not arguments.target_lang == language:
continue
elif arguments.target_lang == language:
languageIsAvailable = True
break
if not languageIsAvailable:
print("[ERROR][PREFLIGHT] Language {0} is not supported!".format(arguments.target_lang))
print("[INFO][TERMINATION] Supported languages are:", end=" ")
for language in availableLanguages:
print("{0}".format(language), end=" ")
print("\n")
exit(1)
if not os.path.exists(arguments.file):
print("[ERROR][PREFLIGHT] File {0} not found!".format(arguments.file))
exit(1)
@ -34,7 +68,7 @@ def main():
fileContent = readOnlyFileDescriptor.read()
params = {
"auth_key": constants.API_ACCESS_KEY,
"auth_key": constants.API_ACCESS_KEY
}
print("[INFO][PREFLIGHT] Performing subscription check...", end="")
@ -54,7 +88,7 @@ def main():
params = {
"auth_key": constants.API_ACCESS_KEY,
"text": fileContent,
"target_lang": "EN"
"target_lang": arguments.target_lang
}
translationRequest = requests.get(constants.TRANSLATE_API_ENDPOINT_URL, data=params)

Loading…
Cancel
Save