Added selfprivacy-translate integration
parent
9032ac95d7
commit
5d98ca8e30
|
@ -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 = ""
|
|
@ -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()
|
|
@ -0,0 +1,3 @@
|
||||||
|
requests
|
||||||
|
requests_toolbelt
|
||||||
|
argparse
|
|
@ -0,0 +1,22 @@
|
||||||
|
export ruInputFiles=$(ls src/ru/src)
|
||||||
|
|
||||||
|
for inputFile in ${ruInputFiles}
|
||||||
|
do
|
||||||
|
python3.9 ${1} --file src/ru/src/${inputFile} --output src/en/src/${inputFile}
|
||||||
|
done
|
||||||
|
|
||||||
|
buildEnglishTranslation()
|
||||||
|
{
|
||||||
|
cd src/en/
|
||||||
|
mdbook build
|
||||||
|
}
|
||||||
|
|
||||||
|
buildRussianTranslations()
|
||||||
|
{
|
||||||
|
cd src/ru/
|
||||||
|
mdbook build
|
||||||
|
}
|
||||||
|
|
||||||
|
buildEnglishTranslation
|
||||||
|
cd ../../
|
||||||
|
buildRussianTranslations
|
Loading…
Reference in New Issue