twitter-openapi-python 0.1.0__py3-none-any.whl → 0.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,8 @@
2
2
 
3
3
  # flake8: noqa
4
4
 
5
+ from twitter_openapi_python.api.birdwatch_api import BirdwatchApiUtils
6
+ from twitter_openapi_python.api.community_api import CommunityApiUtils
5
7
  from twitter_openapi_python.api.default_api import DefaultApiUtils
6
8
  from twitter_openapi_python.api.initial_state_api import InitialStateApiUtils
7
9
  from twitter_openapi_python.api.post_api import PostApiUtils
@@ -0,0 +1,116 @@
1
+ from typing import Any, Optional
2
+
3
+ import twitter_openapi_python_generated as twitter
4
+ import twitter_openapi_python_generated.models as models
5
+ from x_client_transaction import ClientTransaction
6
+
7
+ from twitter_openapi_python.models import (
8
+ TimelineApiUtilsResponse,
9
+ TweetApiUtilsData,
10
+ TwitterApiUtilsResponse,
11
+ )
12
+ from twitter_openapi_python.utils import (
13
+ data_request,
14
+ error_check,
15
+ timeline_request,
16
+ tweet_entries_converter,
17
+ )
18
+
19
+ ResponseType = TwitterApiUtilsResponse[TimelineApiUtilsResponse[TweetApiUtilsData]]
20
+ ParamType = dict[str, Any]
21
+
22
+
23
+ class BirdwatchApiUtils:
24
+ """Community Notes."""
25
+
26
+ api: twitter.BirdwatchApi
27
+ flag: ParamType
28
+
29
+ def __init__(self, api: twitter.BirdwatchApi, flag: ParamType, ct: ClientTransaction):
30
+ self.api = api
31
+ self.flag = flag
32
+ self.ct = ct
33
+
34
+ def get_birdwatch_fetch_alias_self_select_status(
35
+ self,
36
+ extra_param: Optional[ParamType] = None,
37
+ ) -> TwitterApiUtilsResponse[models.BirdwatchAliasSelfSelectStatus]:
38
+ param: ParamType = {}
39
+ if extra_param is not None:
40
+ param.update(extra_param)
41
+ return data_request(
42
+ self.api.get_birdwatch_fetch_alias_self_select_status_with_http_info,
43
+ self.flag["BirdwatchFetchAliasSelfSelectStatus"],
44
+ self.ct,
45
+ lambda e: error_check(e.data, e.errors).authenticated_user_birdwatch_alias_self_select_status,
46
+ param,
47
+ )
48
+
49
+ def get_birdwatch_fetch_authenticated_user_profile(
50
+ self,
51
+ extra_param: Optional[ParamType] = None,
52
+ ) -> TwitterApiUtilsResponse[models.BirdwatchProfile]:
53
+ param: ParamType = {}
54
+ if extra_param is not None:
55
+ param.update(extra_param)
56
+ return data_request(
57
+ self.api.get_birdwatch_fetch_authenticated_user_profile_with_http_info,
58
+ self.flag["BirdwatchFetchAuthenticatedUserProfile"],
59
+ self.ct,
60
+ lambda e: error_check(e.data, e.errors).authenticated_user_birdwatch_profile,
61
+ param,
62
+ )
63
+
64
+ def get_birdwatch_fetch_birdwatch_profile(
65
+ self,
66
+ alias: str,
67
+ extra_param: Optional[ParamType] = None,
68
+ ) -> TwitterApiUtilsResponse[models.BirdwatchProfile]:
69
+ param: ParamType = {"alias": alias}
70
+ if extra_param is not None:
71
+ param.update(extra_param)
72
+ return data_request(
73
+ self.api.get_birdwatch_fetch_birdwatch_profile_with_http_info,
74
+ self.flag["BirdwatchFetchBirdwatchProfile"],
75
+ self.ct,
76
+ lambda e: error_check(e.data, e.errors).birdwatch_profile_by_alias,
77
+ param,
78
+ )
79
+
80
+ def get_birdwatch_fetch_global_timeline(
81
+ self,
82
+ cursor: Optional[str] = None,
83
+ extra_param: Optional[ParamType] = None,
84
+ ) -> ResponseType:
85
+ """Posts with notes on the Community Notes home page (first tab)."""
86
+ param: ParamType = {}
87
+ if cursor is not None:
88
+ param["cursor"] = cursor
89
+ if extra_param is not None:
90
+ param.update(extra_param)
91
+ return timeline_request(
92
+ self.api.get_birdwatch_fetch_global_timeline_with_http_info,
93
+ self.flag["BirdwatchFetchGlobalTimeline"],
94
+ self.ct,
95
+ lambda e: error_check(
96
+ e.data.viewer.birdwatch_home_page.body.initial_timeline.timeline.timeline, e.errors
97
+ ).instructions,
98
+ param,
99
+ tweet_entries_converter,
100
+ )
101
+
102
+ def get_birdwatch_fetch_public_data(
103
+ self,
104
+ extra_param: Optional[ParamType] = None,
105
+ ) -> TwitterApiUtilsResponse[models.BirdwatchPublicDataFileBundle]:
106
+ """Download urls of the latest public data files."""
107
+ param: ParamType = {}
108
+ if extra_param is not None:
109
+ param.update(extra_param)
110
+ return data_request(
111
+ self.api.get_birdwatch_fetch_public_data_with_http_info,
112
+ self.flag["BirdwatchFetchPublicData"],
113
+ self.ct,
114
+ lambda e: error_check(e.data, e.errors).birdwatch_latest_public_data_file_bundle,
115
+ param,
116
+ )
@@ -0,0 +1,149 @@
1
+ from typing import Any, List, Optional
2
+
3
+ import twitter_openapi_python_generated as twitter
4
+ import twitter_openapi_python_generated.models as models
5
+ from x_client_transaction import ClientTransaction
6
+
7
+ from twitter_openapi_python.models import (
8
+ TimelineApiUtilsResponse,
9
+ TweetApiUtilsData,
10
+ TwitterApiUtilsResponse,
11
+ )
12
+ from twitter_openapi_python.utils import (
13
+ data_request,
14
+ error_check,
15
+ timeline_request,
16
+ tweet_entries_converter,
17
+ )
18
+
19
+ ResponseType = TwitterApiUtilsResponse[TimelineApiUtilsResponse[TweetApiUtilsData]]
20
+ ParamType = dict[str, Any]
21
+
22
+
23
+ class CommunityApiUtils:
24
+ api: twitter.CommunityApi
25
+ flag: ParamType
26
+
27
+ def __init__(self, api: twitter.CommunityApi, flag: ParamType, ct: ClientTransaction):
28
+ self.api = api
29
+ self.flag = flag
30
+ self.ct = ct
31
+
32
+ def get_communities_fetch_one_query(
33
+ self,
34
+ community_id: str,
35
+ extra_param: Optional[ParamType] = None,
36
+ ) -> TwitterApiUtilsResponse[models.CommunityPageCommunity]:
37
+ """The community page (full members and moderators)."""
38
+ param: ParamType = {"communityId": community_id}
39
+ if extra_param is not None:
40
+ param.update(extra_param)
41
+ return data_request(
42
+ self.api.get_communities_fetch_one_query_with_http_info,
43
+ self.flag["CommunitiesFetchOneQuery"],
44
+ self.ct,
45
+ lambda e: error_check(e.data, e.errors).community_results.result,
46
+ param,
47
+ )
48
+
49
+ def get_community_query(
50
+ self,
51
+ community_id: str,
52
+ extra_param: Optional[ParamType] = None,
53
+ ) -> TwitterApiUtilsResponse[models.CommunitySettingsCommunity]:
54
+ """The community settings / moderation pages (partial users, trending hashtags)."""
55
+ param: ParamType = {"communityId": community_id}
56
+ if extra_param is not None:
57
+ param.update(extra_param)
58
+ return data_request(
59
+ self.api.get_community_query_with_http_info,
60
+ self.flag["CommunityQuery"],
61
+ self.ct,
62
+ lambda e: error_check(e.data, e.errors).community_results.result,
63
+ param,
64
+ )
65
+
66
+ def get_communities_search_query(
67
+ self,
68
+ query: str,
69
+ cursor: Optional[str] = None,
70
+ extra_param: Optional[ParamType] = None,
71
+ ) -> TwitterApiUtilsResponse[models.CommunitiesSearchSlice]:
72
+ """Community cards for a query; the next page is slice_info.next_cursor."""
73
+ param: ParamType = {"query": query}
74
+ if cursor is not None:
75
+ param["cursor"] = cursor
76
+ if extra_param is not None:
77
+ param.update(extra_param)
78
+ return data_request(
79
+ self.api.get_communities_search_query_with_http_info,
80
+ self.flag["CommunitiesSearchQuery"],
81
+ self.ct,
82
+ lambda e: error_check(e.data, e.errors).communities_search_slice,
83
+ param,
84
+ )
85
+
86
+ def get_topic_carousel_query(
87
+ self,
88
+ extra_param: Optional[ParamType] = None,
89
+ ) -> TwitterApiUtilsResponse[List[models.CommunityTopic]]:
90
+ param: ParamType = {}
91
+ if extra_param is not None:
92
+ param.update(extra_param)
93
+ return data_request(
94
+ self.api.get_topic_carousel_query_with_http_info,
95
+ self.flag["TopicCarouselQuery"],
96
+ self.ct,
97
+ lambda e: error_check(e.data, e.errors).community_topic_results.community_topics,
98
+ param,
99
+ )
100
+
101
+ def get_global_communities_post_search_timeline(
102
+ self,
103
+ raw_query: str,
104
+ cursor: Optional[str] = None,
105
+ count: Optional[int] = None,
106
+ extra_param: Optional[ParamType] = None,
107
+ ) -> ResponseType:
108
+ """Posts across communities (Top)."""
109
+ param: ParamType = {"rawQuery": raw_query}
110
+ if cursor is not None:
111
+ param["cursor"] = cursor
112
+ if count is not None:
113
+ param["count"] = count
114
+ if extra_param is not None:
115
+ param.update(extra_param)
116
+ return timeline_request(
117
+ self.api.get_global_communities_post_search_timeline_with_http_info,
118
+ self.flag["GlobalCommunitiesPostSearchTimeline"],
119
+ self.ct,
120
+ lambda e: error_check(e.data.search_by_raw_query.communities_posts_search_page.timeline, e.errors).instructions,
121
+ param,
122
+ tweet_entries_converter,
123
+ )
124
+
125
+ def get_global_communities_latest_post_search_timeline(
126
+ self,
127
+ raw_query: str,
128
+ cursor: Optional[str] = None,
129
+ count: Optional[int] = None,
130
+ extra_param: Optional[ParamType] = None,
131
+ ) -> ResponseType:
132
+ """Posts across communities (Latest)."""
133
+ param: ParamType = {"rawQuery": raw_query}
134
+ if cursor is not None:
135
+ param["cursor"] = cursor
136
+ if count is not None:
137
+ param["count"] = count
138
+ if extra_param is not None:
139
+ param.update(extra_param)
140
+ return timeline_request(
141
+ self.api.get_global_communities_latest_post_search_timeline_with_http_info,
142
+ self.flag["GlobalCommunitiesLatestPostSearchTimeline"],
143
+ self.ct,
144
+ lambda e: error_check(
145
+ e.data.search_by_raw_query.communities_latest_posts_search_page.timeline, e.errors
146
+ ).instructions,
147
+ param,
148
+ tweet_entries_converter,
149
+ )
@@ -78,3 +78,78 @@ class DefaultApiUtils:
78
78
  if response.data is None:
