Skip to content

Commit 17236f9

Browse files
Celeste AngCeleste Ang
authored andcommitted
feat: allow posting of multiple media_ids in twitter plugin
1 parent f2aa30b commit 17236f9

File tree

5 files changed

+45
-34
lines changed

5 files changed

+45
-34
lines changed

plugins/twitter/examples/test_game_twitter.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,29 @@
1717
# Test case 1: Post a Tweet
1818
print("\nRunning Test Case 1: Post a Tweet")
1919
post_tweet_fn = game_twitter_plugin.get_function('post_tweet')
20-
post_tweet_fn("Hello world! This is a test tweet from the GAME Twitter Plugin!")
20+
post_tweet_fn(tweet="Hello world! This is a test tweet from the GAME Twitter Plugin!", media_ids=[])
2121
print("Posted tweet!")
2222

2323
# Test case 2: Reply to a Tweet
2424
print("\nRunning Test Case 2: Reply to a Tweet")
2525
reply_tweet_fn = game_twitter_plugin.get_function('reply_tweet')
26-
reply_tweet_fn(tweet_id=1879472470362816626, reply="Hey! This is a test reply!")
26+
reply_tweet_fn(tweet_id=1879472470362816626, reply="Hey! This is a test reply!", media_ids=[])
2727
print("Replied to tweet!")
2828

29-
# Test case 3: Quote a Tweet
30-
print("\nRunning Test Case 3: Quote a Tweet")
31-
quote_tweet_fn = game_twitter_plugin.get_function('quote_tweet')
32-
quote_tweet_fn(tweet_id=1879472470362816626, quote="Hey! This is a test quote tweet!")
33-
print("Quoted tweet!")
29+
# # Test case 3: Quote a Tweet
30+
# print("\nRunning Test Case 3: Quote a Tweet")
31+
# quote_tweet_fn = game_twitter_plugin.get_function('quote_tweet')
32+
# quote_tweet_fn(tweet_id=1879472470362816626, quote="Hey! This is a test quote tweet!", media_ids=[])
33+
# print("Quoted tweet!")
3434

35-
# Test case 4: Search Tweets
36-
print("\nRunning Test Case 4: Search Tweets")
37-
search_tweets_fn = game_twitter_plugin.get_function('search_tweets')
38-
response = search_tweets_fn(query="Python")
39-
print(f"Searched tweets: {response}")
35+
# # Test case 4: Search Tweets
36+
# print("\nRunning Test Case 4: Search Tweets")
37+
# search_tweets_fn = game_twitter_plugin.get_function('search_tweets')
38+
# response = search_tweets_fn(query="Python")
39+
# print(f"Searched tweets: {response}")
4040

41-
# Test case 5: Get authenticated user
42-
print("\nRunning Test Case 5: Get details of authenticated user")
43-
get_authenticated_user_fn = game_twitter_plugin.get_function('get_authenticated_user')
44-
response = get_authenticated_user_fn()
45-
print(f"Got details of authenticated user: {response}")
41+
# # Test case 5: Get authenticated user
42+
# print("\nRunning Test Case 5: Get details of authenticated user")
43+
# get_authenticated_user_fn = game_twitter_plugin.get_function('get_authenticated_user')
44+
# response = get_authenticated_user_fn()
45+
# print(f"Got details of authenticated user: {response}")

plugins/twitter/examples/test_twitter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
# Test case 1: Post a Tweet
2222
print("\nRunning Test Case 1: Post a Tweet")
2323
post_tweet_fn = twitter_plugin.get_function('post_tweet')
24-
post_tweet_fn("Hello world! This is a test tweet from the Twitter Plugin!")
24+
post_tweet_fn("Hello world! This is a test tweet from the Twitter Plugin!", media_ids=[])
2525
print("Posted tweet!")
2626

