file deletion on response

This commit is contained in:
Owen
2024-12-16 23:05:51 +01:00
parent b85d9350a5
commit 040ff63e6d
3 changed files with 23 additions and 18 deletions
+8 -7
View File
@@ -8,7 +8,6 @@ defmodule PlantIdDiscordBot.Cog.PlantNet do
@api Application.compile_env(:plantid_discord_bot, :api) @api Application.compile_env(:plantid_discord_bot, :api)
@plantnet_api_base_url Application.compile_env(:plantid_discord_bot, :plantnet_api_base_url) @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) @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) @max_results Application.compile_env(:plantid_discord_bot, :max_results)
@doc """ @doc """
@@ -41,7 +40,6 @@ defmodule PlantIdDiscordBot.Cog.PlantNet do
attachment_urls = get_attachment_urls(interaction) attachment_urls = get_attachment_urls(interaction)
original_images = get_original_images(attachment_urls) original_images = get_original_images(attachment_urls)
# FUNCTION return image filename list as a list of images to delete
saved_images = saved_images =
try do try do
File.download_and_save_files!(attachment_urls) File.download_and_save_files!(attachment_urls)
@@ -68,9 +66,10 @@ defmodule PlantIdDiscordBot.Cog.PlantNet do
case HTTPoison.get(query_uri) do case HTTPoison.get(query_uri) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} -> {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
response_message = Parser.parse(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, %{ Api.create_followup_message(interaction.application_id, interaction.token, %{
content: response_message content: response_message <> "\n#{original_images}"
}) })
{:ok, %HTTPoison.Response{status_code: 400}} -> {:ok, %HTTPoison.Response{status_code: 400}} ->
@@ -81,7 +80,8 @@ defmodule PlantIdDiscordBot.Cog.PlantNet do
{:ok, %HTTPoison.Response{status_code: 404}} -> {:ok, %HTTPoison.Response{status_code: 404}} ->
# TODO add logger # TODO add logger
# RateLimiter.increase_counter(guild_id) RateLimiter.increase_counter(interaction.guild_id)
Api.create_followup_message(interaction.application_id, interaction.token, %{ Api.create_followup_message(interaction.application_id, interaction.token, %{
content: "Species Not Found" content: "Species Not Found"
}) })
@@ -99,7 +99,9 @@ defmodule PlantIdDiscordBot.Cog.PlantNet do
}) })
end 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 end
defp get_attachment_urls(interaction) do defp get_attachment_urls(interaction) do
@@ -115,7 +117,6 @@ defmodule PlantIdDiscordBot.Cog.PlantNet do
defp build_query_uri(image_filenames) do defp build_query_uri(image_filenames) do
identify_api_url = "#{@plantnet_api_base_url}/identify/all?api-key=#{@plantnet_api_key}" identify_api_url = "#{@plantnet_api_base_url}/identify/all?api-key=#{@plantnet_api_key}"
query_uri =
URI.append_query( URI.append_query(
URI.parse(identify_api_url), URI.parse(identify_api_url),
"images=#{Enum.join(image_filenames, "&images=")}" "images=#{Enum.join(image_filenames, "&images=")}"
+3 -3
View File
@@ -56,10 +56,10 @@ defmodule PlantIdDiscordBot.FileServer.File do
|> File.read() |> File.read()
end 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 @spec delete_files!([String.t()]) :: :ok
def delete_files!(filename) do def delete_files!(filenames) do
File.rm!(Path.join(@image_path, filename)) Enum.each(filenames, &File.rm!(Path.join(@image_path, &1)))
end end
@spec download_file!(String.t()) :: binary @spec download_file!(String.t()) :: binary
+5 -1
View File
@@ -28,7 +28,7 @@ defmodule PlantIdDiscordBot.PlantNet.Parser do
@gbif_base_url "https://www.gbif.org/species" @gbif_base_url "https://www.gbif.org/species"
@pfaf_base_url "https://pfaf.org/user/Plant.aspx?LatinName=" @pfaf_base_url "https://pfaf.org/user/Plant.aspx?LatinName="
@powo_base_url "https://powo.science.kew.org/taxon" @powo_base_url "https://powo.science.kew.org/taxon"
@score_threshold 0.3 @score_threshold Application.compile_env(:plantid_discord_bot, :score_threshold)
@doc """ @doc """
Parses the response from the PlantNet API into a map. 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"]}>) [GBIF](<#{best_result["gbif_url"]}>) | [PFAF](<#{best_result["pfaf_url"]}>) | [POWO](<#{best_result["powo_url"]}>)
Threat status: #{best_result["iucn"]["category"]} Threat status: #{best_result["iucn"]["category"]}
#{get_alternatives(other_results)}
""" """
end end
@@ -123,6 +125,8 @@ defmodule PlantIdDiscordBot.PlantNet.Parser do
alternatives = alternatives =
Enum.map(data, & &1["species"]["scientificNameWithoutAuthor"]) Enum.map(data, & &1["species"]["scientificNameWithoutAuthor"])
|> Enum.join(", ") |> Enum.join(", ")
"Alternatives include **#{alternatives}**."
end end
end end
end end