agent_twitter_client/timeline/
v1.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
use crate::models::tweets::Mention;
use crate::models::tweets::PlaceRaw;
use crate::models::{Profile, Tweet};
use crate::profile::LegacyUserRaw;
use crate::timeline::tweet_utils::{parse_media_groups, reconstruct_tweet_html};
use chrono::DateTime;
use chrono::Utc;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Debug, Deserialize, Serialize)]
pub struct Hashtag {
    pub text: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineUserMentionBasicRaw {
    pub id_str: Option<String>,
    pub name: Option<String>,
    pub screen_name: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineMediaBasicRaw {
    pub media_url_https: Option<String>,
    pub r#type: Option<String>,
    pub url: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineUrlBasicRaw {
    pub expanded_url: Option<String>,
    pub url: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct ExtSensitiveMediaWarningRaw {
    pub adult_content: Option<bool>,
    pub graphic_violence: Option<bool>,
    pub other: Option<bool>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct VideoVariant {
    pub bitrate: Option<i32>,
    pub url: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct VideoInfo {
    pub variants: Option<Vec<VideoVariant>>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineMediaExtendedRaw {
    pub id_str: Option<String>,
    pub media_url_https: Option<String>,
    pub ext_sensitive_media_warning: Option<ExtSensitiveMediaWarningRaw>,
    pub r#type: Option<String>,
    pub url: Option<String>,
    pub video_info: Option<VideoInfo>,
    pub ext_alt_text: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct SearchResultRaw {
    pub rest_id: Option<String>,
    pub __typename: Option<String>,
    pub core: Option<UserResultsCore>,
    pub views: Option<Views>,
    pub note_tweet: Option<NoteTweet>,
    pub quoted_status_result: Option<QuotedStatusResult>,
    pub legacy: Option<LegacyTweetRaw>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct UserResultsCore {
    pub user_results: Option<UserResults>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct UserResults {
    pub result: Option<UserResult>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct UserResult {
    pub is_blue_verified: Option<bool>,
    pub legacy: Option<LegacyUserRaw>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Views {
    pub count: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct NoteTweet {
    pub note_tweet_results: Option<NoteTweetResults>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct NoteTweetResults {
    pub result: Option<NoteTweetResult>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct NoteTweetResult {
    pub text: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct QuotedStatusResult {
    pub result: Option<Box<SearchResultRaw>>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineResultRaw {
    pub result: Option<Box<TimelineResultRaw>>,
    pub rest_id: Option<String>,
    pub __typename: Option<String>,
    pub core: Option<TimelineCore>,
    pub views: Option<TimelineViews>,
    pub note_tweet: Option<TimelineNoteTweet>,
    pub quoted_status_result: Option<Box<TimelineQuotedStatus>>,
    pub legacy: Option<Box<LegacyTweetRaw>>,
    pub tweet: Option<Box<TimelineResultRaw>>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineCore {
    pub user_results: Option<TimelineUserResults>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineUserResults {
    pub result: Option<TimelineUserResult>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineUserResult {
    pub is_blue_verified: Option<bool>,
    pub legacy: Option<LegacyUserRaw>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineViews {
    pub count: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineNoteTweet {
    pub note_tweet_results: Option<TimelineNoteTweetResults>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineNoteTweetResults {
    pub result: Option<TimelineNoteTweetResult>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineNoteTweetResult {
    pub text: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineQuotedStatus {
    pub result: Option<Box<TimelineResultRaw>>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct LegacyTweetRaw {
    pub bookmark_count: Option<i32>,
    pub conversation_id_str: Option<String>,
    pub created_at: Option<String>,
    pub favorite_count: Option<i32>,
    pub full_text: Option<String>,
    pub entities: Option<TweetEntities>,
    pub extended_entities: Option<TweetExtendedEntities>,
    pub id_str: Option<String>,
    pub in_reply_to_status_id_str: Option<String>,
    pub place: Option<PlaceRaw>,
    pub reply_count: Option<i32>,
    pub retweet_count: Option<i32>,
    pub retweeted_status_id_str: Option<String>,
    pub retweeted_status_result: Option<TimelineRetweetedStatus>,
    pub quoted_status_id_str: Option<String>,
    pub time: Option<String>,
    pub user_id_str: Option<String>,
    pub ext_views: Option<TweetExtViews>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TweetEntities {
    pub hashtags: Option<Vec<Hashtag>>,
    pub media: Option<Vec<TimelineMediaBasicRaw>>,
    pub urls: Option<Vec<TimelineUrlBasicRaw>>,
    pub user_mentions: Option<Vec<TimelineUserMentionBasicRaw>>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TweetExtendedEntities {
    pub media: Option<Vec<TimelineMediaExtendedRaw>>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineRetweetedStatus {
    pub result: Option<TimelineResultRaw>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TweetExtViews {
    pub state: Option<String>,
    pub count: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineGlobalObjectsRaw {
    pub tweets: Option<HashMap<String, Option<LegacyTweetRaw>>>,
    pub users: Option<HashMap<String, Option<LegacyUserRaw>>>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineDataRawCursor {
    pub value: Option<String>,
    pub cursor_type: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineDataRawEntity {
    pub id: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineDataRawModuleItem {
    pub client_event_info: Option<ClientEventInfo>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct ClientEventInfo {
    pub details: Option<ClientEventDetails>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct ClientEventDetails {
    pub guide_details: Option<GuideDetails>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct GuideDetails {
    pub transparent_guide_details: Option<TransparentGuideDetails>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TransparentGuideDetails {
    pub trend_metadata: Option<TrendMetadata>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TrendMetadata {
    pub trend_name: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineDataRawAddEntry {
    pub content: Option<TimelineEntryContent>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineDataRawPinEntry {
    pub content: Option<TimelinePinContent>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelinePinContent {
    pub item: Option<TimelineItem>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineDataRawReplaceEntry {
    pub content: Option<TimelineReplaceContent>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineReplaceContent {
    pub operation: Option<TimelineOperation>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineDataRawInstruction {
    pub add_entries: Option<TimelineAddEntries>,
    pub pin_entry: Option<TimelineDataRawPinEntry>,
    pub replace_entry: Option<TimelineDataRawReplaceEntry>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineAddEntries {
    pub entries: Option<Vec<TimelineDataRawAddEntry>>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineDataRaw {
    pub instructions: Option<Vec<TimelineDataRawInstruction>>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineV1 {
    pub global_objects: Option<TimelineGlobalObjectsRaw>,
    pub timeline: Option<TimelineDataRaw>,
}

#[derive(Debug)]
pub enum ParseTweetResult {
    Success { tweet: Tweet },
    Error { err: String },
}

#[derive(Debug, Serialize, Deserialize)]
pub struct QueryTweetsResponse {
    pub tweets: Vec<Tweet>,
    pub next: Option<String>,
    pub previous: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct QueryProfilesResponse {
    pub profiles: Vec<Profile>,
    pub next: Option<String>,
    pub previous: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineEntryContent {
    pub item: Option<TimelineItem>,
    pub operation: Option<TimelineOperation>,
    pub timeline_module: Option<TimelineModule>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineItem {
    pub content: Option<TimelineContent>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineContent {
    pub tweet: Option<TimelineDataRawEntity>,
    pub user: Option<TimelineDataRawEntity>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineOperation {
    pub cursor: Option<TimelineDataRawCursor>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineModule {
    pub items: Option<Vec<TimelineModuleItemWrapper>>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TimelineModuleItemWrapper {
    pub item: Option<TimelineDataRawModuleItem>,
}

#[derive(Debug)]
pub struct UserMention {
    pub id: String,
    pub username: String,
    pub name: String,
}

pub fn parse_timeline_tweet(timeline: &TimelineV1, id: &str) -> ParseTweetResult {
    let empty_tweets = HashMap::new();
    let tweets = match &timeline.global_objects {
        Some(go) => go.tweets.as_ref().unwrap_or(&empty_tweets),
        None => {
            return ParseTweetResult::Error {
                err: "No global objects found".to_string(),
            }
        }
    };

    let tweet = match tweets.get(id) {
        Some(Some(t)) => t,
        _ => {
            return ParseTweetResult::Error {
                err: format!("Tweet \"{}\" was not found in the timeline object.", id),
            }
        }
    };

    let user_id = match &tweet.user_id_str {
        Some(id) => id,
        None => {
            return ParseTweetResult::Error {
                err: "Tweet has no user ID".to_string(),
            }
        }
    };

    let empty_users = HashMap::new();
    let users = match &timeline.global_objects {
        Some(go) => go.users.as_ref().unwrap_or(&empty_users),
        None => {
            return ParseTweetResult::Error {
                err: "No users found".to_string(),
            }
        }
    };

    let user = match users.get(user_id) {
        Some(Some(u)) => u,
        _ => {
            return ParseTweetResult::Error {
                err: format!("User \"{}\" has no username data.", user_id),
            }
        }
    };

    let hashtags = tweet
        .entities
        .as_ref()
        .and_then(|e| e.hashtags.as_ref())
        .map(|h| h.iter().filter_map(|tag| tag.text.clone()).collect())
        .unwrap_or_default();

    let mentions = tweet
        .entities
        .as_ref()
        .and_then(|e| e.user_mentions.as_ref())
        .map(|m| {
            m.iter()
                .filter_map(|mention| {
                    if let (Some(id), Some(screen_name), Some(name)) =
                        (&mention.id_str, &mention.screen_name, &mention.name)
                    {
                        Some(Mention {
                            id: id.clone(),
                            username: Some(screen_name.clone()),
                            name: Some(name.clone()),
                        })
                    } else {
                        None
                    }
                })
                .collect()
        })
        .unwrap_or_default();

    let empty_media = Vec::new();
    let media = tweet
        .extended_entities
        .as_ref()
        .and_then(|e| e.media.as_ref())
        .unwrap_or(&empty_media);

    let urls = tweet
        .entities
        .as_ref()
        .and_then(|e| e.urls.as_ref())
        .map(|u| {
            u.iter()
                .filter_map(|url| url.expanded_url.clone())
                .collect()
        })
        .unwrap_or_default();

    let (photos, videos, sensitive_content) = parse_media_groups(media);

    let mut tweet_obj = Tweet {
        conversation_id: tweet.conversation_id_str.clone(),
        id: Some(id.to_string()),
        hashtags,
        likes: tweet.favorite_count,
        mentions,
        name: user.name.clone(),
        permanent_url: Some(format!(
            "https://twitter.com/{}/status/{}",
            user.screen_name.as_ref().unwrap_or(&String::new()),
            id
        )),
        photos,
        replies: tweet.reply_count,
        retweets: tweet.retweet_count,
        text: tweet.full_text.clone(),
        thread: Vec::new(),
        urls,
        user_id: tweet.user_id_str.clone(),
        username: user.screen_name.clone(),
        videos,
        time_parsed: None,
        timestamp: None,
        place: None,
        is_quoted: Some(false),
        quoted_status_id: None,
        quoted_status: None,
        is_reply: Some(false),
        in_reply_to_status_id: None,
        in_reply_to_status: None,
        is_retweet: Some(false),
        retweeted_status_id: None,
        retweeted_status: None,
        views: None,
        is_pin: Some(false),
        sensitive_content: Some(sensitive_content),
        html: None,
        bookmark_count: None,
        is_self_thread: None,
        poll: None,
        created_at: None,
        ext_views: None,
        quote_count: None,
        reply_count: None,
        retweet_count: None,
        screen_name: None,
        thread_id: None,
    };

    if let Some(created_at) = &tweet.created_at {
        if let Ok(parsed_time) = DateTime::parse_from_str(created_at, "%a %b %d %H:%M:%S %z %Y") {
            tweet_obj.time_parsed = Some(parsed_time.with_timezone(&Utc));
            tweet_obj.timestamp = Some(parsed_time.timestamp());
        }
    }

    if let Some(place) = &tweet.place {
        tweet_obj.place = Some(place.clone());
    }

    if let Some(quoted_id) = &tweet.quoted_status_id_str {
        tweet_obj.is_quoted = Some(true);
        tweet_obj.quoted_status_id = Some(quoted_id.clone());

        if let ParseTweetResult::Success {
            tweet: quoted_tweet,
        } = parse_timeline_tweet(timeline, quoted_id)
        {
            tweet_obj.quoted_status = Some(Box::new(quoted_tweet));
        }
    }

    if let Some(ext_views) = &tweet.ext_views {
        if let Some(count) = &ext_views.count {
            if let Ok(views) = count.parse::<i32>() {
                tweet_obj.views = Some(views);
            }
        }
    }

    tweet_obj.html = reconstruct_tweet_html(tweet, &tweet_obj.photos, &tweet_obj.videos);

    ParseTweetResult::Success { tweet: tweet_obj }
}