1use crate::fields::{
2 media_fields::MediaFields, place_fields::PlaceFields, poll_fields::PollFields,
3 tweet_fields::TweetFields, user_fields::UserFields,
4};
5use crate::responses::{errors::Errors, includes::Includes, meta::Meta, tweets::Tweets};
6use crate::{
7 api::{apply_options, execute_twitter, make_url, Authentication, TwapiOptions},
8 error::Error,
9 headers::Headers,
10};
11use chrono::prelude::*;
12use itertools::Itertools;
13use reqwest::RequestBuilder;
14use serde::{Deserialize, Serialize};
15use std::collections::HashSet;
16
17const URL: &str = "/2/tweets/search/all";
18
19#[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Clone)]
20pub enum Expansions {
21 #[serde(rename = "article.cover_media")]
22 ArticleCoverMedia,
23 #[serde(rename = "article.media_entities")]
24 ArticleMediaEntities,
25 #[serde(rename = "attachments.media_keys")]
26 AttachmentsMediaKeys,
27 #[serde(rename = "attachments.media_source_tweet")]
28 AttachmentsMediaSourceTweet,
29 #[serde(rename = "attachments.poll_ids")]
30 AttachmentsPollIds,
31 #[serde(rename = "author_id")]
32 AuthorId,
33 #[serde(rename = "edit_history_tweet_ids")]
34 EditHistoryTweetIds,
35 #[serde(rename = "entities.mentions.username")]
36 EntitiesMentionsUsername,
37 #[serde(rename = "geo.place_id")]
38 GeoPlaceId,
39 #[serde(rename = "in_reply_to_user_id")]
40 InReplyToUserId,
41 #[serde(rename = "entities.note.mentions.username")]
42 EntitiesNoteMentionsUsername,
43 #[serde(rename = "referenced_tweets.id")]
44 ReferencedTweetsId,
45 #[serde(rename = "referenced_tweets.id.author_id")]
46 ReferencedTweetsIdAuthorId,
47}
48
49impl Expansions {
50 pub fn all() -> HashSet<Self> {
51 let mut result = HashSet::new();
52 result.insert(Self::ArticleCoverMedia);
53 result.insert(Self::ArticleMediaEntities);
54 result.insert(Self::AttachmentsMediaKeys);
55 result.insert(Self::AttachmentsMediaSourceTweet);
56 result.insert(Self::AttachmentsPollIds);
57 result.insert(Self::AuthorId);
58 result.insert(Self::EditHistoryTweetIds);
59 result.insert(Self::EntitiesMentionsUsername);
60 result.insert(Self::GeoPlaceId);
61 result.insert(Self::InReplyToUserId);
62 result.insert(Self::EntitiesNoteMentionsUsername);
63 result.insert(Self::ReferencedTweetsId);
64 result.insert(Self::ReferencedTweetsIdAuthorId);
65 result
66 }
67}
68
69impl std::fmt::Display for Expansions {
70 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
71 match self {
72 Self::ArticleCoverMedia => write!(f, "article.cover_media"),
73 Self::ArticleMediaEntities => write!(f, "article.media_entities"),
74 Self::AttachmentsMediaKeys => write!(f, "attachments.media_keys"),
75 Self::AttachmentsMediaSourceTweet => write!(f, "attachments.media_source_tweet"),
76 Self::AttachmentsPollIds => write!(f, "attachments.poll_ids"),
77 Self::AuthorId => write!(f, "author_id"),
78 Self::EditHistoryTweetIds => write!(f, "edit_history_tweet_ids"),
79 Self::EntitiesMentionsUsername => write!(f, "entities.mentions.username"),
80 Self::GeoPlaceId => write!(f, "geo.place_id"),
81 Self::InReplyToUserId => write!(f, "in_reply_to_user_id"),
82 Self::EntitiesNoteMentionsUsername => write!(f, "entities.note.mentions.username"),
83 Self::ReferencedTweetsId => write!(f, "referenced_tweets.id"),
84 Self::ReferencedTweetsIdAuthorId => write!(f, "referenced_tweets.id.author_id"),
85 }
86 }
87}
88
89impl Default for Expansions {
90 fn default() -> Self {
91 Self::ArticleCoverMedia
92 }
93}
94
95#[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Clone)]
96pub enum SortOrder {
97 #[serde(rename = "recency")]
98 Recency,
99 #[serde(rename = "relevancy")]
100 Relevancy,
101}
102
103impl std::fmt::Display for SortOrder {
104 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
105 match self {
106 Self::Recency => write!(f, "recency"),
107 Self::Relevancy => write!(f, "relevancy"),
108 }
109 }
110}
111
112impl Default for SortOrder {
113 fn default() -> Self {
114 Self::Recency
115 }
116}
117
118#[derive(Debug, Clone, Default)]
119pub struct Api {
120 query: String,
121 end_time: Option<DateTime<Utc>>,
122 expansions: Option<HashSet<Expansions>>,
123 max_results: Option<usize>,
124 media_fields: Option<HashSet<MediaFields>>,
125 next_token: Option<String>,
126 place_fields: Option<HashSet<PlaceFields>>,
127 poll_fields: Option<HashSet<PollFields>>,
128 since_id: Option<String>,
129 sort_order: Option<SortOrder>,
130 start_time: Option<DateTime<Utc>>,
131 tweet_fields: Option<HashSet<TweetFields>>,
132 until_id: Option<String>,
133 user_fields: Option<HashSet<UserFields>>,
134 twapi_options: Option<TwapiOptions>,
135}
136
137impl Api {
138 pub fn new(query: &str) -> Self {
139 Self {
140 query: query.to_owned(),
141 ..Default::default()
142 }
143 }
144
145 pub fn all(query: &str) -> Self {
146 Self {
147 query: query.to_owned(),
148 expansions: Some(Expansions::all()),
149 media_fields: Some(MediaFields::all()),
150 place_fields: Some(PlaceFields::all()),
151 poll_fields: Some(PollFields::all()),
152 tweet_fields: Some(TweetFields::organic()),
153 user_fields: Some(UserFields::all()),
154 max_results: Some(500),
155 ..Default::default()
156 }
157 }
158
159 pub fn open(query: &str) -> Self {
160 Self {
161 query: query.to_owned(),
162 expansions: Some(Expansions::all()),
163 media_fields: Some(MediaFields::open()),
164 place_fields: Some(PlaceFields::all()),
165 poll_fields: Some(PollFields::all()),
166 tweet_fields: Some(TweetFields::open()),
167 user_fields: Some(UserFields::all()),
168 max_results: Some(500),
169 ..Default::default()
170 }
171 }
172
173 pub fn end_time(mut self, value: DateTime<Utc>) -> Self {
174 self.end_time = Some(value);
175 self
176 }
177
178 pub fn expansions(mut self, value: HashSet<Expansions>) -> Self {
179 self.expansions = Some(value);
180 self
181 }
182
183 pub fn max_results(mut self, value: usize) -> Self {
184 self.max_results = Some(value);
185 self
186 }
187
188 pub fn media_fields(mut self, value: HashSet<MediaFields>) -> Self {
189 self.media_fields = Some(value);
190 self
191 }
192
193 pub fn next_token(mut self, value: &str) -> Self {
194 self.next_token = Some(value.to_owned());
195 self
196 }
197
198 pub fn place_fields(mut self, value: HashSet<PlaceFields>) -> Self {
199 self.place_fields = Some(value);
200 self
201 }
202
203 pub fn poll_fields(mut self, value: HashSet<PollFields>) -> Self {
204 self.poll_fields = Some(value);
205 self
206 }
207
208 pub fn since_id(mut self, value: &str) -> Self {
209 self.since_id = Some(value.to_owned());
210 self
211 }
212
213 pub fn sort_order(mut self, value: SortOrder) -> Self {
214 self.sort_order = Some(value);
215 self
216 }
217
218 pub fn start_time(mut self, value: DateTime<Utc>) -> Self {
219 self.start_time = Some(value);
220 self
221 }
222
223 pub fn tweet_fields(mut self, value: HashSet<TweetFields>) -> Self {
224 self.tweet_fields = Some(value);
225 self
226 }
227
228 pub fn until_id(mut self, value: &str) -> Self {
229 self.until_id = Some(value.to_owned());
230 self
231 }
232
233 pub fn user_fields(mut self, value: HashSet<UserFields>) -> Self {
234 self.user_fields = Some(value);
235 self
236 }
237
238 pub fn twapi_options(mut self, value: TwapiOptions) -> Self {
239 self.twapi_options = Some(value);
240 self
241 }
242
243 pub fn build(self, authentication: &impl Authentication) -> RequestBuilder {
244 let mut query_parameters = vec![];
245 query_parameters.push(("query", self.query));
246 if let Some(end_time) = self.end_time {
247 query_parameters.push(("end_time", end_time.format("%Y-%m-%dT%H%M%SZ").to_string()));
248 }
249 if let Some(expansions) = self.expansions {
250 query_parameters.push(("expansions", expansions.iter().join(",")));
251 }
252 if let Some(max_results) = self.max_results {
253 query_parameters.push(("max_results", max_results.to_string()));
254 }
255 if let Some(media_fields) = self.media_fields {
256 query_parameters.push(("media.fields", media_fields.iter().join(",")));
257 }
258 if let Some(next_token) = self.next_token {
259 query_parameters.push(("next_token", next_token));
260 }
261 if let Some(place_fields) = self.place_fields {
262 query_parameters.push(("place.fields", place_fields.iter().join(",")));
263 }
264 if let Some(poll_fields) = self.poll_fields {
265 query_parameters.push(("poll.fields", poll_fields.iter().join(",")));
266 }
267 if let Some(since_id) = self.since_id {
268 query_parameters.push(("since_id", since_id));
269 }
270 if let Some(sort_order) = self.sort_order {
271 query_parameters.push(("sort_order", sort_order.to_string()));
272 }
273 if let Some(start_time) = self.start_time {
274 query_parameters.push((
275 "start_time",
276 start_time.format("%Y-%m-%dT%H%M%SZ").to_string(),
277 ));
278 }
279 if let Some(tweet_fields) = self.tweet_fields {
280 query_parameters.push(("tweet.fields", tweet_fields.iter().join(",")));
281 }
282 if let Some(until_id) = self.until_id {
283 query_parameters.push(("until_id", until_id));
284 }
285 if let Some(user_fields) = self.user_fields {
286 query_parameters.push(("user.fields", user_fields.iter().join(",")));
287 }
288 let client = reqwest::Client::new();
289 let url = make_url(&self.twapi_options, URL);
290 let builder = client.get(&url).query(&query_parameters);
291 authentication.execute(
292 apply_options(builder, &self.twapi_options),
293 "GET",
294 &url,
295 &query_parameters
296 .iter()
297 .map(|it| (it.0, it.1.as_str()))
298 .collect::<Vec<_>>(),
299 )
300 }
301
302 pub async fn execute(
303 self,
304 authentication: &impl Authentication,
305 ) -> Result<(Response, Headers), Error> {
306 execute_twitter(self.build(authentication)).await
307 }
308}
309
310#[derive(Serialize, Deserialize, Debug, Clone, Default)]
311pub struct Response {
312 #[serde(skip_serializing_if = "Option::is_none")]
313 pub data: Option<Vec<Tweets>>,
314 #[serde(skip_serializing_if = "Option::is_none")]
315 pub errors: Option<Vec<Errors>>,
316 #[serde(skip_serializing_if = "Option::is_none")]
317 pub includes: Option<Includes>,
318 #[serde(skip_serializing_if = "Option::is_none")]
319 pub meta: Option<Meta>,
320 #[serde(flatten)]
321 pub extra: std::collections::HashMap<String, serde_json::Value>,
322}
323
324impl Response {
325 pub fn is_empty_extra(&self) -> bool {
326 let res = self.extra.is_empty()
327 && self
328 .data
329 .as_ref()
330 .map(|it| it.iter().all(|item| item.is_empty_extra()))
331 .unwrap_or(true)
332 && self
333 .errors
334 .as_ref()
335 .map(|it| it.iter().all(|item| item.is_empty_extra()))
336 .unwrap_or(true)
337 && self
338 .includes
339 .as_ref()
340 .map(|it| it.is_empty_extra())
341 .unwrap_or(true)
342 && self
343 .meta
344 .as_ref()
345 .map(|it| it.is_empty_extra())
346 .unwrap_or(true);
347 if !res {
348 println!("Response {:?}", self.extra);
349 }
350 res
351 }
352}