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