Skip to content
This repository was archived by the owner on Dec 28, 2021. It is now read-only.

Commit 8cf6ddc

Browse files
Ethan ChrispEthan Chrisp
Ethan Chrisp
authored and
Ethan Chrisp
committed
Add Friend Feed Reaction functionality (#8)
1 parent 0763c92 commit 8cf6ddc

File tree

7 files changed

+193
-6
lines changed

7 files changed

+193
-6
lines changed

callofduty/client.py

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
from typing import List, Optional, Union
33

4-
from .enums import GameType, Language, Mode, Platform, TimeFrame, Title
4+
from .enums import GameType, Language, Mode, Platform, Reaction, TimeFrame, Title
55
from .feed import Blog, FeedItem, Video
66
from .leaderboard import Leaderboard
77
from .loadout import Loadout, LoadoutItem
@@ -15,6 +15,7 @@
1515
VerifyLanguage,
1616
VerifyMode,
1717
VerifyPlatform,
18+
VerifyReaction,
1819
VerifyTimeFrame,
1920
VerifyTitle,
2021
)
@@ -144,6 +145,94 @@ async def GetFriendFeed(self, **kwargs) -> List[FeedItem]:
144145

145146
return feed
146147

148+
async def SetFeedReaction(
149+
self,
150+
reaction: Reaction,
151+
platform: Platform,
152+
username: str,
153+
title: Title,
154+
date: int,
155+
category: str,
156+
) -> None:
157+
"""
158+
Set a Reaction to a Call of Duty Friend Feed item.
159+
160+
Parameters
161+
----------
162+
reaction : callofduty.Reaction
163+
Reaction to add to the feed item.
164+
platform : callofduty.Platform
165+
Platform to get the player from.
166+
username : str
167+
Player's username for the designated platform.
168+
title : callofduty.Title
169+
Title of the feed item.
170+
date : int
171+
Timstamp of the feed item.
172+
category : str
173+
Category of the feed item.
174+
175+
Returns
176+
-------
177+
None
178+
"""
179+
180+
VerifyReaction(reaction)
181+
VerifyPlatform(platform)
182+
VerifyTitle(title)
183+
184+
json: dict = {
185+
"username": username,
186+
"platform": platform.value,
187+
"title": title.value,
188+
"date": date,
189+
"category": category,
190+
}
191+
192+
return await self.http.SetFeedReaction(reaction.value, json)
193+
194+
async def RemoveFeedReaction(
195+
self,
196+
platform: Platform,
197+
username: str,
198+
title: Title,
199+
date: int,
200+
category: str,
201+
) -> None:
202+
"""
203+
Unset the Reaction to a Call of Duty Friend Feed item.
204+
205+
Parameters
206+
----------
207+
platform : callofduty.Platform
208+
Platform to get the player from.
209+
username : str
210+
Player's username for the designated platform.
211+
title : callofduty.Title
212+
Title of the feed item.
213+
date : int
214+
Timstamp of the feed item.
215+
category : str
216+
Category of the feed item.
217+
218+
Returns
219+
-------
220+
None
221+
"""
222+
223+
VerifyPlatform(platform)
224+
VerifyTitle(title)
225+
226+
json: dict = {
227+
"username": username,
228+
"platform": platform.value,
229+
"title": title.value,
230+
"date": date,
231+
"category": category,
232+
}
233+
234+
return await self.http.SetFeedReaction(Reaction.Remove.value, json)
235+
147236
async def GetMyIdentities(self) -> list:
148237
"""
149238
Get the Title Identities for the authenticated Call of Duty player.

callofduty/enums.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,12 @@ class GameType(Enum):
4141
Core = "core"
4242
Hardcore = "hc"
4343
WorldLeague = "arena"
44+
45+
46+
class Reaction(Enum):
47+
Fire = "love"
48+
ThumbsUp = "thumbs_up"
49+
Cool = "cool"
50+
Shocked = "shock"
51+
FistBump = "congrats"
52+
Remove = "none"

callofduty/errors.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ class InvalidGameType(ClientException):
7676
pass
7777

7878

79+
class InvalidReaction(ClientException):
80+
"""
81+
Exception which is thrown when an invalid reaction is passed to any
82+
client function.
83+
"""
84+
85+
pass
86+
87+
7988
class HTTPException(CallofDutyException):
8089
"""
8190
Exception which is thrown when an HTTP request operation fails.

callofduty/feed.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from datetime import datetime
33
from typing import List, Optional
44

5-
from .enums import Title
5+
from .enums import Reaction, Title
66
from .match import Match
77
from .object import Object
88
from .player import Player
@@ -57,6 +57,46 @@ def __init__(self, client, data: dict):
5757
{"id": _matchId, "platform": self.player.platform, "title": self.title},
5858
)
5959

