Skip to main content

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