Skip to content

Commit 344f4c9

Browse files
committed
Support fetching images by their image name
1 parent 5d252b7 commit 344f4c9

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "recnetpy"
7-
version = "0.1.35"
7+
version = "0.1.36"
88
authors = [
99
{ name="RecNetBot Development"}
1010
]

src/recnetpy/managers/image_manager.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,36 @@ class ImageManager(BaseManager['Image', 'ImageResponse']):
1212
This is a factory object for creating image objects. Its the
1313
main interface for fetching image related data.
1414
"""
15+
async def get(self, name: str) -> Optional['Image']:
16+
"""
17+
Gets image data by their name, and returns it as an image object.
18+
Example of an image name: https://img.rec.net/>43ixtpl65wc9fc6ff4vsyrzoo.jpg<
19+
Only accepts image names of public RecNet posts.
20+
Returns nothing if the image doesn't exist or is private.
21+
22+
:param name: The name of the image.
23+
:return: An image object representing the data or nothing if not found.
24+
"""
25+
data: 'Response[List[ImageResponse]]' = await self.rec_net.api.images.v4.bulk.make_request('post', body = {'Names': name})
26+
if data.data: return self.create_dataclass(id, data.data[0])
27+
return None
28+
29+
30+
async def get_many(self, names: List[str]) -> Optional['Image']:
31+
"""
32+
Gets a list of images by a list of image names, and returns
33+
a list of image object.
34+
Example of an image name: https://img.rec.net/>43ixtpl65wc9fc6ff4vsyrzoo.jpg<
35+
Only accepts image names of public RecNet posts.
36+
Images that couldn't be found will be silently ignored.
37+
38+
:param name: The name of the image.
39+
:return: A list of image objects.
40+
"""
41+
data: 'Response[List[ImageResponse]]' = await self.rec_net.api.images.v4.bulk.make_request('post', body = {'Names': names})
42+
return self.create_from_data_list(data.data)
43+
44+
1545
async def fetch(self, id: int) -> Optional['Image']:
1646
"""
1747
Gets image data by their id, and returns it as an image object.
@@ -23,7 +53,8 @@ async def fetch(self, id: int) -> Optional['Image']:
2353
data: 'Response[ImageResponse]' = await self.rec_net.api.images.v4(id).make_request('get')
2454
if data.data: return self.create_dataclass(id, data.data)
2555
return None
26-
56+
57+
2758
async def fetch_many(self, ids: List[int]) -> List['Image']:
2859
"""
2960
Gets a list of images by a list of image ids, and returns

0 commit comments

Comments
 (0)