Skip to content

Commit

Permalink
Create inaturalist_photo_page_url_search_240515.R
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelcleon authored May 15, 2024
1 parent c586ea1 commit a67373a
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions inaturalist_photo_page_url_search_240515.R
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit a67373a

Please sign in to comment.