From f1118ffe5e3e2467cbe58d9ac7f9b8b66847fb88 Mon Sep 17 00:00:00 2001 From: Illia Chub Date: Thu, 4 Nov 2021 15:13:41 +0200 Subject: [PATCH] Initial commit --- .gitignore | 1 + constants.py | 3 ++ intro.md | 13 +++++++++ intro_en.md | 13 +++++++++ main.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 ++ setup.py | 0 source.txt | 1 + translation.txt | 1 + 9 files changed, 106 insertions(+) create mode 100644 .gitignore create mode 100644 constants.py create mode 100644 intro.md create mode 100644 intro_en.md create mode 100644 main.py create mode 100644 requirements.txt create mode 100644 setup.py create mode 100644 source.txt create mode 100644 translation.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba0430d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ \ No newline at end of file diff --git a/constants.py b/constants.py new file mode 100644 index 0000000..5e8fbdf --- /dev/null +++ b/constants.py @@ -0,0 +1,3 @@ +TRANSLATE_API_ENDPOINT_URL = "https://api-free.deepl.com/v2/translate" +USAGE_API_ENDPOINT_URL = "https://api-free.deepl.com/v2/usage" +API_ACCESS_KEY = "" diff --git a/intro.md b/intro.md new file mode 100644 index 0000000..c414f7f --- /dev/null +++ b/intro.md @@ -0,0 +1,13 @@ +# πŸ‘‹ Π’Π²Π΅Π΄Π΅Π½ΠΈΠ΅ + +[SelfPrivacy](https://selfprivacy.org) β€” мобильноС [ΠΏΡ€ΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅](https://f-droid.org/en/packages/pro.kherel.selfprivacy/) Ρ€Π°Π·Π²ΠΎΡ€Π°Ρ‡ΠΈΠ²Π°Π΅Ρ‚ сСрвСр с Π³ΠΎΡ‚ΠΎΠ²Ρ‹ΠΌΠΈ ΠΊ использованию сСрвисами: +- ЭлСктронная ΠΏΠΎΡ‡Ρ‚Π° +- ΠŸΠ°Ρ€ΠΎΠ»ΡŒΠ½Ρ‹ΠΉ ΠΌΠ΅Π½Π΅Π΄ΠΆΠ΅Ρ€ +- ΠœΠ΅ΡΡΠ΅Π½Π΄ΠΆΠ΅Ρ€ +- VPN +- Π€Π°ΠΉΠ»ΠΎΠ²ΠΎΠ΅ Ρ…Ρ€Π°Π½ΠΈΠ»ΠΈΡ‰Π΅ +- ΠšΠΎΠ½Ρ„Π΅Ρ€Π΅Π½Ρ†ΠΈΠΈ +- ... ΠΈ ΠΌΠ½ΠΎΠ³ΠΈΠ΅ Π΄Ρ€ΡƒΠ³ΠΈΠ΅ [сСрвисы](services.md) + +## ⚠️ Beta +Π’Π΅ΠΊΡƒΡ‰ΠΈΠΉ Ρ€Π΅Π»ΠΈΠ· ΠΏΠΎΠΊΠ° Π½Π΅ Π³ΠΎΡ‚ΠΎΠ² ΠΊ повсСднСвному использованию Π½Π΅ΠΊΠ²Π°Π»ΠΈΡ„ΠΈΡ†ΠΈΡ€ΠΎΠ²Π°Π½Π½Ρ‹ΠΌ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΌ. \ No newline at end of file diff --git a/intro_en.md b/intro_en.md new file mode 100644 index 0000000..98c8bb9 --- /dev/null +++ b/intro_en.md @@ -0,0 +1,13 @@ +# πŸ‘‹ Introduction + +[SelfPrivacy](https://selfprivacy.org) - mobile [app](https://f-droid.org/en/packages/pro.kherel.selfprivacy/) deploys a server with ready-to-use services: +- Email +- Password manager +- Messenger +- VPN +- File storage +- Conferences +- ... and many other [services](services.md) + +## ⚠️ Beta +The current release is not yet ready for everyday use by an unskilled user. \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..b2f50f8 --- /dev/null +++ b/main.py @@ -0,0 +1,71 @@ +import requests +from requests_toolbelt.multipart.encoder import MultipartEncoder +import json +import argparse +import os +import constants + +def main(): + # Impl + print("[INFO][PREFLIGHT] Initializing translator...") + + argumentParser = argparse.ArgumentParser(description="Translate text using Deepl service") + + argumentParser.add_argument( + "--file", + help="File, that contains text to translate", + type=str + ) + + argumentParser.add_argument( + "--output", + help="File, that translation will be written to(specify /dev/stdout for output into console)", + type=str + ) + + arguments = argumentParser.parse_args() + + if not os.path.exists(arguments.file): + print("[ERROR][PREFLIGHT] File {0} not found!".format(arguments.file)) + exit(1) + elif os.path.exists(arguments.file): + readOnlyFileDescriptor = open(arguments.file, "r") + + fileContent = readOnlyFileDescriptor.read() + + params = { + "auth_key": constants.API_ACCESS_KEY, + } + + print("[INFO][PREFLIGHT] Performing subscription check...", end="") + + usageMonitoringRequest = requests.get(constants.USAGE_API_ENDPOINT_URL, data=params) + + responseInJSON = json.loads(str(usageMonitoringRequest.text)) + availableCharacters = int(responseInJSON["character_limit"]) + + if len(fileContent) > availableCharacters: + print("[ERROR][PREFLIGHT] Amount of characters in the text file exceeds available!") + readOnlyFileDescriptor.close() + exit(1) + elif len(fileContent) <= availableCharacters: + print("done") + + params = { + "auth_key": constants.API_ACCESS_KEY, + "text": fileContent, + "target_lang": "EN" + } + + translationRequest = requests.get(constants.TRANSLATE_API_ENDPOINT_URL, data=params) + + responseInJSON = json.loads(str(translationRequest.text)) + translation = responseInJSON["translations"][0]["text"] + + readWriteFileDescriptor = open(arguments.output, "w") + readWriteFileDescriptor.write(translation) + + readOnlyFileDescriptor.close() + readWriteFileDescriptor.close() + +main() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..69ea33e --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +requests +requests_toolbelt +argparse \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..e69de29 diff --git a/source.txt b/source.txt new file mode 100644 index 0000000..f5cb204 --- /dev/null +++ b/source.txt @@ -0,0 +1 @@ +ΠŸΡ€ΠΈΠ²Π΅Ρ‚, ΠΌΠΈΡ€! \ No newline at end of file diff --git a/translation.txt b/translation.txt new file mode 100644 index 0000000..5dd01c1 --- /dev/null +++ b/translation.txt @@ -0,0 +1 @@ +Hello, world! \ No newline at end of file