79
79
  raise Exception("No tweet")
80
80
  return response # type: ignore
81
+
82
+ def get_authenticate_periscope(
83
+ self,
84
+ extra_param: Optional[ParamType] = None,
85
+ ) -> TwitterApiUtilsResponse[str]:
86
+ """A Periscope (live video) token for the account."""
87
+ param: ParamType = {}
88
+ if extra_param is not None:
89
+ param.update(extra_param)
90
+ return self.request(
91
+ apiFn=self.api.get_authenticate_periscope_with_http_info,
92
+ convertFn=lambda x: error_check(x.data, x.errors).authenticate_periscope,
93
+ key="AuthenticatePeriscope",
94
+ param=param,
95
+ )
96
+
97
+ def get_creator_studio_tab_bar_item_query(
98
+ self,
99
+ extra_param: Optional[ParamType] = None,
100
+ ) -> TwitterApiUtilsResponse[models.CreatorStudioTabBarItemQueryResponseData]:
101
+ param: ParamType = {}
102
+ if extra_param is not None:
103
+ param.update(extra_param)
104
+ return self.request(
105
+ apiFn=self.api.get_creator_studio_tab_bar_item_query_with_http_info,
106
+ convertFn=lambda x: error_check(x.data, x.errors),
107
+ key="CreatorStudioTabBarItemQuery",
108
+ param=param,
109
+ )
110
+
111
+ def get_data_saver_mode(
112
+ self,
113
+ device_id: Optional[str] = None,
114
+ extra_param: Optional[ParamType] = None,
115
+ ) -> TwitterApiUtilsResponse[models.DataSaverModeResponseData]:
116
+ param: ParamType = {}
117
+ if device_id is not None:
118
+ param["device_id"] = device_id
119
+ if extra_param is not None:
120
+ param.update(extra_param)
121
+ return self.request(
122
+ apiFn=self.api.get_data_saver_mode_with_http_info,
123
+ convertFn=lambda x: error_check(x.data, x.errors),
124
+ key="DataSaverMode",
125
+ param=param,
126
+ )
127
+
128
+ def get_grok_home(
129
+ self,
130
+ extra_param: Optional[ParamType] = None,
131
+ ) -> TwitterApiUtilsResponse[models.GrokHome]:
132
+ """Grok model options, prompts and eligibility."""
133
+ param: ParamType = {}
134
+ if extra_param is not None:
135
+ param.update(extra_param)
136
+ return self.request(
137
+ apiFn=self.api.get_grok_home_with_http_info,
138
+ convertFn=lambda x: error_check(x.data, x.errors).grok_home,
139
+ key="GrokHome",
140
+ param=param,
141
+ )
142
+
143
+ def get_pinned_timelines(
144
+ self,
145
+ extra_param: Optional[ParamType] = None,
146
+ ) -> TwitterApiUtilsResponse[models.PinnedTimelines]:
147
+ param: ParamType = {}
148
+ if extra_param is not None:
149
+ param.update(extra_param)
150
+ return self.request(
151
+ apiFn=self.api.get_pinned_timelines_with_http_info,
152
+ convertFn=lambda x: error_check(x.data, x.errors).pinned_timelines,
153
+ key="PinnedTimelines",
154
+ param=param,
155
+ )
@@ -164,3 +164,164 @@ class PostApiUtils:
164
164
  _headers=get_headers(self.flag["UnfavoriteTweet"], self.ct),
