Commit 7ed4ac88 authored by Jakob Lerch's avatar Jakob Lerch
Browse files

do stuff

parent 8082410e
......@@ -18,18 +18,18 @@ class Etherpad(Pad):
"""
def __init__(self, url: str, api_key: str):
url_parted = url.partition("/")
self.main_url = url_parted[0]
self.pad_id = url_parted[-1]
url_parted = url.split("/p/")
self.main_url, self.pad_id = url_parted
self.api_key = api_key
def read(self) -> str:
r = requests.get(self.main_url + "api/1/getText?apikey=" + self.api_key +
r = requests.get(self.main_url + "/api/1/getText?apikey=" + self.api_key +
"&padID=" + self.pad_id)
return r.text
return r.json()["data"]["text"]
def write(self, string: str) -> None:
pass
r = requests.get(self.main_url + "/api/1/setText?apikey=" + self.api_key +
"&padID=" + self.pad_id + "&text=" + string)
class HedgedocNote(Pad):
......
import requests
import json
def translate_src_to_dst(url: str, src_string: str, src_lang: str, dst_lang: str, api_key: str) -> str:
r = requests.post(
url + "/translate",
data={
"q": src_string if src_string != "" else " ",
"source": src_lang,
"target": dst_lang,
"format": "text"
}
)
return json.loads(r.text)["translatedText"]
from configparser import ConfigParser
from pathlib import Path
import requests
import time
from .pad import Etherpad, HedgedocNote
# TODO: make src_lang and dst_lang and config accessible to this function
# TODO: get api key
# TODO: https://etherpad.org/doc/v1.8.4/#index_http_api
# TODO: test etherpad
# TODO: write setup.py
def translate_src_to_dst(src_string: str) -> str:
dst_string = requests.post(
config["main"]["translate_service_url"] + "/translate",
data={
"q": src_string,
"source": src_lang,
"target": dst_lang,
"format": "text",
},
)
return dst_string
if __name__ == "__main__":
# read config
# ..read api keys
api_keys = ConfigParser()
api_keys.read("api_keys.conf")
# ..read everything else
config = ConfigParser()
config.read("config.conf")
# create src pad
src_conf = config["source"]
src_pad = (
HedgedocNote(src_conf["url"])
if src_conf["kind"] == "hedgedoc"
else Etherpad(src_conf["url"], api_keys["default"]["etherpad_key"])
)
src_lang = src_conf["language"]
# create dst pad
# ..other than etherpad is not supported as destination yet
dst_conf = config["source"]
dst_pad = Etherpad(dst_conf["url"], api_keys["default"]["etherpad_key"])
dst_lang = dst_conf["language"]
# do translation
# ..initial translation
dst_pad.write(translate_src_to_dst(src_pad.read()))
# ..further translation
while True:
# TODO: think of algorithm / try naiv method, if this works, do it
dst_pad.write(translate_src_to_dst(src_pad.read()))
time.sleep(5)
from configparser import ConfigParser
from functools import cache
from protocol_translator.pad import Etherpad
from protocol_translation.pad import Etherpad
test_string_1 = "123\n123\n123\n123\n"
test_string_2 = "124\n124\n124\n124\n"
@cache
def init():
global test_string_1
global test_string_2
api_keys = ConfigParser()
api_keys.read("api_keys.conf")
e = Etherpad("https://collab.fem-net.de/pad/p/test2",
e = Etherpad("https://collab.fem-net.de/pad/p/wOdasS2kvJwC9BDb",
api_keys["default"]["etherpad_key"])
return e
......@@ -17,13 +23,16 @@ def init():
def test_init():
e = init()
assert isinstance(e, pad.Etherpad)
assert isinstance(e, Etherpad)
def test_read():
e = init()
assert e.read() == "testtest"
assert e.read() == test_string_1
def test_write():
e = init()
e.write(test_string_2)
assert e.read() == test_string_2
e.write(test_string_1)
from configparser import ConfigParser
import json
import requests
from functools import cache
import protocol_translation.utils as utils
def init():
api_keys = ConfigParser()
api_keys.read("api_keys.conf")
config = ConfigParser()
config.read("config.conf")
return (api_keys, config)
def test_translate():
keys, conf = init()
text_de = "Dies ist ein Ball."
text_en = "this is a ball."
translated_text = utils.translate_src_to_dst(
conf["main"]["libretranslate_service_url"], text_de, "de", "en", keys["default"]["libretranslate_key"])
assert translated_text == text_en
def test_translate_empty():
keys, conf = init()
text_de = ""
text_en = ""
translated_text = utils.translate_src_to_dst(
conf["main"]["libretranslate_service_url"], text_de, "de", "en", keys["default"]["libretranslate_key"])
assert translated_text == text_en
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment