dronefly-discord 0.2.0.dev1__tar.gz → 0.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,17 +1,16 @@
1
- Metadata-Version: 2.3
1
+ Metadata-Version: 2.4
2
2
  Name: dronefly-discord
3
- Version: 0.2.0.dev1
3
+ Version: 0.2.2
4
4
  Summary: Dronefly Discord library
5
- License: AGPL-3.0-or-later
6
5
  Author: Ben Armstrong
7
- Author-email: synrg@debian.org
8
- Requires-Python: >=3.11,<3.12
9
- Classifier: License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)
6
+ Author-email: Ben Armstrong <synrg@debian.org>
7
+ License-Expression: AGPL-3.0-or-later
10
8
  Classifier: Programming Language :: Python :: 3
11
9
  Classifier: Programming Language :: Python :: 3.11
12
- Requires-Dist: discord-py (>=2.3.1)
13
- Requires-Dist: dronefly-core (>=0.5.0.dev1,<0.6.0)
14
- Requires-Dist: inflect (>=5.3.0,<6.0.0)
10
+ Requires-Dist: inflect>=5.3.0,<6
11
+ Requires-Dist: discord-py>=2.3.1
12
+ Requires-Dist: dronefly-core==0.5.2
13
+ Requires-Python: >=3.11, <3.12
15
14
  Description-Content-Type: text/markdown
16
15
 
17
16
  # Dronefly Discord
@@ -37,4 +36,3 @@ Discord bot's core components.
37
36
  Dronefly Discord bot brings [iNaturalist](https://www.inaturalist.org) taxa,
38
37
  observations, and other data from the site into conversations on the
39
38
  [Discord](https://discord.com) chat platform.
40
-
@@ -0,0 +1,14 @@
1
+ import discord
2
+
3
+
4
+ class InteractionContext:
5
+ def __init__(self, interaction: discord.Interaction):
6
+ self.interaction = interaction
7
+ self.author = interaction.user
8
+ self.bot = interaction.client
9
+
10
+ async def send(self, **kwargs):
11
+ if self.interaction.response.is_done():
12
+ return await self.interaction.followup.send(**kwargs)
13
+ await self.interaction.response.send_message(**kwargs)
14
+ return await self.interaction.original_response()
@@ -1,8 +1,10 @@
1
+ import asyncio
2
+ import io
1
3
  from functools import wraps
2
4
 
3
5
  import discord
4
6
  import inflect
5
- from pyinaturalist.models import IconPhoto, Taxon
7
+ from pyinaturalist.models import IconPhoto, Photo, Taxon
6
8
 
7
9
  from dronefly.core import formatters
8
10
  from dronefly.core.formatters.constants import WWW_BASE_URL
@@ -117,15 +119,51 @@ def get_taxon_photo(taxon, index):
117
119
  return (taxon_photo, description)
118
120
 
119
121
 
120
- def make_image_embed(taxon: Taxon, formatter: TaxonFormatter, index: int = 1):
122
+ async def fetch_image_bytes(
123
+ photo: Photo, size: str = "large", open_timeout: float = 15
124
+ ) -> bytes | None:
125
+ binary_stream = None
126
+ try:
127
+
128
+ def open_and_read():
129
+ nonlocal binary_stream
130
+ binary_stream = photo.open(size) # blocking; run inside thread
131
+ return binary_stream.read()
132
+
133
+ data = await asyncio.to_thread(
134
+ open_and_read,
135
+ )
136
+ return data
137
+ finally:
138
+ if binary_stream is not None:
139
+ binary_stream.close()
140
+
141
+
142
+ async def make_image_embed(
143
+ taxon: Taxon,
144
+ formatter: TaxonFormatter,
145
+ index: int = 1,
146
+ session=None,
147
+ ):
121
148
  title = formatter.format_title()
122
149
 
123
150
  (taxon_photo, description) = get_taxon_photo(taxon, index)
124
151
  formatter.image_description = description
125
152
  embed = make_embed(url=f"{WWW_BASE_URL}/taxa/{taxon.id}")
126
153
  embed.title = title
154
+ file = None
127
155
  if taxon_photo:
128
- embed.set_image(url=taxon_photo.original_url)
156
+ image_bytes = await fetch_image_bytes(taxon_photo, "original")
157
+ if image_bytes:
158
+ filename = f"taxon-{taxon.id}-{index}.png"
159
+ buf = io.BytesIO(image_bytes)
160
+ buf.seek(0)
161
+ file = discord.File(buf, filename=filename)
162
+ embed.set_image(url=f"attachment://{filename}")
163
+ else:
164
+ embed.set_image(url=taxon_photo.original_url)
129
165
  embed.set_footer(text=taxon_photo.attribution)
130
166
  embed.description = formatter.format()
167
+ if file:
168
+ return {"embed": embed, "file": file}
131
169
  return embed