Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Technik
protocol-translation-live
Commits
04ff06f8
Commit
04ff06f8
authored
Mar 29, 2022
by
Jakob Lerch
Browse files
add etherpad api key
parent
38a9556e
Changes
2
Hide whitespace changes
Inline
Side-by-side
protocol_translator/__main__.py
View file @
04ff06f8
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
)
protocol_translator/pad.py
View file @
04ff06f8
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
()
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment