Skip to content

Commit

Permalink
add get charity campaigns
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmTomahawkx committed Aug 16, 2023
1 parent 856d383 commit 3b13fe9
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 2 deletions.
14 changes: 14 additions & 0 deletions docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ ChatterColor
:members:
:inherited-members:

CharityCampaign
----------------
.. attributetable:: CharityCampaign

.. autoclass:: CharityCampaign
:members:
:inherited-members:

.. attributetable:: CharityValues

.. autoclass:: CharityValues
:members:
:inherited-members:

CheerEmote
------------
.. attributetable:: CheerEmote
Expand Down
5 changes: 5 additions & 0 deletions twitchio/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -1173,3 +1173,8 @@ async def get_channel_chat_badges(self, broadcaster_id: str):

async def get_content_classification_labels(self, locale: str):
return await self.request(Route("GET", "content_classification_labels", "", query=[("locale", locale)]))

async def get_channel_charity_campaigns(self, broadcaster_id: str, token: str):
return await self.request(
Route("GET", "charity/campaigns", query=[("broadcaster_id", broadcaster_id)], token=token)
)
74 changes: 74 additions & 0 deletions twitchio/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
"ChatBadge",
"ChatBadgeVersions",
"ContentClassificationLabel",
"CharityValues",
"CharityCampaign",
)


Expand Down Expand Up @@ -1936,3 +1938,75 @@ def __init__(self, data: dict):

def __repr__(self):
return f"<ContentClassificationLabel id={self.id}>"


class CharityValues:
"""
Represents the current/target funds of a charity campaign.
Attributes
-----------
value: :class:`int`
The value of the campaign (either so far, or the target value).
decimal_places: :class:`int`
The decimal places to be inserted into :attr:`.value`.
currency: :class:`str`
The currency this charity is raising funds in. eg ``USD``, ``GBP``, ``EUR``.
"""

__slots__ = ("value", "decimal_places", "currency")

def __init__(self, data: dict) -> None:
self.value: int = data["value"]
self.decimal_places: int = data["decimal_places"]
self.currency: str = data["currency"]


class CharityCampaign:
"""
Represents a Charity Campaign on a channel.
Attributes
-----------
campaign_id: :class:`str`
The ID of the running charity campaign.
broadcaster: :class:`~twitchio.PartialUser`
The broadcaster running the campaign.
user: :class:`~twitchio.PartialUser`
The user who donated.
charity_name: :class:`str`
The name of the charity.
charity_description: :class:`str`
The description of the charity.
charity_logo: :class:`str`
The logo of the charity.
charity_website: :class:`str`
The websiet of the charity.
current: :class:`CharityValues`
The current funds raised by this campaign.
target: :class:`CharityValues`
The target funds to be raised for this campaign.
"""

__slots__ = (
"campaign_id",
"broadcaster",
"charity_name",
"charity_description",
"charity_logo",
"charity_website",
"current",
"target",
)

def __init__(self, data: dict, http: TwitchHTTP, broadcaster: PartialUser | None = None) -> None:
self.campaign_id: str = data["campaign_id"]
self.broadcaster: PartialUser = broadcaster or PartialUser(
http, data["broadcaster_id"], data["broadcaster_name"]
)
self.charity_name: str = data["charity_name"]
self.charity_description: str = data["charity_description"]
self.charity_logo: str = data["charity_logo"]
self.charity_website: str = data["charity_website"]
self.current: CharityValues = CharityValues(data["current_amount"])
self.target: CharityValues = CharityValues(data["target_amount"])
20 changes: 18 additions & 2 deletions twitchio/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
if TYPE_CHECKING:
from .http import TwitchHTTP
from .channel import Channel
from .models import BitsLeaderboard, Clip, ExtensionBuilder, Tag, FollowEvent, Prediction
from .models import BitsLeaderboard, Clip, ExtensionBuilder, Tag, FollowEvent, Prediction, CharityCampaign
__all__ = (
"PartialUser",
"BitLeaderboardUser",
Expand Down Expand Up @@ -1633,7 +1633,8 @@ async def fetch_chat_badges(self):
Fetches broadcaster's list of custom chat badges.
The list is empty if the broadcaster hasn't created custom chat badges.
Returns:
Returns
--------
List[:class:`twitchio.ChatBadge`]
"""

Expand All @@ -1642,6 +1643,21 @@ async def fetch_chat_badges(self):
data = await self._http.get_channel_chat_badges(broadcaster_id=str(self.id))
return [ChatBadge(x) for x in data]

async def fetch_charity_campaigns(self, token: str) -> List[CharityCampaign]:
"""|coro|
Fetches a list of charity campaigns the broadcaster is running.
Requires a user token with the ``channel:read:charity`` scope.
Returns
--------
List[:class:`twitchio.CharityCampaign`]
"""
from .models import CharityCampaign

data = await self._http.get_channel_charity_campaigns(str(self.id), token)
return [CharityCampaign(d, self._http, self) for d in data]


class BitLeaderboardUser(PartialUser):
__slots__ = "rank", "score"
Expand Down

0 comments on commit 3b13fe9

Please sign in to comment.