Skip to main content

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