From 040ff63e6d7a7f6212ffd326574746c02a0caf76 Mon Sep 17 00:00:00 2001 From: Owen Date: Mon, 16 Dec 2024 23:05:51 +0100 Subject: [PATCH] file deletion on response --- lib/plantid_discord_bot/cogs/plantnet.ex | 29 +++++++++++---------- lib/plantid_discord_bot/file_server/file.ex | 6 ++--- lib/plantid_discord_bot/plantnet/parser.ex | 6 ++++- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/lib/plantid_discord_bot/cogs/plantnet.ex b/lib/plantid_discord_bot/cogs/plantnet.ex index 4cbb2b5..2301aaf 100644 --- a/lib/plantid_discord_bot/cogs/plantnet.ex +++ b/lib/plantid_discord_bot/cogs/plantnet.ex @@ -8,7 +8,6 @@ defmodule PlantIdDiscordBot.Cog.PlantNet do @api Application.compile_env(:plantid_discord_bot, :api) @plantnet_api_base_url Application.compile_env(:plantid_discord_bot, :plantnet_api_base_url) @plantnet_api_key Application.compile_env(:plantid_discord_bot, :plantnet_api_key) - @score_threshold Application.compile_env(:plantid_discord_bot, :score_threshold) @max_results Application.compile_env(:plantid_discord_bot, :max_results) @doc """ @@ -41,7 +40,6 @@ defmodule PlantIdDiscordBot.Cog.PlantNet do attachment_urls = get_attachment_urls(interaction) original_images = get_original_images(attachment_urls) - # FUNCTION return image filename list as a list of images to delete saved_images = try do File.download_and_save_files!(attachment_urls) @@ -68,9 +66,10 @@ defmodule PlantIdDiscordBot.Cog.PlantNet do case HTTPoison.get(query_uri) do {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> response_message = Parser.parse(body) - # RateLimiter.increase_counter(guild_id) + RateLimiter.increase_counter(interaction.guild_id) + Api.create_followup_message(interaction.application_id, interaction.token, %{ - content: response_message + content: response_message <> "\n#{original_images}" }) {:ok, %HTTPoison.Response{status_code: 400}} -> @@ -81,7 +80,8 @@ defmodule PlantIdDiscordBot.Cog.PlantNet do {:ok, %HTTPoison.Response{status_code: 404}} -> # TODO add logger - # RateLimiter.increase_counter(guild_id) + RateLimiter.increase_counter(interaction.guild_id) + Api.create_followup_message(interaction.application_id, interaction.token, %{ content: "Species Not Found" }) @@ -99,7 +99,9 @@ defmodule PlantIdDiscordBot.Cog.PlantNet do }) end - # TODO delete saved images, make into a task + # TODO make into a task for async deletion, deal with errors + Enum.map(saved_images, fn {:ok, filename} -> filename end) + |> File.delete_files!() end defp get_attachment_urls(interaction) do @@ -115,13 +117,12 @@ defmodule PlantIdDiscordBot.Cog.PlantNet do defp build_query_uri(image_filenames) do identify_api_url = "#{@plantnet_api_base_url}/identify/all?api-key=#{@plantnet_api_key}" - query_uri = - 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() + 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 diff --git a/lib/plantid_discord_bot/file_server/file.ex b/lib/plantid_discord_bot/file_server/file.ex index 8ac791a..83e87f7 100644 --- a/lib/plantid_discord_bot/file_server/file.ex +++ b/lib/plantid_discord_bot/file_server/file.ex @@ -56,10 +56,10 @@ defmodule PlantIdDiscordBot.FileServer.File do |> File.read() end - # TODO make into a task for async deletion + # TODO make into a task for async deletion and deal with errors @spec delete_files!([String.t()]) :: :ok - def delete_files!(filename) do - File.rm!(Path.join(@image_path, filename)) + def delete_files!(filenames) do + Enum.each(filenames, &File.rm!(Path.join(@image_path, &1))) end @spec download_file!(String.t()) :: binary diff --git a/lib/plantid_discord_bot/plantnet/parser.ex b/lib/plantid_discord_bot/plantnet/parser.ex index 2271992..37732ed 100644 --- a/lib/plantid_discord_bot/plantnet/parser.ex +++ b/lib/plantid_discord_bot/plantnet/parser.ex @@ -28,7 +28,7 @@ defmodule PlantIdDiscordBot.PlantNet.Parser do @gbif_base_url "https://www.gbif.org/species" @pfaf_base_url "https://pfaf.org/user/Plant.aspx?LatinName=" @powo_base_url "https://powo.science.kew.org/taxon" - @score_threshold 0.3 + @score_threshold Application.compile_env(:plantid_discord_bot, :score_threshold) @doc """ Parses the response from the PlantNet API into a map. @@ -86,6 +86,8 @@ defmodule PlantIdDiscordBot.PlantNet.Parser do [GBIF](<#{best_result["gbif_url"]}>) | [PFAF](<#{best_result["pfaf_url"]}>) | [POWO](<#{best_result["powo_url"]}>) Threat status: #{best_result["iucn"]["category"]} + + #{get_alternatives(other_results)} """ end @@ -123,6 +125,8 @@ defmodule PlantIdDiscordBot.PlantNet.Parser do alternatives = Enum.map(data, & &1["species"]["scientificNameWithoutAuthor"]) |> Enum.join(", ") + + "Alternatives include **#{alternatives}**." end end end