Update Deepl translation
Now getting auth-key from db.
This commit is contained in:
@ -1,10 +1,12 @@
|
||||
defmodule Outlook.Translators.Deepl do
|
||||
|
||||
def test(pid) do
|
||||
for n <- 0..100 do
|
||||
send(pid, {:progress, %{progress: n}})
|
||||
Process.sleep 50
|
||||
end
|
||||
def translate(pid, article, options) do
|
||||
send(pid, {:progress, %{progress: 0}})
|
||||
credentials = start_translation(article, options)
|
||||
status = check_status(pid, credentials)
|
||||
translation = get_translation(credentials)
|
||||
send(pid, {:translation, %{translation: translation, billed_characters: status.billed_characters}})
|
||||
Process.sleep 1000
|
||||
send(pid, {:progress, %{progress: nil}})
|
||||
end
|
||||
|
||||
@ -13,10 +15,11 @@ defmodule Outlook.Translators.Deepl do
|
||||
@doc """
|
||||
Upload the content to translate and return document_id and document_key as Map.
|
||||
"""
|
||||
def start_translation %{auth_key: auth_key} = _credentials, content, target_lang do
|
||||
def start_translation content, options do
|
||||
form = get_multipart_form(
|
||||
[
|
||||
{"target_lang", target_lang},
|
||||
{"source_lang", options.source_lang},
|
||||
{"target_lang", options.target_lang},
|
||||
{"file", content, {"form-data", [{:name, "file"}, {:filename, "datei.html"}]}, []}
|
||||
]
|
||||
)
|
||||
@ -25,16 +28,16 @@ defmodule Outlook.Translators.Deepl do
|
||||
:post,
|
||||
"https://api-free.deepl.com/v2/document",
|
||||
form,
|
||||
get_multipart_headers(auth_key)
|
||||
get_multipart_headers(options.auth_key)
|
||||
)
|
||||
Jason.decode!(response_raw.body, keys: :atoms)
|
||||
|> Map.put(:auth_key, auth_key)
|
||||
|> Map.put(:auth_key, options.auth_key)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Upload the content to translate and return the estimated time until done.
|
||||
Check the status until translation is "done".
|
||||
"""
|
||||
def check_status credentials do
|
||||
def check_status pid, credentials do
|
||||
response_raw = HTTPoison.request!(
|
||||
:post,
|
||||
"https://api-free.deepl.com/v2/document/#{credentials.document_id}",
|
||||
@ -45,8 +48,12 @@ defmodule Outlook.Translators.Deepl do
|
||||
|
||||
case response do
|
||||
%{status: "translating"} ->
|
||||
Process.sleep(String.to_integer(response.seconds_remaining) * 1000)
|
||||
check_status(credentials)
|
||||
steps = response.seconds_remaining * 5
|
||||
for n <- 0..steps do
|
||||
send(pid, {:progress, %{progress: 100 * n / steps}})
|
||||
Process.sleep 200
|
||||
end
|
||||
check_status(pid, credentials)
|
||||
%{status: "done"} ->
|
||||
response
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user