mirror of
https://github.com/TheRealOwenRees/plantid-discord-bot.git
synced 2026-07-22 20:16:57 +00:00
separation of functions
This commit is contained in:
@@ -39,30 +39,53 @@ defmodule PlantIdDiscordBot.Cog.PlantNet do
|
||||
}
|
||||
})
|
||||
|
||||
attachment_urls = get_attachment_urls(interaction)
|
||||
original_images = get_original_images(attachment_urls)
|
||||
try do
|
||||
attachment_urls = get_attachment_urls(interaction)
|
||||
original_images = get_original_images(attachment_urls)
|
||||
|
||||
saved_images =
|
||||
try do
|
||||
File.download_and_save_files!(attachment_urls)
|
||||
rescue
|
||||
e ->
|
||||
Api.create_followup_message(interaction.application_id, interaction.token, %{
|
||||
content: e.message
|
||||
})
|
||||
end
|
||||
saved_images = File.download_and_save_files!(attachment_urls)
|
||||
|
||||
images =
|
||||
if Mix.env() in [:test, :dev] do
|
||||
PlantIdDiscordBotTest.Mocks.PlantNet.Images.images()
|
||||
else
|
||||
Enum.map(saved_images, fn {:ok, filename} -> "#{@fileserver_url}/#{filename}" end)
|
||||
end
|
||||
prepare_images(saved_images)
|
||||
|> build_query_uri()
|
||||
|> get_response(interaction, original_images)
|
||||
|
||||
Logger.debug(Enum.join(images, ", "))
|
||||
cleanup_saved_images(saved_images)
|
||||
rescue
|
||||
e ->
|
||||
Api.create_followup_message(interaction.application_id, interaction.token, %{
|
||||
content: e.message
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
query_uri = build_query_uri(images)
|
||||
defp get_attachment_urls(interaction) do
|
||||
interaction.data.resolved.attachments
|
||||
|> Map.values()
|
||||
|> Enum.map(& &1.url)
|
||||
end
|
||||
|
||||
@spec get_original_images([String.t()]) :: String.t()
|
||||
defp get_original_images(attachment_urls), do: Enum.join(attachment_urls, "\n")
|
||||
|
||||
defp prepare_images(saved_images) do
|
||||
if Mix.env() in [:test, :dev] do
|
||||
PlantIdDiscordBotTest.Mocks.PlantNet.Images.images()
|
||||
else
|
||||
Enum.map(saved_images, fn {:ok, filename} -> "#{@fileserver_url}/#{filename}" end)
|
||||
end
|
||||
end
|
||||
|
||||
@spec build_query_uri([String.t()]) :: String.t()
|
||||
defp build_query_uri(image_filenames) do
|
||||
URI.parse("#{@plantnet_api_base_url}/identify/all?api-key=#{@plantnet_api_key}")
|
||||
|> URI.append_query("images=#{Enum.join(image_filenames, "&images=")}")
|
||||
|> URI.append_query("nb-results=#{@max_results}")
|
||||
|> URI.append_query("type=kt")
|
||||
|> URI.to_string()
|
||||
end
|
||||
|
||||
@spec get_response(String.t(), Nostrum.Interaction.t(), String.t()) :: :ok
|
||||
defp get_response(query_uri, interaction, original_images) do
|
||||
case HTTPoison.get(query_uri) do
|
||||
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
|
||||
response_message = Parser.parse(body)
|
||||
@@ -105,30 +128,10 @@ defmodule PlantIdDiscordBot.Cog.PlantNet do
|
||||
content: "Internal Server Error"
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
defp cleanup_saved_images(saved_images) do
|
||||
Enum.map(saved_images, fn {:ok, filename} -> filename end)
|
||||
|> File.delete_files!()
|
||||
end
|
||||
|
||||
defp get_attachment_urls(interaction) do
|
||||
interaction.data.resolved.attachments
|
||||
|> Map.values()
|
||||
|> Enum.map(& &1.url)
|
||||
end
|
||||
|
||||
@spec get_original_images([String.t()]) :: String.t()
|
||||
defp get_original_images(attachment_urls), do: Enum.join(attachment_urls, "\n")
|
||||
|
||||
@spec build_query_uri([String.t()]) :: String.t()
|
||||
defp build_query_uri(image_filenames) do
|
||||
identify_api_url = "#{@plantnet_api_base_url}/identify/all?api-key=#{@plantnet_api_key}"
|
||||
|
||||
URI.append_query(
|
||||
URI.parse(identify_api_url),
|
||||
"images=#{Enum.join(image_filenames, "&images=")}"
|
||||
)
|
||||
|> URI.append_query("nb-results=#{@max_results}")
|
||||
|> URI.append_query("type=kt")
|
||||
|> URI.to_string()
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user