Browse Source

Add Asset.read() to retrieve assets into bytes objects

pull/2107/head
slice 6 years ago
parent
commit
d80d4145b1
No known key found for this signature in database GPG Key ID: 1508C19D7436A26D
  1. 62
      discord/asset.py

62
discord/asset.py

@ -102,7 +102,7 @@ class Asset:
raise InvalidArgument("format must be one of {}".format(VALID_STATIC_FORMATS)) raise InvalidArgument("format must be one of {}".format(VALID_STATIC_FORMATS))
if hash is None: if hash is None:
return Asset(state) return cls(state)
url = 'https://cdn.discordapp.com/{key}/{0}/{1}.{2}?size={3}' url = 'https://cdn.discordapp.com/{key}/{0}/{1}.{2}?size={3}'
return cls(state, url.format(id, hash, format, size, key=key)) return cls(state, url.format(id, hash, format, size, key=key))
@ -130,38 +130,32 @@ class Asset:
def __hash__(self): def __hash__(self):
return hash(self._url) return hash(self._url)
async def save(self, fp, *, seek_begin=True): async def read(self):
"""|coro| """|coro|
Saves this asset into a file-like object. Retrieves the content of this asset as a :class:`bytes` object.
Parameters .. warning::
-----------
fp: Union[BinaryIO, :class:`os.PathLike`] :class:`PartialEmoji` won't have a connection state if user created,
Same as in :meth:`Attachment.save`. and a URL won't be present if a custom image isn't associated with
seek_begin: :class:`bool` the asset, e.g. a guild with no custom icon.
Same as in :meth:`Attachment.save`.
.. versionadded:: 1.1.0
Raises Raises
-------- ------
DiscordException DiscordException
There was no valid URL or internal connection state. There was no valid URL or internal connection state.
.. note::
:class:`PartialEmoji` will not have a state if you make
your own instance via ``PartialEmoji(animated=False, name='x', id=2345678)``.
The URL will not be provided if there is no custom image.
HTTPException HTTPException
Saving the asset failed. Downloading the asset failed.
NotFound NotFound
The asset was deleted. The asset was deleted.
Returns Returns
-------- -------
:class:`int` :class:`bytes`
The number of bytes written. The content of the asset.
""" """
if not self._url: if not self._url:
raise DiscordException('Invalid asset (no URL provided)') raise DiscordException('Invalid asset (no URL provided)')
@ -169,7 +163,31 @@ class Asset:
if self._state is None: if self._state is None:
raise DiscordException('Invalid state (no ConnectionState provided)') raise DiscordException('Invalid state (no ConnectionState provided)')
data = await self._state.http.get_from_cdn(self._url) return await self._state.http.get_from_cdn(self._url)
async def save(self, fp, *, seek_begin=True):
"""|coro|
Saves this asset into a file-like object.
Parameters
----------
fp: Union[BinaryIO, :class:`os.PathLike`]
Same as in :meth:`Attachment.save`.
seek_begin: :class:`bool`
Same as in :meth:`Attachment.save`.
Raises
------
Same as :meth:`read`.
Returns
--------
:class:`int`
The number of bytes written.
"""
data = await self.read()
if isinstance(fp, io.IOBase) and fp.writable(): if isinstance(fp, io.IOBase) and fp.writable():
written = fp.write(data) written = fp.write(data)
if seek_begin: if seek_begin:

Loading…
Cancel
Save