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