Skip to content

Commit

Permalink
taxon list: Use typing context if more API calls needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
synrg committed Aug 14, 2024
1 parent a85c709 commit 85f6688
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions inatcog/commands/taxon.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,32 +140,33 @@ async def taxon_list(self, ctx, *, query: Optional[str]):
# remainder (direct children - those at the specified rank level)
# don't constitute a single page of results, then show children
# instead.
_descendants = await ctx.inat_client.taxa.search(
taxon_id=_without_rank_ids,
rank_level=rank_level,
is_active=True,
per_page=500,
)
# The choice of 2500 as our limit is arbitrary:
# - will take 5 more API calls to satisfy
# - encompasses the largest genera (e.g. Astragalus)
# - meant to limit unreasonable sized queries so they don't make
# excessive API demands
# - TODO: switch to using a local DB built from full taxonomy dump
# so we can lift this restriction
if _descendants.count() > 2500:
short_description = "Children"
await ctx.send(
f"Too many {self.p.plural(_per_rank)}. "
"Listing children instead."
async with ctx.typing():
_descendants = await ctx.inat_client.taxa.search(
taxon_id=_without_rank_ids,
rank_level=rank_level,
is_active=True,
per_page=500,
)
_per_rank = "child"
else:
taxon_list = [
taxon,
*_children,
*(await _descendants.async_all()),
]
# The choice of 2500 as our limit is arbitrary:
# - will take 5 more API calls to satisfy
# - encompasses the largest genera (e.g. Astragalus)
# - meant to limit unreasonable sized queries so they don't make
# excessive API demands
# - TODO: switch to using a local DB built from full taxonomy dump
# so we can lift this restriction
if _descendants.count() > 2500:
short_description = "Children"
await ctx.send(
f"Too many {self.p.plural(_per_rank)}. "
"Listing children instead."
)
_per_rank = "child"
else:
taxon_list = [
taxon,
*_children,
*(await _descendants.async_all()),
]
if _per_rank == "child":
short_description = "Children"
else:
Expand Down

0 comments on commit 85f6688

Please sign in to comment.