Skip to main content

twapi_v2/api/
get_2_tweets_search_all.rs

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