OK, I made a small mistake in the Prompt which I used initially. My Prompt was actually this:
“I want to list in html all observations of any taxon where the grade is needs_id, the rank is genus or higher but not as high as species, where the observer is [me_id], there is an identifier who is not [me_id], and [me_id] has not agreed with the community taxon id. I want the html to have a link to each record. Using python.”
I was sent the python code below. It did not work first time because of the error which I made in my Prompt. I asked it to add some debugging lines, and then I found my error.
The only change which I had to make in order to get the expected results was to change these lines:
“rank”: “genus”, # Minimum rank is genus
“hrank”: “species”, # Maximum rank is species (not species itself)
to this:
“hrank”: “genus”, # Minimum rank is genus
#“hrank”: “species”, # Maximum rank is species (not species itself)
I then asked it to give me the output in an HTML table and to add some additional columns. I have been using the code successfully with no other required bug fixes.
I then asked it to give me the output in an HTML table and to add some additional columns. I have been using the code successfully with (so far as I recall) no other required bug fixes.
I have also several times used ChatGPT to write Python code to do other fiddly things with the API, such as things which required combining results from “observations” with results from “taxons”.
I would be a disbeliever like you if I had not actually been successfully doing this for several months. It is a huge time-saver.
import requests
import json
iNaturalist API endpoint
API_URL = “https://api.inaturalist.org/v1/observations”
Parameters for the API query
params = {
“user_id”: “[me_id]”, # Filter observations by the observer
“quality_grade”: “needs_id”, # Observations needing identification
“rank”: “genus”, # Minimum rank is genus
“hrank”: “species”, # Maximum rank is species (not species itself)
“per_page”: 100, # Number of observations per page
}
def fetch_observations():
“”“Fetch observations from the iNaturalist API based on the query parameters.”“”
observations =
page = 1
while True:
params[“page”] = page
response = requests.get(API_URL, params=params)
data = response.json()
observations.extend(data[“results”])
if not data[“results”] or page >= data[“total_pages”]:
break
page += 1
return observations
def filter_observations(observations):
“”“Filter observations based on the additional criteria.”“”
filtered =
for obs in observations:
# Check if [me_id]
is the observer
if obs[“user”][“login”] != “[me_id]”:
continue
# Check if `[me_id]` is among identifiers and has agreed with the community taxon
has_[me_id]_identified = False
has_other_identifier = False
for idf in obs["identifications"]:
if idf["user"]["login"] == "[me_id]":
has_[me_id]_identified = True
if idf["current"] and idf["taxon"]["id"] == obs.get("community_taxon", {}).get("id"):
break
else:
has_other_identifier = True
else:
if has_[me_id]_identified and has_other_identifier:
filtered.append(obs)
return filtered
def generate_html(observations):
“”“Generate an HTML file with links to the observations.”“”
html_content = “
Filtered Observations
”
with open(“observations.html”, “w”) as f:
f.write(html_content)
print(“HTML file generated: observations.html”)
Main process
observations = fetch_observations()
filtered_observations = filter_observations(observations)
generate_html(filtered_observations)