60+
async def react(self, reaction: Reaction) -> None:
61+
"""
62+
Set a Reaction to the Call of Duty Friend Feed item.
63+
64+
Parameters
65+
----------
66+
reaction : callofduty.Reaction
67+
Reaction to add to the feed item.
68+
69+
Returns
70+
-------
71+
None
72+
"""
73+
74+
await self._client.SetFeedReaction(
75+
reaction,
76+
self.player.platform,
77+
self.player.username,
78+
self.title,
79+
(self.date.timestamp() * 1000),
80+
self.category,
81+
)
82+
83+
async def unreact(self) -> None:
84+
"""
85+
Unset the Reaction to the Call of Duty Friend Feed item.
86+
87+
Returns
88+
-------
89+
None
90+
"""
91+
92+
return await self._client.RemoveFeedReaction(
93+
self.player.platform,
94+
self.player.username,
95+
self.title,
96+
(self.date.timestamp() * 1000),
97+
self.category,
98+
)
99+
60100

61101
class Blog(Object):
62102
"""

callofduty/http.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,13 @@ class Request:
4343
baseUrl : str, optional
4444
Base URL to use for the request (default is https://callofduty.com/)
4545
headers : dict, optional
46-
Headers to send along with the request (default is None.)
46+
Headers to include in the request (default is None.)
47+
json : dict, optional
48+
JSON data to include in the body of the request (default is None.)
4749
"""
4850

4951
defaultBaseUrl: str = "https://callofduty.com/"
52+
myBaseUrl: str = "https://my.callofduty.com/"
5053
squadsBaseUrl: str = "https://squads.callofduty.com/"
5154

5255
accessToken: Optional[str] = None
@@ -55,6 +58,7 @@ class Request:
5558
def __init__(self, method: str, endpoint: Optional[str] = None, **kwargs):
5659
self.method: str = method
5760
self.headers: Dict[str, str] = {}
61+
self.json: dict = kwargs.get("json", {})
5862

5963
if endpoint is not None:
6064
baseUrl: str = kwargs.get("baseUrl", self.defaultBaseUrl)
@@ -95,7 +99,7 @@ async def Send(self, req: Request) -> Union[dict, str]:
9599

96100
async with self.session as client:
97101
res: Response = await client.request(
98-
req.method, req.url, headers=req.headers
102+
req.method, req.url, headers=req.headers, json=req.json
99103
)
100104

101105
data: Union[dict, str] = await JSONorText(res)
@@ -161,6 +165,16 @@ async def GetFriendFeed(self) -> Union[dict, str]:
161165
Request("GET", "api/papi-client/userfeed/v1/friendFeed/rendered/")
162166
)
163167

168+
async def SetFeedReaction(self, reaction: str, json: dict) -> Union[dict, str]:
169+
return await self.Send(
170+
Request(
171+
"POST",
172+
f"api/papi-client/userfeed/v1/reactions/set/{reaction}/en",
173+
baseUrl=Request.myBaseUrl,
174+
json=json,
175+
)
176+
)
177+
164178
async def GetMyIdentities(self) -> Union[dict, str]:
165179
return await self.Send(Request("GET", "api/papi-client/crm/cod/v2/identities/"))
166180

callofduty/utils.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import logging
22
import re
33

4-
from .enums import GameType, Language, Mode, Platform, TimeFrame, Title
4+
from .enums import GameType, Language, Mode, Platform, Reaction, TimeFrame, Title
55
from .errors import (
66
InvalidGameType,
77
InvalidLanguage,
88
InvalidMode,
99
InvalidPlatform,
10+
InvalidReaction,
1011
InvalidTimeFrame,
1112
InvalidTitle,
1213
)
@@ -110,6 +111,21 @@ def VerifyGameType(value: GameType):
110111
raise InvalidGameType(f"{value.name} is not a valid game type")
111112

112113

114+
def VerifyReaction(value: Reaction):
115+
"""
116+
Raise an InvalidReaction client exception if a value which is not
117+
present in the Reaction enum is passed.
118+
119+
Parameters
120+
----------
121+
value : callofduty.Reaction
122+
Value to confirm is present in the Reaction enum.
123+
"""
124+
125+
if value not in Reaction:
126+
raise InvalidReaction(f"{value.name} is not a valid reaction")
127+
128+
113129
def StripHTML(input: str) -> str:
114130
"""
115131
Strip the HTML formatting from a string.

test.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from dotenv import load_dotenv
55

66
import callofduty
7-
from callofduty import Mode, Platform, Title
7+
from callofduty import Mode, Platform, Reaction, Title
88

99

1010
async def main():
@@ -86,6 +86,16 @@ async def main():
8686
# if player.username != item.player.username:
8787
# print(f" {player.username} ({player.platform.name})")
8888

89+
# feed = await client.GetFriendFeed(limit=5)
90+
# for item in feed:
91+
# print(item.text)
92+
# await item.react(Reaction.Fire)
93+
94+
# feed = await client.GetFriendFeed(limit=5)
95+
# for item in feed:
96+
# print(item.text)
97+
# await item.unreact()
98+
8999
# maps = await client.GetAvailableMaps(Title.ModernWarfare)
90100
# for mapName in maps:
91101
# print(mapName)

0 commit comments

Comments
 (0)