Skip to content

Commit

Permalink
Sign multiple assets, better autosign
Browse files Browse the repository at this point in the history
  • Loading branch information
shayypy committed Apr 30, 2024
1 parent 6529ef7 commit 503a6dd
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 16 deletions.
13 changes: 10 additions & 3 deletions guilded/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
import io
import os
import re
from typing import TYPE_CHECKING, Any, Optional, Tuple, Union
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union
from urllib.parse import parse_qs, parse_qsl, quote_plus, urlencode, urlparse
import yarl

Expand All @@ -64,6 +64,8 @@
if TYPE_CHECKING:
from typing_extensions import Self

from .types.asset import UrlSignature


VALID_STATIC_FORMATS = frozenset({'jpeg', 'jpg', 'webp', 'png'})
VALID_ASSET_FORMATS = VALID_STATIC_FORMATS | {'gif', 'apng'}
Expand Down Expand Up @@ -153,8 +155,13 @@ async def sign(self) -> Self:
if valid:
self.url = url_with_signature(self.url, self._state.cdn_qs)
else:
urls = await self._state.create_url_signatures([self.url])
self.url = urls[0]
urls: List[UrlSignature] = await self._state.create_url_signatures([self.url])
if not urls[0].get("signature") and self._state.cdn_qs:
# We may have just received a valid token with the sign
# request that refresh_signature was unable to retrieve.
self.url = url_with_signature(self.url, self._state.cdn_qs)
else:
self.url = urls[0].get("signature") or urls[0]["url"]

return self

Expand Down
55 changes: 50 additions & 5 deletions guilded/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
import traceback
from typing import TYPE_CHECKING, Any, Callable, Coroutine, Generator, List, Optional, Type, Union

from .errors import ClientException, HTTPException
from .errors import ClientException
from .enums import *
from .events import BaseEvent
from .gateway import GuildedWebSocket, WebSocketClosure
Expand All @@ -72,7 +72,10 @@
from types import TracebackType
from typing_extensions import Self

from .types.asset import UrlSignature

from .abc import ServerChannel
from .asset import AssetMixin
from .channel import DMChannel, PartialMessageable
from .emote import Emote
from .message import ChatMessage
Expand Down Expand Up @@ -324,7 +327,7 @@ async def setup_hook(self) -> None:
To perform asynchronous setup after the bot is logged in but before
it has connected to the Websocket, overwrite this method.
This is only called once, in :meth:`login`, and will be called before
This is only called once, in :meth:`start`, and will be called before
any events are dispatched, making it a better solution than doing such
setup in the :func:`~.on_ready` event.
Expand Down Expand Up @@ -808,6 +811,49 @@ async def remove_status(self) -> None:
"""
await self.http.delete_my_status()

async def sign_assets(self, *assets: AssetMixin) -> List[AssetMixin]:
"""|coro|
Sign multiple assets at once.
.. warning::
Assets are signed in chunks of 10. If you are providing a lot of
assets, this method may be very slow.
.. versionadded:: 1.13.1
Parameters
-----------
assets: List[:class:`.AssetMixin`]
The assets to sign.
Returns
--------
List[:class:`.AssetMixin`]
The signed input assets, modified in place.
Raises
-------
HTTPException
Failed to sign one or multiple assets.
"""

filtered = [asset for asset in assets if not asset.signed]
batches: List[List[AssetMixin]] = [filtered[i:i+10] for i in range(0, len(filtered), 10)]
for batch in batches:
if len(batch) == 0:
continue

urls: List[UrlSignature] = await self.http.create_url_signatures([asset.url for asset in batch])
# Assume the same order
i = 0
for asset in batch:
asset.url = urls[i].get("signature") or urls[i]["url"]
i += 1

return assets

async def on_error(self, event_method, *args, **kwargs) -> None:
print(f'Ignoring exception in {event_method}:', file=sys.stderr)
traceback.print_exc()
Expand All @@ -822,9 +868,8 @@ async def start(self, token: str = None, *, reconnect: bool = True) -> None:

self.http.session = aiohttp.ClientSession()

# We want to do this before anything has a chance to construct an Asset
await self.http.refresh_signature()

# The client does not have an auto signature at this point. Is this
# something we should worry about for `setup_hook`s?
await self._async_setup_hook()
await self.setup_hook()

Expand Down
17 changes: 9 additions & 8 deletions guilded/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,24 +406,20 @@ async def refresh_signature(self):
"""
:class:`bool`: Refresh the CDN signature. Returns a :class:`bool`;
whether the current signature is valid.
.. versionadded:: 1.13.1
"""
if not self._auto_sign:
return False
if not self.cdn_qs_expired:
return True

try:
headers = await self.get_event_headers()
await self.get_my_user()
except:
return False

new_token = headers.get("x-cdn-token")
if not new_token:
self.cdn_qs = None
return False

self.cdn_qs = new_token
return True
return not self.cdn_qs_expired

# /teams

Expand Down Expand Up @@ -599,6 +595,11 @@ async def request(self, route, **kwargs):
last_user.id = self.my_id
self._users[self.my_id] = last_user

cdn_token = response.headers.get('x-cdn-token')
if cdn_token and self._auto_sign and cdn_token != self.cdn_qs and cdn_token != 'None':
log.debug('Response provided a CDN token, storing')
self.cdn_qs = cdn_token

if response.headers.get('Content-Type', '').startswith(('image/', 'video/')):
data = await response.read()
else:
Expand Down
33 changes: 33 additions & 0 deletions guilded/types/asset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
MIT License
Copyright (c) 2020-present shay (shayypy)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

from __future__ import annotations
from typing import TypedDict
from typing_extensions import NotRequired


class UrlSignature(TypedDict):
url: str
signature: NotRequired[str]
retryAfter: NotRequired[int]

0 comments on commit 503a6dd

Please sign in to comment.