Skip to content

Commit 3b13fe9

Browse files
committed
add get charity campaigns
1 parent 856d383 commit 3b13fe9

File tree

4 files changed

+111
-2
lines changed

4 files changed

+111
-2
lines changed

docs/reference.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ ChatterColor
135135
:members:
136136
:inherited-members:
137137

138+
CharityCampaign
139+
----------------
140+
.. attributetable:: CharityCampaign
141+
142+
.. autoclass:: CharityCampaign
143+
:members:
144+
:inherited-members:
145+
146+
.. attributetable:: CharityValues
147+
148+
.. autoclass:: CharityValues
149+
:members:
150+
:inherited-members:
151+
138152
CheerEmote
139153
------------
140154
.. attributetable:: CheerEmote

twitchio/http.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,3 +1173,8 @@ async def get_channel_chat_badges(self, broadcaster_id: str):
11731173

11741174
async def get_content_classification_labels(self, locale: str):
11751175
return await self.request(Route("GET", "content_classification_labels", "", query=[("locale", locale)]))
1176+
1177+
async def get_channel_charity_campaigns(self, broadcaster_id: str, token: str):
1178+
return await self.request(
1179+
Route("GET", "charity/campaigns", query=[("broadcaster_id", broadcaster_id)], token=token)
1180+
)

twitchio/models.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
"ChatBadge",
8181
"ChatBadgeVersions",
8282
"ContentClassificationLabel",
83+
"CharityValues",
84+
"CharityCampaign",
8385
)
8486

8587

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

19371939
def __repr__(self):
19381940
return f"<ContentClassificationLabel id={self.id}>"
1941+
1942+
1943+
class CharityValues:
1944+
"""
1945+
Represents the current/target funds of a charity campaign.
1946+
1947+
Attributes
1948+
-----------
1949+
value: :class:`int`
1950+
The value of the campaign (either so far, or the target value).
1951+
decimal_places: :class:`int`
1952+
The decimal places to be inserted into :attr:`.value`.
1953+
currency: :class:`str`
1954+
The currency this charity is raising funds in. eg ``USD``, ``GBP``, ``EUR``.
1955+
"""
1956+
1957+
__slots__ = ("value", "decimal_places", "currency")
1958+
1959+
def __init__(self, data: dict) -> None:
1960+
self.value: int = data["value"]
1961+
self.decimal_places: int = data["decimal_places"]
1962+
self.currency: str = data["currency"]
1963+
1964+
1965+
class CharityCampaign:
1966+
"""
1967+
Represents a Charity Campaign on a channel.
1968+
1969+
Attributes
1970+
-----------
1971+
campaign_id: :class:`str`
1972+
The ID of the running charity campaign.
1973+
broadcaster: :class:`~twitchio.PartialUser`
1974+
The broadcaster running the campaign.
1975+
user: :class:`~twitchio.PartialUser`
1976+
The user who donated.
1977+
charity_name: :class:`str`
1978+
The name of the charity.
1979+
charity_description: :class:`str`
1980+
The description of the charity.
1981+
charity_logo: :class:`str`
1982+
The logo of the charity.
1983+
charity_website: :class:`str`
1984+
The websiet of the charity.
1985+
current: :class:`CharityValues`
1986+
The current funds raised by this campaign.
1987+
target: :class:`CharityValues`
1988+
The target funds to be raised for this campaign.
1989+
"""
1990+
1991+
__slots__ = (
1992+
"campaign_id",
1993+
"broadcaster",
1994+
"charity_name",
1995+
"charity_description",
1996+
"charity_logo",
1997+
"charity_website",
1998+
"current",
1999+
"target",
2000+
)
2001+
2002+
def __init__(self, data: dict, http: TwitchHTTP, broadcaster: PartialUser | None = None) -> None:
2003+
self.campaign_id: str = data["campaign_id"]
2004+
self.broadcaster: PartialUser = broadcaster or PartialUser(
2005+
http, data["broadcaster_id"], data["broadcaster_name"]
2006+
)
2007+
self.charity_name: str = data["charity_name"]
2008+
self.charity_description: str = data["charity_description"]
2009+
self.charity_logo: str = data["charity_logo"]
2010+
self.charity_website: str = data["charity_website"]
2011+
self.current: CharityValues = CharityValues(data["current_amount"])
2012+
self.target: CharityValues = CharityValues(data["target_amount"])

twitchio/user.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
if TYPE_CHECKING:
3636
from .http import TwitchHTTP
3737
from .channel import Channel
38-
from .models import BitsLeaderboard, Clip, ExtensionBuilder, Tag, FollowEvent, Prediction
38+
from .models import BitsLeaderboard, Clip, ExtensionBuilder, Tag, FollowEvent, Prediction, CharityCampaign
3939
__all__ = (
4040
"PartialUser",
4141
"BitLeaderboardUser",
@@ -1633,7 +1633,8 @@ async def fetch_chat_badges(self):
16331633
Fetches broadcaster's list of custom chat badges.
16341634
The list is empty if the broadcaster hasn't created custom chat badges.
16351635
1636-
Returns:
1636+
Returns
1637+
--------
16371638
List[:class:`twitchio.ChatBadge`]
16381639
"""
16391640

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

1646+
async def fetch_charity_campaigns(self, token: str) -> List[CharityCampaign]:
1647+
"""|coro|
1648+
1649+
Fetches a list of charity campaigns the broadcaster is running.
1650+
Requires a user token with the ``channel:read:charity`` scope.
1651+
1652+
Returns
1653+
--------
1654+
List[:class:`twitchio.CharityCampaign`]
1655+
"""
1656+
from .models import CharityCampaign
1657+
1658+
data = await self._http.get_channel_charity_campaigns(str(self.id), token)
1659+
return [CharityCampaign(d, self._http, self) for d in data]
1660+
16451661

16461662
class BitLeaderboardUser(PartialUser):
16471663
__slots__ = "rank", "score"

0 commit comments

Comments
 (0)