agent_twitter_client/
relationships.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
use crate::api::requests::request_api;
use crate::api::requests::request_form_api;
use crate::auth::user_auth::TwitterAuth;
use crate::error::{Result, TwitterError};
use crate::models::Profile;
use crate::timeline::v1::QueryProfilesResponse;
use chrono::{DateTime, Utc};
use reqwest::Method;
use serde::Deserialize;
use serde_json::{json, Value};
use reqwest::Client;
#[derive(Debug, Deserialize)]
pub struct RelationshipResponse {
    pub data: Option<RelationshipData>,
    #[serde(skip)]
    pub errors: Option<Vec<TwitterError>>,
}

#[derive(Debug, Deserialize)]
pub struct RelationshipData {
    pub user: UserRelationships,
}

#[derive(Debug, Deserialize)]
pub struct UserRelationships {
    pub result: UserResult,
}

#[derive(Debug, Deserialize)]
pub struct UserResult {
    pub timeline: Timeline,
    pub rest_id: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct Timeline {
    pub timeline: TimelineData,
}

#[derive(Debug, Deserialize)]
pub struct TimelineData {
    pub instructions: Vec<TimelineInstruction>,
}

#[derive(Debug, Deserialize)]
#[serde(tag = "type")]
pub enum TimelineInstruction {
    #[serde(rename = "TimelineAddEntries")]
    AddEntries { entries: Vec<TimelineEntry> },
    #[serde(rename = "TimelineReplaceEntry")]
    ReplaceEntry { entry: TimelineEntry },
}

#[derive(Debug, Deserialize)]
pub struct TimelineEntry {
    pub content: EntryContent,
    pub entry_id: String,
    pub sort_index: String,
}

#[derive(Debug, Deserialize)]
pub struct EntryContent {
    #[serde(rename = "itemContent")]
    pub item_content: Option<ItemContent>,
    pub cursor: Option<CursorContent>,
}

#[derive(Debug, Deserialize)]
pub struct ItemContent {
    #[serde(rename = "user_results")]
    pub user_results: Option<UserResults>,
    #[serde(rename = "userDisplayType")]
    pub user_display_type: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct UserResults {
    pub result: UserResultData,
}

#[derive(Debug, Deserialize)]
pub struct UserResultData {
    #[serde(rename = "typename")]
    pub type_name: Option<String>,
    #[serde(rename = "mediaColor")]
    pub media_color: Option<MediaColor>,
    pub id: Option<String>,
    pub rest_id: Option<String>,
    pub affiliates_highlighted_label: Option<Value>,
    pub has_graduated_access: Option<bool>,
    pub is_blue_verified: Option<bool>,
    pub profile_image_shape: Option<String>,
    pub legacy: Option<UserLegacy>,
    pub professional: Option<Professional>,
}

#[derive(Debug, Deserialize)]
pub struct MediaColor {
    pub r: Option<ColorPalette>,
}

#[derive(Debug, Deserialize)]
pub struct ColorPalette {
    pub ok: Option<Value>,
}

#[derive(Debug, Deserialize)]
pub struct UserLegacy {
    pub following: Option<bool>,
    pub followed_by: Option<bool>,
    pub screen_name: Option<String>,
    pub name: Option<String>,
    pub description: Option<String>,
    pub location: Option<String>,
    pub url: Option<String>,
    pub protected: Option<bool>,
    pub verified: Option<bool>,
    pub followers_count: Option<i32>,
    pub friends_count: Option<i32>,
    pub statuses_count: Option<i32>,
    pub listed_count: Option<i32>,
    pub created_at: Option<String>,
    pub profile_image_url_https: Option<String>,
    pub profile_banner_url: Option<String>,
    pub pinned_tweet_ids_str: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct Professional {
    pub rest_id: Option<String>,
    pub professional_type: Option<String>,
    pub category: Option<Vec<ProfessionalCategory>>,
}

#[derive(Debug, Deserialize)]
pub struct ProfessionalCategory {
    pub id: i64,
    pub name: String,
}

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

#[derive(Debug, Deserialize)]
pub struct RelationshipTimeline {
    pub data: Option<RelationshipTimelineData>,
    pub errors: Option<Vec<TwitterError>>,
}

#[derive(Debug, Deserialize)]
pub struct RelationshipTimelineData {
    pub user: UserData,
}

#[derive(Debug, Deserialize)]
pub struct UserData {
    pub result: RelationshipUserResult,
}

#[derive(Debug, Deserialize)]
pub struct RelationshipUserResult {
    pub timeline: Timeline,
}

#[derive(Debug, Deserialize)]
pub struct InnerTimeline {
    pub instructions: Vec<Instruction>,
}

#[derive(Debug, Deserialize)]
#[serde(tag = "type")]
pub enum Instruction {
    #[serde(rename = "TimelineAddEntries")]
    AddEntries {
        entries: Vec<RelationshipTimelineEntry>,
    },
    #[serde(rename = "TimelineReplaceEntry")]
    ReplaceEntry { entry: RelationshipTimelineEntry },
}

#[derive(Debug, Deserialize)]
pub struct RelationshipTimelineEntry {
    pub content: EntryContent,
    pub entry_id: String,
    pub sort_index: String,
}

#[derive(Debug, Deserialize)]
pub struct RelationshipTimelineContainer {
    pub timeline: InnerTimeline,
}

#[derive(Debug, Deserialize)]
pub struct RelationshipTimelineWrapper {
    pub timeline: InnerTimeline,
}
pub async fn get_following(
    client: &Client,
    auth: &dyn TwitterAuth,
    user_id: &str,
    count: i32,
    cursor: Option<String>,
) -> Result<(Vec<Profile>, Option<String>)> {
    let response = fetch_profile_following(client, auth, user_id, count, cursor).await?;
    Ok((response.profiles, response.next))
}
pub async fn get_followers(
    client: &Client,
    auth: &dyn TwitterAuth,
    user_id: &str,
    count: i32,
    cursor: Option<String>,
) -> Result<(Vec<Profile>, Option<String>)> {
    let response = fetch_profile_following(client, auth, user_id, count, cursor).await?;
    Ok((response.profiles, response.next))
}

pub async fn fetch_profile_following(
    client: &Client,
    auth: &dyn TwitterAuth,
    user_id: &str,
    max_profiles: i32,
    cursor: Option<String>,
) -> Result<QueryProfilesResponse> {
    let timeline = get_following_timeline(client, auth, user_id, max_profiles, cursor).await?;

    Ok(parse_relationship_timeline(&timeline))
}

async fn get_following_timeline(
    client: &Client,
    auth: &dyn TwitterAuth,
    user_id: &str,
    max_items: i32,
    cursor: Option<String>,
) -> Result<RelationshipTimeline> {

    let count = if max_items > 50 { 50 } else { max_items };

    let mut variables = json!({
        "userId": user_id,
        "count": count,
        "includePromotedContent": false,
    });

    if let Some(cursor_val) = cursor {
        if !cursor_val.is_empty() {
            variables["cursor"] = json!(cursor_val);
        }
    }

    let features = json!({
        "responsive_web_twitter_article_tweet_consumption_enabled": false,
        "tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled": true,
        "longform_notetweets_inline_media_enabled": true,
        "responsive_web_media_download_video_enabled": false,
    });

    let url = format!(
        "https://twitter.com/i/api/graphql/iSicc7LrzWGBgDPL0tM_TQ/Following?variables={}&features={}",
        urlencoding::encode(&variables.to_string()),
        urlencoding::encode(&features.to_string())
    );

    let mut headers = reqwest::header::HeaderMap::new();
    auth.install_headers(&mut headers).await?;

    let (_data, _) = request_api::<RelationshipTimeline>(client, &url, headers, Method::GET, None).await?;

    Ok(_data)
}

fn parse_relationship_timeline(timeline: &RelationshipTimeline) -> QueryProfilesResponse {
    let mut profiles = Vec::new();
    let mut next_cursor = None;
    let mut previous_cursor = None;

    if let Some(data) = &timeline.data {
        for instruction in &data.user.result.timeline.timeline.instructions {
            match instruction {
                TimelineInstruction::AddEntries { entries } => {
                    for entry in entries {
                        if let Some(item_content) = &entry.content.item_content {
                            if let Some(user_results) = &item_content.user_results {
                                if let Some(legacy) = &user_results.result.legacy {
                                    let profile = Profile {
                                        username: legacy.screen_name.clone().unwrap_or_default(),
                                        name: legacy.name.clone().unwrap_or_default(),
                                        id: user_results
                                            .result
                                            .rest_id
                                            .as_ref()
                                            .map(String::from)
                                            .unwrap_or_default(),
                                        description: legacy.description.clone(),
                                        location: legacy.location.clone(),
                                        url: legacy.url.clone(),
                                        protected: legacy.protected.unwrap_or_default(),
                                        verified: legacy.verified.unwrap_or_default(),
                                        followers_count: legacy.followers_count.unwrap_or_default(),
                                        following_count: legacy.friends_count.unwrap_or_default(),
                                        tweets_count: legacy.statuses_count.unwrap_or_default(),
                                        listed_count: legacy.listed_count.unwrap_or_default(),
                                        created_at: legacy
                                            .created_at
                                            .as_ref()
                                            .and_then(|date| {
                                                DateTime::parse_from_str(
                                                    date,
                                                    "%a %b %d %H:%M:%S %z %Y",
                                                )
                                                .ok()
                                                .map(|dt| dt.with_timezone(&Utc))
                                            })
                                            .unwrap_or_default(),
                                        profile_image_url: legacy.profile_image_url_https.clone(),
                                        profile_banner_url: legacy.profile_banner_url.clone(),
                                        pinned_tweet_id: legacy.pinned_tweet_ids_str.clone(),
                                        is_blue_verified: Some(
                                            user_results.result.is_blue_verified.unwrap_or(false),
                                        ),
                                    };

                                    profiles.push(profile);
                                }
                            }
                        } else if let Some(cursor_content) = &entry.content.cursor {
                            match cursor_content.cursor_type.as_deref() {
                                Some("Bottom") => next_cursor = Some(cursor_content.value.clone()),
                                Some("Top") => previous_cursor = Some(cursor_content.value.clone()),
                                _ => {}
                            }
                        }
                    }
                }
                TimelineInstruction::ReplaceEntry { entry } => {
                    if let Some(cursor_content) = &entry.content.cursor {
                        match cursor_content.cursor_type.as_deref() {
                            Some("Bottom") => next_cursor = Some(cursor_content.value.clone()),
                            Some("Top") => previous_cursor = Some(cursor_content.value.clone()),
                            _ => {}
                        }
                    }
                }
            }
        }
    }

    QueryProfilesResponse {
        profiles,
        next: next_cursor,
        previous: previous_cursor,
    }
}

pub async fn follow_user(client: &Client, auth: &dyn TwitterAuth, username: &str) -> Result<()> {
    let user_id = crate::profile::get_user_id_by_screen_name(client, auth, username).await?;

    let url = "https://api.twitter.com/1.1/friendships/create.json";

    let form = vec![
        (
            "include_profile_interstitial_type".to_string(),
            "1".to_string(),
        ),
        ("skip_status".to_string(), "true".to_string()),
        ("user_id".to_string(), user_id),
    ];

    let mut headers = reqwest::header::HeaderMap::new();
    auth.install_headers(&mut headers).await?;

    headers.insert(
        "Content-Type",
        "application/x-www-form-urlencoded".parse().unwrap(),
    );
    headers.insert(
        "Referer",
        format!("https://twitter.com/{}", username).parse().unwrap(),
    );
    headers.insert("X-Twitter-Active-User", "yes".parse().unwrap());
    headers.insert("X-Twitter-Auth-Type", "OAuth2Session".parse().unwrap());
    headers.insert("X-Twitter-Client-Language", "en".parse().unwrap());

    let (_, _) = request_form_api::<Value>(client, url, headers, form).await?;

    Ok(())
}

pub async fn unfollow_user(client: &Client, auth: &dyn TwitterAuth, username: &str) -> Result<()> {

    let user_id = crate::profile::get_user_id_by_screen_name(client, auth, username).await?;

    let url = "https://api.twitter.com/1.1/friendships/destroy.json";

    let form = vec![
        (
            "include_profile_interstitial_type".to_string(),
            "1".to_string(),
        ),
        ("skip_status".to_string(), "true".to_string()),
        ("user_id".to_string(), user_id),
    ];

    let mut headers = reqwest::header::HeaderMap::new();
    auth.install_headers(&mut headers).await?;

    headers.insert(
        "Content-Type",
        "application/x-www-form-urlencoded".parse().unwrap(),
    );
    headers.insert(
        "Referer",
        format!("https://twitter.com/{}", username).parse().unwrap(),
    );
    headers.insert("X-Twitter-Active-User", "yes".parse().unwrap());
    headers.insert("X-Twitter-Auth-Type", "OAuth2Session".parse().unwrap());
    headers.insert("X-Twitter-Client-Language", "en".parse().unwrap());

    let (_, _) = request_form_api::<Value>(client, url, headers, form).await?;

    Ok(())
}