2727
# Test case 2: Reply to a Tweet
2828
print("\nRunning Test Case 2: Reply to a Tweet")
2929
reply_tweet_fn = twitter_plugin.get_function('reply_tweet')
30-
reply_tweet_fn(tweet_id=1879472470362816626, reply="Hey! This is a test reply!")
30+
reply_tweet_fn(tweet_id=1879472470362816626, reply="Hey! This is a test reply!", media_ids=[])
3131
print("Replied to tweet!")
3232

3333
# Test case 3: Like a Tweet
@@ -39,7 +39,7 @@
3939
# Test case 4: Quote a Tweet
4040
print("\nRunning Test Case 4: Quote a Tweet")
4141
quote_tweet_fn = twitter_plugin.get_function('quote_tweet')
42-
quote_tweet_fn(tweet_id=1879472470362816626, quote="Hey! This is a test quote tweet!")
42+
quote_tweet_fn(tweet_id=1879472470362816626, quote="Hey! This is a test quote tweet!", media_ids=[])
4343
print("Quoted tweet!")
4444

4545
# Test case 5: Get Metrics

plugins/twitter/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
44

55
[tool.poetry]
66
name = "twitter_plugin_gamesdk"
7-
version = "0.2.0"
7+
version = "0.2.1"
88
description = "Twitter Plugin for Python SDK for GAME by Virtuals"
99
authors = ["Celeste Ang <[email protected]>"]
1010
homepage = "https://github.com/game-by-virtuals/game-python"

