Commit 04ff06f8 authored by Jakob Lerch's avatar Jakob Lerch
Browse files

add etherpad api key

parent 38a9556e
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"
})
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
config_file = "config.conf"
# ..read api keys
api_keys = ConfigParser()
api_keys.read("api_keys.conf")
# ..read everything else
config = ConfigParser()
config.read(config_file)
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"])
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"])
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()))
dst_pad.write(translate_src_to_dst(src_pad.read()))
# ..further translation
while(True):
while True:
# TODO: think of algorithm / try naiv method, if this works, do it
pass
dst_pad.write(translate_src_to_dst(src_pad.read()))
time.sleep(5)
from abc import ABC, abstractmethod
import requests
class Pad(ABC):
@abstractmethod
def read(self) -> str:
...
pass
@abstractmethod
def write(self, string: str) -> None:
"""[summary]
Args:
string (str): [description]
"""
...
pass
class Etherpad(Pad):
def __init__(self, url: str):
self.url = url
"""For reading and writing the official HTTP API of Etherpad was used
"""
def __init__(self, url: str, api_key: str):
url_parted = url.partition("/")
self.main_url = url_parted[0]
self.pad_id = url_parted[-1]
self.api_key = api_key
def read(self) -> str:
pass
r = requests.get(self.main_url + "api/1/getText?apikey=" + self.api_key +
"&padID=" + self.pad_id)
return r.text
def write(self, string: str) -> None:
pass
......@@ -37,4 +41,4 @@ class HedgedocNote(Pad):
return r.text
def write(self, string: str) -> None:
raise NotImplementedError()
\ No newline at end of file
raise NotImplementedError()
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