From a67373a6f04b3cae85012cf46baa99381a6edd2c Mon Sep 17 00:00:00 2001 From: Miguel Leon Date: Wed, 15 May 2024 16:30:11 -0600 Subject: [PATCH] Create inaturalist_photo_page_url_search_240515.R --- inaturalist_photo_page_url_search_240515.R | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 inaturalist_photo_page_url_search_240515.R diff --git a/inaturalist_photo_page_url_search_240515.R b/inaturalist_photo_page_url_search_240515.R new file mode 100644 index 0000000..375ff9f --- /dev/null +++ b/inaturalist_photo_page_url_search_240515.R @@ -0,0 +1,27 @@ + +# Install necessary packages if not already installed +if (!require("httr")) install.packages("httr") +if (!require("jsonlite")) install.packages("jsonlite") +get_inat_photos_url <- function(species_name) { + api_url <- sprintf("https://api.inaturalist.org/v1/taxa?q=%s", URLencode(species_name)) + + response <- httr::GET(api_url) + + if (response$status_code == 200) { + data <- httr::content(response, "parsed") + + if (length(data$results) > 0) { + species_id <- data$results[[1]]$id + species_name_url <- gsub(" ", "-", species_name) + url <- sprintf("https://www.inaturalist.org/taxa/%s-%s/browse_photos", species_id, species_name_url) + return(url) + } else { + return("No results found for the given species name.") + } + } else { + return("Failed to fetch data from iNaturalist API.") + } +} + +url <- get_inat_photos_url("Andira inermis") +print(url)