Skip to content

Commit

Permalink
run black
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmTomahawkx committed Mar 17, 2024
1 parent 1c2333a commit 9abe3ce
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
4 changes: 2 additions & 2 deletions twitchio/ext/eventsub/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ async def pump(self) -> None:
await self.connect()
return

except TypeError as e:
except TypeError as e:
logger.warning(f"Received bad frame: {e.args[0]}")

if e.args[0] is None: # websocket was closed, reconnect
if e.args[0] is None: # websocket was closed, reconnect
logger.info("Known bad frame, restarting connection")
await self.connect()
return
Expand Down
2 changes: 1 addition & 1 deletion twitchio/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ async def get_user_emotes(self, user_id: str, broadcaster_id: Optional[str], tok
q: List = [("user_id", user_id)]
if broadcaster_id:
q.append(("broadcaster_id", broadcaster_id))

return await self.request(Route("GET", "chat/emotes/user", query=q, token=token))

async def get_stream_key(self, token: str, broadcaster_id: str):
Expand Down
29 changes: 20 additions & 9 deletions twitchio/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,15 +659,15 @@ class Emote:
Represents an Emote.
.. note::
It seems twitch is sometimes returning duplicate information from the emotes endpoint.
To deduplicate your emotes, you can call ``set()`` on the list of emotes (or any other hashmap), which will remove the duplicates.
.. code-block:: python
my_list_of_emotes = await user.get_user_emotes(...)
deduplicated_emotes = set(my_list_of_emotes)
Attributes
-----------
id: :class:`str`
Expand Down Expand Up @@ -701,7 +701,18 @@ class Emote:
Whether this emote is available in dark theme background mode.
"""

__slots__ = "id", "set_id", "owner_id", "name", "type", "scales", "format_static", "format_animated", "theme_light", "theme_dark"
__slots__ = (
"id",
"set_id",
"owner_id",
"name",
"type",
"scales",
"format_static",
"format_animated",
"theme_light",
"theme_dark",
)

def __init__(self, data: dict) -> None:
self.id: str = data["id"]
Expand All @@ -714,7 +725,7 @@ def __init__(self, data: dict) -> None:
self.theme_light: bool = "light" in data["theme_mode"]
self.format_static: bool = "static" in data["format"]
self.format_animated: bool = "animated" in data["format"]

def url_for(self, format: Literal["static", "animated"], theme: Literal["dark", "light"], scale: str) -> str:
"""
Returns a cdn url that can be used to download or serve the emote on a website.
Expand All @@ -729,26 +740,26 @@ def url_for(self, format: Literal["static", "animated"], theme: Literal["dark",
scale: :class:`str`
The scale of the emote. This should be formatted in this format: ``"1.0"``.
The scales available for this emote can be checked via :attr:`~.scales`.
Returns
--------
:class:`str`
"""
if scale not in self.scales:
raise ValueError(f"scale for this emote must be one of {', '.join(self.scales)}, not {scale}")

if (theme == "dark" and not self.theme_dark) or (theme == "light" and not self.theme_light):
raise ValueError(f"theme {theme} is not an available value for this emote")

if (format == "static" and not self.format_static) or (format == "animated" and not self.format_animated):
raise ValueError(f"format {format} is not an available value for this emote")

return f"https://static-cdn.jtvnw.net/emoticons/v2/{self.id}/{format}/{theme}/{scale}"

def __repr__(self) -> str:
return f"<Emote id={self.id} name={self.name}>"

def __hash__(self) -> int: # this exists so we can do set(list of emotes) to get rid of duplicates
def __hash__(self) -> int: # this exists so we can do set(list of emotes) to get rid of duplicates
return hash(self.id)


Expand Down
7 changes: 4 additions & 3 deletions twitchio/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,17 +639,17 @@ async def fetch_channel_emotes(self):

data = await self._http.get_channel_emotes(str(self.id))
return [ChannelEmote(self._http, x) for x in data]

async def fetch_user_emotes(self, token: str, broadcaster: Optional[PartialUser] = None) -> List[Emote]:
"""|coro|
Fetches emotes the user has access to. Optionally, you can filter by a broadcaster.
.. note::
As of writing, this endpoint seems extrememly unoptimized by twitch, and may (read: will) take a lot of API requests to load.
See https://github.com/twitchdev/issues/issues/921 .
Parameters
-----------
token: :class:`str`
Expand All @@ -663,6 +663,7 @@ async def fetch_user_emotes(self, token: str, broadcaster: Optional[PartialUser]
List[:class:`~twitchio.Emote`]
"""
from .models import Emote

data = await self._http.get_user_emotes(str(self.id), broadcaster and str(broadcaster.id), token)
return [Emote(d) for d in data]

Expand Down

0 comments on commit 9abe3ce

Please sign in to comment.