plugins/twitter/twitter_plugin_gamesdk/game_twitter_plugin.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def __init__(self, options: Dict[str, Any]) -> None:
5656
self._functions: Dict[str, Callable[..., Any]] = {
5757
"reply_tweet": self._reply_tweet,
5858
"post_tweet": self._post_tweet,
59-
# "like_tweet": self._like_tweet,
59+
"like_tweet": self._like_tweet,
6060
"quote_tweet": self._quote_tweet,
6161
"search_tweets": self._search_tweets,
6262
"get_authenticated_user": self._get_authenticated_user,
@@ -112,14 +112,15 @@ def _fetch_api(self, endpoint: str, method: str = "GET", data: Optional[Dict] =
112112

113113
return response.json()
114114

115-
def _post_tweet(self, tweet: str, media_id: Optional[str] = None) -> Dict[str, Any]:
115+
def _post_tweet(self, tweet: str, media_ids: Optional[List[str]] = None) -> Dict[str, Any]:
116116
"""
117117
Post a tweet with optional media.
118118
"""
119+
if media_ids and len(media_ids) > 4:
120+
raise ValueError("media_ids cannot contain more than 4 items.")
119121
payload = {"content": tweet}
120-
if media_id:
121-
payload["mediaId"] = media_id
122-
122+
if media_ids:
123+
payload["mediaIds"] = media_ids
123124
return self._fetch_api("/post", "POST", data=payload)
124125

125126
def _search_tweets(self, query: str) -> Dict[str, Any]:
@@ -128,13 +129,15 @@ def _search_tweets(self, query: str) -> Dict[str, Any]:
128129
"""
129130
return self._fetch_api(f"/search?query={requests.utils.quote(query)}", "GET")
130131

131-
def _reply_tweet(self, tweet_id: int, reply: str, media_id: Optional[str] = None) -> None:
132+
def _reply_tweet(self, tweet_id: int, reply: str, media_ids: Optional[str] = None) -> None:
132133
"""
133134
Reply to a tweet.
134135
"""
136+
if media_ids and len(media_ids) > 4:
137+
raise ValueError("media_ids cannot contain more than 4 items.")
135138
payload = {"content": reply}
136-
if media_id:
137-
payload["mediaId"] = media_id
139+
if media_ids:
140+
payload["mediaIds"] = media_ids
138141
return self._fetch_api(f"/reply/{tweet_id}", "POST", data=payload)
139142

140143
def _like_tweet(self, tweet_id: int) -> None:
@@ -143,11 +146,15 @@ def _like_tweet(self, tweet_id: int) -> None:
143146
"""
144147
return self._fetch_api(f"/like/{tweet_id}", "POST")
145148

146-
def _quote_tweet(self, tweet_id: int, quote: str) -> None:
149+
def _quote_tweet(self, tweet_id: int, quote: str, media_ids: Optional[str] = None) -> None:
147150
"""
148151
Quote a tweet.
149152
"""
153+
if media_ids and len(media_ids) > 4:
154+
raise ValueError("media_ids cannot contain more than 4 items.")
150155
payload = {"content": quote}
156+
if media_ids:
157+
payload["mediaIds"] = media_ids
151158
return self._fetch_api(f"/quote/{tweet_id}", "POST", data=payload)
152159

153160
def _get_authenticated_user(self) -> Dict[str, Any]:

plugins/twitter/twitter_plugin_gamesdk/twitter_plugin.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def _get_metrics(self) -> Dict[str, int]:
150150
self.logger.error(f"Failed to fetch metrics: {e}")
151151
return {}
152152

153-
def _reply_tweet(self, tweet_id: int, reply: str) -> None:
153+
def _reply_tweet(self, tweet_id: int, reply: str, media_ids: Optional[str] = None) -> None:
154154
"""
155155
Reply to a specific tweet.
156156
@@ -162,12 +162,14 @@ def _reply_tweet(self, tweet_id: int, reply: str) -> None:
162162
tweepy.TweepyException: If there's an error posting the reply.
163163
"""
164164
try:
165+
if media_ids and len(media_ids) > 4:
166+
raise ValueError("media_ids cannot contain more than 4 items.")
165167
self.twitter_client.create_tweet(in_reply_to_tweet_id=tweet_id, text=reply)
166168
self.logger.info(f"Successfully replied to tweet {tweet_id}.")
167169
except tweepy.TweepyException as e:
168170
self.logger.error(f"Failed to reply to tweet {tweet_id}: {e}")
169171

170-
def _post_tweet(self, tweet: str) -> Dict[str, Any]:
172+
def _post_tweet(self, tweet: str, media_ids: Optional[str] = None) -> Dict[str, Any]:
171173
"""
172174
Post a new tweet.
173175
@@ -181,6 +183,8 @@ def _post_tweet(self, tweet: str) -> Dict[str, Any]:
181183
tweepy.TweepyException: If there's an error posting the tweet.
182184
"""
183185
try:
186+
if media_ids and len(media_ids) > 4:
187+
raise ValueError("media_ids cannot contain more than 4 items.")
184188
self.twitter_client.create_tweet(text=tweet)
185189
self.logger.info("Tweet posted successfully.")
186190
except tweepy.TweepyException as e:
@@ -202,7 +206,7 @@ def _like_tweet(self, tweet_id: int) -> None:
202206
except tweepy.TweepyException as e:
203207
self.logger.error(f"Failed to like tweet {tweet_id}: {e}")
204208

205-
def _quote_tweet(self, tweet_id: int, quote: str) -> None:
209+
def _quote_tweet(self, tweet_id: int, quote: str, media_ids: Optional[str] = None) -> None:
206210
"""
207211
Quote a specific tweet with additional text.
208212
@@ -214,7 +218,7 @@ def _quote_tweet(self, tweet_id: int, quote: str) -> None:
214218
tweepy.TweepyException: If there's an error posting the quote tweet.
215219
"""
216220
try:
217-
self.twitter_client.create_tweet(quote_tweet_id=tweet_id, text=quote)
221+
self.twitter_client.create_tweet(quote_tweet_id=tweet_id, text=quote, media_ids=media_ids)
218222
self.logger.info(f"Successfully quoted tweet {tweet_id}.")
219223
except tweepy.TweepyException as e:
220224
self.logger.error(f"Failed to quote tweet {tweet_id}: {e}")

0 commit comments

Comments
 (0)