Update Deepl translation

Now getting auth-key from db.
This commit is contained in:
Thelonius Kort
2023-01-09 21:29:19 +01:00
parent 3cd9d983fe
commit 71e6a8da60
4 changed files with 111 additions and 82 deletions

View File

@ -6,99 +6,93 @@ defmodule Outlook.Translators do
import Ecto.Query, warn: false
alias Outlook.Repo
alias Outlook.Translators.DeeplAccount
alias Outlook.InternalTree.TranslationUnit
alias Outlook.Translators.{DeeplAccount,Deepl}
alias OutlookWeb.HtmlDocComponent
@doc """
Returns the list of deepl_accounts.
## Examples
iex> list_deepl_accounts()
[%DeeplAccount{}, ...]
"""
def list_deepl_accounts do
Repo.all(DeeplAccount)
end
@doc """
Gets a single deepl_account.
Raises `Ecto.NoResultsError` if the Deepl account does not exist.
## Examples
iex> get_deepl_account!(123)
%DeeplAccount{}
iex> get_deepl_account!(456)
** (Ecto.NoResultsError)
"""
def get_deepl_account!(id), do: Repo.get!(DeeplAccount, id)
@doc """
Creates a deepl_account.
def get_deepl_auth_key(user_id) do
query =
from DeeplAccount,
where: [user_id: ^user_id],
select: [:auth_key]
Repo.one!(query)
|> Map.get(:auth_key)
end
## Examples
iex> create_deepl_account(%{field: value})
{:ok, %DeeplAccount{}}
iex> create_deepl_account(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_deepl_account(attrs \\ %{}) do
%DeeplAccount{}
|> DeeplAccount.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a deepl_account.
## Examples
iex> update_deepl_account(deepl_account, %{field: new_value})
{:ok, %DeeplAccount{}}
iex> update_deepl_account(deepl_account, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_deepl_account(%DeeplAccount{} = deepl_account, attrs) do
deepl_account
|> DeeplAccount.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a deepl_account.
## Examples
iex> delete_deepl_account(deepl_account)
{:ok, %DeeplAccount{}}
iex> delete_deepl_account(deepl_account)
{:error, %Ecto.Changeset{}}
"""
def delete_deepl_account(%DeeplAccount{} = deepl_account) do
Repo.delete(deepl_account)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking deepl_account changes.
## Examples
iex> change_deepl_account(deepl_account)
%Ecto.Changeset{data: %DeeplAccount{}}
"""
def change_deepl_account(%DeeplAccount{} = deepl_account, attrs \\ %{}) do
DeeplAccount.changeset(deepl_account, attrs)
end
def translate(translation, current_user) do
%{lang: target_lang,
article: %{content: article_tree, language: source_lang}
} = translation
article_as_html = prepare_article(article_tree)
auth_key = get_deepl_auth_key(current_user.id)
args = [
self(),
article_as_html,
%{
source_lang: source_lang,
target_lang: target_lang,
auth_key: auth_key
}
]
Task.start_link(Deepl, :translate, args)
end
defp prepare_article(tree) do
# Logger.info "so far."
HtmlDocComponent.render_doc(%{tree: tree})
|> Phoenix.HTML.Safe.to_iodata()
|> IO.iodata_to_binary()
end
def process_translation_result(result, tunit_ids) do
# TODO: update :our_character_count
process_translation(result.translation, tunit_ids)
end
def process_translation(translation, tunit_ids) do
tunit_map = translation
|> Floki.parse_fragment!
|> Floki.find("span.tunit")
|> Enum.map(fn {_,atts,cont} ->
%TranslationUnit{
uuid: Enum.find(atts, fn {k,_} -> k == "uuid" end) |> Tuple.to_list |> Enum.at(1),
content: Floki.raw_html(cont),
status: :untranslated
}
end)
|> Enum.map(fn tunit -> {tunit.uuid, tunit} end)
|> Enum.into(%{})
case Enum.sort(Map.keys(tunit_map)) == Enum.sort(tunit_ids) do
true -> {:ok, tunit_map}
false -> {:error, "keys don't equal the originals"}
end
end
end