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
+15 -14
View File
@@ -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
+3 -3
View File
@@ -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
+5 -1
View File
@@ -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