165
165
  )
166
166
  return build_response(res, res.data)
167
+
168
+ def post_create_bookmark(
169
+ self,
170
+ tweet_id: str,
171
+ ) -> ResponseType[models.CreateBookmarkResponse]:
172
+ variables = non_nullable(
173
+ twitter.PostCreateBookmarkRequestVariables.from_dict(self.flag["CreateBookmark"]["variables"])
174
+ )
175
+ variables.tweet_id = tweet_id
176
+
177
+ res = self.api.post_create_bookmark_with_http_info(
178
+ path_query_id=self.flag["CreateBookmark"]["queryId"],
179
+ post_create_bookmark_request=twitter.PostCreateBookmarkRequest(
180
+ queryId=self.flag["CreateBookmark"]["queryId"],
181
+ variables=variables,
182
+ ),
183
+ _headers=get_headers(self.flag["CreateBookmark"], self.ct),
184
+ )
185
+ return build_response(res, res.data)
186
+
187
+ def post_delete_bookmark(
188
+ self,
189
+ tweet_id: str,
190
+ ) -> ResponseType[models.DeleteBookmarkResponse]:
191
+ variables = non_nullable(
192
+ twitter.PostCreateBookmarkRequestVariables.from_dict(self.flag["DeleteBookmark"]["variables"])
193
+ )
194
+ variables.tweet_id = tweet_id
195
+
196
+ res = self.api.post_delete_bookmark_with_http_info(
197
+ path_query_id=self.flag["DeleteBookmark"]["queryId"],
198
+ post_delete_bookmark_request=twitter.PostDeleteBookmarkRequest(
199
+ queryId=self.flag["DeleteBookmark"]["queryId"],
200
+ variables=variables,
201
+ ),
202
+ _headers=get_headers(self.flag["DeleteBookmark"], self.ct),
203
+ )
204
+ return build_response(res, res.data)
205
+
206
+ def post_create_list(
207
+ self,
208
+ name: str,
209
+ description: str = "",
210
+ is_private: bool = False,
211
+ ) -> ResponseType[models.ListResponse]:
212
+ variables = non_nullable(twitter.PostCreateListRequestVariables.from_dict(self.flag["CreateList"]["variables"]))
213
+ features = non_nullable(twitter.PostCreateListRequestFeatures.from_dict(self.flag["CreateList"]["features"]))
214
+ variables.name = name
215
+ variables.description = description
216
+ variables.is_private = is_private
217
+
218
+ res = self.api.post_create_list_with_http_info(
219
+ path_query_id=self.flag["CreateList"]["queryId"],
220
+ post_create_list_request=twitter.PostCreateListRequest(
221
+ queryId=self.flag["CreateList"]["queryId"],
222
+ variables=variables,
223
+ features=features,
224
+ ),
225
+ _headers=get_headers(self.flag["CreateList"], self.ct),
226
+ )
227
+ return build_response(res, res.data)
228
+
229
+ def post_delete_list(
230
+ self,
231
+ list_id: str,
232
+ ) -> ResponseType[models.DeleteListResponse]:
233
+ variables = non_nullable(twitter.PostDeleteListRequestVariables.from_dict(self.flag["DeleteList"]["variables"]))
234
+ variables.list_id = list_id
235
+
236
+ res = self.api.post_delete_list_with_http_info(
237
+ path_query_id=self.flag["DeleteList"]["queryId"],
238
+ post_delete_list_request=twitter.PostDeleteListRequest(
239
+ queryId=self.flag["DeleteList"]["queryId"],
240
+ variables=variables,
241
+ ),
242
+ _headers=get_headers(self.flag["DeleteList"], self.ct),
243
+ )
244
+ return build_response(res, res.data)
245
+
246
+ def post_list_add_member(
247
+ self,
248
+ list_id: str,
249
+ user_id: str,
250
+ ) -> ResponseType[models.ListResponse]:
251
+ variables = non_nullable(
252
+ twitter.PostListAddMemberRequestVariables.from_dict(self.flag["ListAddMember"]["variables"])
253
+ )
254
+ features = non_nullable(twitter.PostCreateListRequestFeatures.from_dict(self.flag["ListAddMember"]["features"]))
255
+ variables.list_id = list_id
256
+ variables.user_id = user_id
257
+
258
+ res = self.api.post_list_add_member_with_http_info(
259
+ path_query_id=self.flag["ListAddMember"]["queryId"],
260
+ post_list_add_member_request=twitter.PostListAddMemberRequest(
261
+ queryId=self.flag["ListAddMember"]["queryId"],
262
+ variables=variables,
263
+ features=features,
264
+ ),
265
+ _headers=get_headers(self.flag["ListAddMember"], self.ct),
266
+ )
267
+ return build_response(res, res.data)
268
+
269
+ def post_list_remove_member(
270
+ self,
271
+ list_id: str,
272
+ user_id: str,
273
+ ) -> ResponseType[models.ListResponse]:
274
+ variables = non_nullable(
275
+ twitter.PostListAddMemberRequestVariables.from_dict(self.flag["ListRemoveMember"]["variables"])
276
+ )
277
+ features = non_nullable(
278
+ twitter.PostCreateListRequestFeatures.from_dict(self.flag["ListRemoveMember"]["features"])
279
+ )
280
+ variables.list_id = list_id
281
+ variables.user_id = user_id
282
+
283
+ res = self.api.post_list_remove_member_with_http_info(
284
+ path_query_id=self.flag["ListRemoveMember"]["queryId"],
285
+ post_list_remove_member_request=twitter.PostListRemoveMemberRequest(
286
+ queryId=self.flag["ListRemoveMember"]["queryId"],
287
+ variables=variables,
288
+ features=features,
289
+ ),
290
+ _headers=get_headers(self.flag["ListRemoveMember"], self.ct),
291
+ )
292
+ return build_response(res, res.data)
293
+
294
+ def post_create_draft_tweet(
295
+ self,
296
+ status: str,
297
+ ) -> ResponseType[models.CreateDraftTweetResponse]:
298
+ variables = non_nullable(
299
+ twitter.PostCreateDraftTweetRequestVariables.from_dict(self.flag["CreateDraftTweet"]["variables"])
300
+ )
301
+ variables.post_tweet_request.status = status
302
+
303
+ res = self.api.post_create_draft_tweet_with_http_info(
304
+ path_query_id=self.flag["CreateDraftTweet"]["queryId"],
305
+ post_create_draft_tweet_request=twitter.PostCreateDraftTweetRequest(
306
+ queryId=self.flag["CreateDraftTweet"]["queryId"],
307
+ variables=variables,
308
+ ),
309
+ _headers=get_headers(self.flag["CreateDraftTweet"], self.ct),
310
+ )
311
+ return build_response(res, res.data)
312
+
313
+ def post_delete_draft_tweets(
314
+ self,
315
+ draft_tweet_ids: list[str],
316
+ ) -> ResponseType[models.DeleteDraftTweetsResponse]:
317
+ variables = twitter.PostDeleteDraftTweetsRequestVariables(draft_tweet_ids=draft_tweet_ids)
318
+
319
+ res = self.api.post_delete_draft_tweets_with_http_info(
320
+ path_query_id=self.flag["DeleteDraftTweets"]["queryId"],
321
+ post_delete_draft_tweets_request=twitter.PostDeleteDraftTweetsRequest(
322
+ queryId=self.flag["DeleteDraftTweets"]["queryId"],
323
+ variables=variables,
324
+ ),
325
+ _headers=get_headers(self.flag["DeleteDraftTweets"], self.ct),
326
+ )
327
+ return build_response(res, res.data)