From 927530c66d75c31bee684a0b32096d7cc2dd87ba Mon Sep 17 00:00:00 2001 From: Thelonius Kort Date: Thu, 5 Jan 2023 22:19:23 +0100 Subject: [PATCH] Add raw Deepl module and a progress bar --- lib/outlook/translators/deepl.ex | 77 +++++++++++++++++++ .../live/translation_live/form_component.ex | 15 +++- .../live/translation_live/new_edit.ex | 6 ++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 lib/outlook/translators/deepl.ex diff --git a/lib/outlook/translators/deepl.ex b/lib/outlook/translators/deepl.ex new file mode 100644 index 0000000..986a9f6 --- /dev/null +++ b/lib/outlook/translators/deepl.ex @@ -0,0 +1,77 @@ +defmodule Outlook.Translators.Deepl do + + def test(pid) do + for n <- 0..100 do + send(pid, {:progress, %{progress: n}}) + Process.sleep 50 + end + send(pid, {:progress, %{progress: nil}}) + end + + # @options [ssl: [{:versions, [:'tlsv1.2']}], recv_timeout: 500] + + @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 + form = get_multipart_form( + [ + {"target_lang", target_lang}, + {"file", content, {"form-data", [{:name, "file"}, {:filename, "datei.html"}]}, []} + ] + ) + + response_raw = HTTPoison.request!( + :post, + "https://api-free.deepl.com/v2/document", + form, + get_multipart_headers(auth_key) + ) + Jason.decode!(response_raw.body, keys: :atoms) + |> Map.put(:auth_key, auth_key) + end + + @doc """ + Upload the content to translate and return the estimated time until done. + """ + def check_status credentials do + response_raw = HTTPoison.request!( + :post, + "https://api-free.deepl.com/v2/document/#{credentials.document_id}", + get_multipart_form([{"document_key", credentials.document_key}]), + get_multipart_headers(credentials.auth_key) + ) + response = Jason.decode!(response_raw.body, keys: :atoms) + + case response do + %{status: "translating"} -> + Process.sleep(String.to_integer(response.seconds_remaining) * 1000) + check_status(credentials) + %{status: "done"} -> + response + end + end + + def get_translation credentials do + + response_raw = HTTPoison.request!( + :post, + "https://api-free.deepl.com/v2/document/#{credentials.document_id}/result", + get_multipart_form([{"document_key", credentials.document_key}]), + get_multipart_headers(credentials.auth_key) + ) + response_raw.body + end + + defp get_multipart_form fields do + {:multipart, fields} + end + + defp get_multipart_headers(auth_key) do + [ + "Authorization": "DeepL-Auth-Key #{auth_key}", + "Content-Type": "multipart/form-data", + "Transfer-Encoding": "chunked", + ] + end +end diff --git a/lib/outlook_web/live/translation_live/form_component.ex b/lib/outlook_web/live/translation_live/form_component.ex index b8e914e..d970608 100644 --- a/lib/outlook_web/live/translation_live/form_component.ex +++ b/lib/outlook_web/live/translation_live/form_component.ex @@ -39,6 +39,9 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
<%= InternalTree.render_html_preview(@translation.article.content, @myself) |> raw %> + <.button phx-disable-with="Translating..." phx-click="translate-deepl" phx-target={@myself} + data-confirm-not="Are you sure? All previously translated text will be lost.">Translate with Deepl +
""" @@ -51,10 +54,20 @@ defmodule OutlookWeb.TranslationLive.FormComponent do {:ok, socket |> assign(assigns) - |> assign(:changeset, changeset)} + |> assign(:changeset, changeset) + |> assign(:deepl_progress, nil)} + end + + def update(%{progress: progress}, socket) do + {:ok, socket |> assign(deepl_progress: progress)} end @impl true + def handle_event("translate-deepl", _, socket) do + Task.start_link(Outlook.Translators.Deepl, :test, [self()]) + {:noreply, socket} + end + def handle_event("validate", %{"translation" => translation_params}, socket) do changeset = socket.assigns.translation diff --git a/lib/outlook_web/live/translation_live/new_edit.ex b/lib/outlook_web/live/translation_live/new_edit.ex index 76da4dd..082c63d 100644 --- a/lib/outlook_web/live/translation_live/new_edit.ex +++ b/lib/outlook_web/live/translation_live/new_edit.ex @@ -50,4 +50,10 @@ defmodule OutlookWeb.TranslationLive.NewEdit do defp common_assigns(socket) do assign(socket, form_cmpnt_id: @form_cmpnt_id) end + + @impl true + def handle_info({:progress, payload}, socket) do + send_update(self(), FormComponent, progress: payload.progress, id: @form_cmpnt_id) + {:noreply, socket} + end end