instagram-scraper-rs 0.1.0

Scrapes an instagram user's photos and videos
Documentation
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
554
555
556
557
558
559
560
561
562
563
//! # Session
//!
//! This module exposes the session for the instagram client

use crate::{types::Comment, Authentication, InstagramScraperError, InstagramScraperResult, Post};

use reqwest::{header, Client, ClientBuilder, Response};

mod requests;
use requests::{
    BASE_URL, CHROME_WIN_USER_AGENT, LOGIN_URL, LOGOUT_URL, STORIES_USER_AGENT, X_CSRF_TOKEN,
};

pub use crate::{Stories, Story, User};

const DEFAULT_POST_AMOUNT: usize = 50;
const DEFAULT_COMMENTS_AMOUNT: usize = 50;

/// The session is a storage for values required by the instagram client to work.
/// It also exposes the instagram HTTP client
#[derive(Debug)]
pub struct Session {
    csrftoken: Option<String>,
    client: Client,
}

impl Default for Session {
    fn default() -> Self {
        Self {
            csrftoken: None,
            client: ClientBuilder::new()
                .cookie_store(true)
                .user_agent(STORIES_USER_AGENT)
                .build()
                .unwrap(),
        }
    }
}

impl Session {
    /// Login into instagram account or as a guest based on provided authentication type
    pub(crate) async fn login(
        &mut self,
        authentication: Authentication,
    ) -> InstagramScraperResult<()> {
        let token = match authentication {
            Authentication::Guest => self.login_as_guest().await?,
            Authentication::UsernamePassword { username, password } => {
                self.login_as_user(username, password).await?
            }
        };
        debug!("login successful; csrf token: {}", token);
        self.csrftoken = Some(token);
        Ok(())
    }

    /// Scrape profile picture for provided username.
    ///
    /// Returns the image url
    pub async fn scrape_profile_pic(
        &mut self,
        user_id: &str,
    ) -> InstagramScraperResult<Option<String>> {
        self.restrict_authed()?;
        debug!("collecting profile pic for {}", user_id);
        let response = self
            .client
            .get(format!(
                "https://i.instagram.com/api/v1/users/{}/info/",
                user_id
            ))
            .send()
            .await?;
        Self::restrict_successful(&response)?;
        self.update_csrftoken(&response);
        let user_info = response
            .text()
            .await
            .map(|t| serde_json::from_str::<requests::UserInfoResponse>(&t).map(|u| u.user))?;
        let user_info = user_info?;
        if user_info.has_anonymous_profile_picture.unwrap_or_default() {
            debug!("user has anonymous profile picture");
            return Ok(None);
        }
        if let Some(url) = user_info.hd_profile_pic_url_info.url {
            debug!("found hd profile pic {}", url);
            Ok(Some(url.replace("\\u0026", "&")))
        } else {
            debug!("searching user profile pic in versions");
            Ok(user_info.hd_profile_pic_versions.and_then(|images| {
                images
                    .into_iter()
                    .rev()
                    .find_map(|img| img.url.map(|x| x.replace("\\u0026", "&")))
            }))
        }
    }

    /// Scrape shared data for user
    pub async fn scrape_shared_data_userinfo(
        &mut self,
        username: &str,
    ) -> InstagramScraperResult<User> {
        self.restrict_authed()?;
        debug!("collecting user info for {}", username);
        let response = self
            .client
            .get(format!(
                "https://i.instagram.com/api/v1/users/web_profile_info/?username={}",
                username
            ))
            .send()
            .await?;
        Self::restrict_successful(&response)?;
        self.update_csrftoken(&response);
        match response
            .text()
            .await
            .map(|t| serde_json::from_str::<requests::WebProfileResponse>(&t).map(|i| i.data.user))
        {
            Err(err) => Err(err.into()),
            Ok(Ok(user)) => Ok(user),
            Ok(Err(err)) => Err(err.into()),
        }
    }

    /// Scrape user stories
    pub async fn scrape_stories(
        &mut self,
        user_id: &str,
        max_highlight_stories: usize,
    ) -> InstagramScraperResult<Stories> {
        self.restrict_authed()?;
        debug!("collecting stories for {}", user_id);
        let main_stories = self.fetch_stories(format!("{}graphql/query/?query_hash=45246d3fe16ccc6577e0bd297a5db1ab&variables=%7B%22reel_ids%22%3A%5B%22{}%22%5D%2C%22tag_names%22%3A%5B%5D%2C%22location_ids%22%3A%5B%5D%2C%22highlight_reel_ids%22%3A%5B%5D%2C%22precomposed_overlay%22%3Afalse%7D", BASE_URL, user_id))
            .await?;
        debug!("collected main stories; collecting highlight stories");
        // fetch highlight stories
        if max_highlight_stories == 0 {
            warn!("max_highlight_stories is 0; return empty vector");
            return Ok(Stories {
                main_stories,
                highlight_stories: vec![],
            });
        }
        let highlight_stories_ids = self.fetch_highlighted_stories_ids(user_id).await?;
        debug!(
            "found {} ids for highlighted stories",
            highlight_stories_ids.len()
        );
        let mut highlight_stories = Vec::with_capacity(highlight_stories_ids.len());
        for chunk in highlight_stories_ids.chunks(3) {
            let id = chunk.join("%22%2C%22");
            debug!("fetching stories in chunk {}", id);
            highlight_stories.extend(
                self.fetch_stories(format!("{}graphql/query/?query_hash=45246d3fe16ccc6577e0bd297a5db1ab&variables=%7B%22reel_ids%22%3A%5B%5D%2C%22tag_names%22%3A%5B%5D%2C%22location_ids%22%3A%5B%5D%2C%22highlight_reel_ids%22%3A%5B%22{}%22%5D%2C%22precomposed_overlay%22%3Afalse%7D", BASE_URL, id)).await?
            );
            if highlight_stories.len() >= max_highlight_stories {
                debug!("reached maximum amount of highlight stories; leaving loop");
                break;
            }
        }
        // remove exceeding items
        if highlight_stories.len() > max_highlight_stories {
            let curlen = highlight_stories.len();
            for i in curlen..max_highlight_stories {
                highlight_stories.remove(i);
            }
        }

        Ok(Stories {
            main_stories,
            highlight_stories,
        })
    }

    /// Scrape posts published by user associated to `user_id`.
    /// You can provide the maximum amount of posts to fetch. Use usize::MAX to get all the available posts.
    /// Keep in mind that a GET request will be sent each 50 posts.
    pub async fn scrape_posts(
        &mut self,
        user_id: &str,
        max_posts: usize,
    ) -> InstagramScraperResult<Vec<Post>> {
        self.restrict_authed()?;
        debug!("collecting up to {} posts for {}", max_posts, user_id);
        let mut posts = Vec::new();
        let mut cursor = String::default();
        loop {
            let amount = if posts.len() + DEFAULT_POST_AMOUNT > max_posts {
                max_posts.saturating_sub(posts.len())
            } else {
                DEFAULT_POST_AMOUNT
            };

            debug!("collecting {} posts from {}", amount, cursor);
            let params = format!(
                r#"{{"id":"{}","first":{},"after":"{}"}}"#,
                user_id, amount, cursor
            );
            let response = self
                .client
                .get(format!(
                    "{}graphql/query/?query_hash=42323d64886122307be10013ad2dcc44&variables={}",
                    BASE_URL, params
                ))
                .send()
                .await?;
            Self::restrict_successful(&response)?;
            self.update_csrftoken(&response);
            match response
                .text()
                .await
                .map(|t| serde_json::from_str::<requests::PostResponse>(&t))
            {
                Err(err) => return Err(err.into()),
                Ok(Ok(post_response)) => {
                    let new_cursor = post_response.end_cursor().map(|x| x.to_string());
                    let response_posts = post_response.posts();
                    debug!("found {} posts", response_posts.len());
                    posts.extend(response_posts);
                    debug!(
                        "checking cursor; new cursor: {:?}; last cursor: {}",
                        new_cursor, cursor
                    );
                    if new_cursor == Some(cursor)
                        || new_cursor.is_none()
                        || posts.len() >= max_posts
                    {
                        debug!("leaving loop");
                        break;
                    }
                    cursor = new_cursor.unwrap();
                }
                Ok(Err(err)) => return Err(err.into()),
            }
        }
        Ok(posts)
    }

    /// Scrape comments
    pub async fn scrape_comments(
        &mut self,
        shortcode: &str,
        max_comments: usize,
    ) -> InstagramScraperResult<Vec<Comment>> {
        self.restrict_authed()?;
        debug!(
            "collecting up to {} comments for {}",
            max_comments, shortcode
        );
        let mut comments = Vec::new();
        let mut cursor = String::default();
        loop {
            let amount = if comments.len() + DEFAULT_COMMENTS_AMOUNT > max_comments {
                max_comments.saturating_sub(comments.len())
            } else {
                DEFAULT_COMMENTS_AMOUNT
            };

            debug!("collecting {} comments from {}", amount, cursor);
            let params = format!(
                r#"{{"shortcode":"{}","first":{},"after":"{}"}}"#,
                shortcode, amount, cursor
            );
            let response = self
                .client
                .get(format!(
                    "{}graphql/query/?query_hash=33ba35852cb50da46f5b5e889df7d159&variables={}",
                    BASE_URL, params
                ))
                .send()
                .await?;
            Self::restrict_successful(&response)?;
            self.update_csrftoken(&response);
            match response
                .text()
                .await
                .map(|t| serde_json::from_str::<requests::CommentResponse>(&t))
            {
                Err(err) => return Err(err.into()),
                Ok(Ok(comment_response)) => {
                    let new_cursor = comment_response.end_cursor().map(|x| x.to_string());
                    let comment_response = comment_response.comments();
                    debug!("found {} comments", comment_response.len());
                    comments.extend(comment_response);
                    debug!(
                        "checking cursor; new cursor: {:?}; last cursor: {}",
                        new_cursor, cursor
                    );
                    if new_cursor == Some(cursor)
                        || new_cursor.is_none()
                        || comments.len() >= max_comments
                    {
                        debug!("leaving loop");
                        break;
                    }
                    cursor = new_cursor.unwrap();
                }
                Ok(Err(err)) => return Err(err.into()),
            }
        }
        Ok(comments)
    }

    // -- private

    /// Logout from Instagram
    pub(crate) async fn logout(&mut self) -> InstagramScraperResult<()> {
        if let Some(csrf_token) = self.csrftoken.as_deref() {
            let response = self
                .client
                .post(LOGOUT_URL)
                .header(header::USER_AGENT, CHROME_WIN_USER_AGENT)
                .form(&requests::LogoutRequest::new(csrf_token.to_string()).form())
                .send()
                .await?;
            Self::restrict_successful(&response)
        } else {
            error!("unauthenticated user; cannot logout");
            Err(InstagramScraperError::Unauthenticated)
        }
    }

    /// Returns whether session is authed
    pub(crate) fn authed(&self) -> bool {
        self.csrftoken.is_some()
    }

    /// Login to instagram as a guest
    async fn login_as_guest(&self) -> InstagramScraperResult<String> {
        debug!("authenticating as guest");
        self.request_csrftoken().await
    }

    /// Login to instagram as an authenticated user
    async fn login_as_user(
        &mut self,
        username: String,
        password: String,
    ) -> InstagramScraperResult<String> {
        debug!("authenticating with username and password");
        let token = self.request_csrftoken().await?;
        let response = self
            .client
            .post(LOGIN_URL)
            .form(
                requests::UsernamePasswordLoginRequest::new(username, password)
                    .form()
                    .as_slice(),
            )
            .header(header::REFERER, BASE_URL)
            .header(X_CSRF_TOKEN, token.clone())
            .header("X-Requested-With", "XMLHttpRequest")
            .send()
            .await?;
        Self::restrict_successful(&response)?;
        debug!("setting cookies received from response");
        let body: requests::UsernamePasswordLoginResponse = response.json().await?;
        if body.authenticated {
            debug!("user authenticated successfully");
            Ok(token)
        } else {
            error!("login failed: {:?}; {:?}", body.status, body.message);
            Err(InstagramScraperError::AuthenticationFailed {
                status: body.status.unwrap_or_default(),
                message: body.message.unwrap_or_default(),
            })
        }
    }

    async fn request_csrftoken(&self) -> InstagramScraperResult<String> {
        let response = self
            .client
            .get(BASE_URL)
            .header(header::REFERER, BASE_URL)
            .send()
            .await?;
        Self::restrict_successful(&response)?;
        trace!("login status: {}", response.status());
        let mut cookies = response.cookies();
        match cookies
            .find(|x| x.name() == "csrftoken")
            .map(|x| x.value().to_string())
        {
            Some(cookie) => Ok(cookie),
            None => Err(InstagramScraperError::CsrfTokenIsMissing),
        }
    }

    /// Update csrf token
    fn update_csrftoken(&mut self, response: &Response) {
        let mut cookies = response.cookies();
        if let Some(token) = cookies
            .find(|x| x.name() == "csrftoken")
            .map(|x| x.value().to_string())
        {
            debug!("new csrftoken: {}", token);
            self.csrftoken = Some(token);
        }
    }

    /// Fetch stories from url
    async fn fetch_stories(&mut self, url: String) -> InstagramScraperResult<Vec<Story>> {
        debug!("fetching user stories at {}", url);
        let response = self.client.get(url).send().await?;
        match response
            .text()
            .await
            .map(|t| serde_json::from_str::<requests::ReelsMedia>(&t).map(|i| i.items()))
        {
            Err(err) => Err(err.into()),
            Ok(Ok(stories)) => Ok(stories.into_iter().map(Story::from).collect()),
            Ok(Err(err)) => Err(err.into()),
        }
    }

    /// Fetch highlighted stories ids
    async fn fetch_highlighted_stories_ids(
        &mut self,
        user_id: &str,
    ) -> InstagramScraperResult<Vec<String>> {
        let response = self.client.get(format!("{}graphql/query/?query_hash=c9100bf9110dd6361671f113dd02e7d6&variables=%7B%22user_id%22%3A%22{}%22%2C%22include_chaining%22%3Afalse%2C%22include_reel%22%3Afalse%2C%22include_suggested_users%22%3Afalse%2C%22include_logged_out_extras%22%3Afalse%2C%22include_highlight_reels%22%3Atrue%2C%22include_related_profiles%22%3Afalse%7D", BASE_URL, user_id)).send().await?;
        match response
            .text()
            .await
            .map(|t| serde_json::from_str::<requests::HighlightReels>(&t).map(|i| i.node_ids()))
        {
            Err(err) => Err(err.into()),
            Ok(Ok(ids)) => Ok(ids),
            Ok(Err(err)) => Err(err.into()),
        }
    }

    /// This function puts a restriction on a function flow to return in case of an unsuccessful status code in the HTTP response.
    ///
    /// it must be called as `Self::restrict_successful(&response)?;`
    fn restrict_successful(response: &reqwest::Response) -> InstagramScraperResult<()> {
        debug!("response status {}", response.status());
        match response.status().is_success() {
            true => Ok(()),
            false => Err(InstagramScraperError::from(response.status())),
        }
    }

    /// This function puts a restriction on a function flow to return in case we're not authenticated
    fn restrict_authed(&self) -> InstagramScraperResult<()> {
        trace!("checking authentication");
        if self.authed() {
            trace!("authed");
            Ok(())
        } else {
            error!("unauthenticated user, but authentication is required");
            Err(InstagramScraperError::Unauthenticated)
        }
    }
}

#[cfg(test)]
mod test {

    use super::*;

    use pretty_assertions::assert_eq;

    #[test]
    fn should_initialize_session() {
        let session = Session::default();
        assert!(session.csrftoken.is_none());
        assert!(!session.authed());
    }

    #[tokio::test]
    async fn should_login_as_guest() {
        let mut session = Session::default();
        assert!(session.login(Authentication::Guest).await.is_ok());
        assert!(session.authed());
        assert!(session.logout().await.is_ok());
    }

    #[tokio::test]
    async fn should_logout_as_guest() {
        let mut session = Session::default();
        assert!(session.login(Authentication::Guest).await.is_ok());
        assert!(session.authed());
        assert!(session.logout().await.is_ok());
    }

    #[tokio::test]
    async fn should_login_as_user_and_scrape_all() {
        let mut session = user_login().await;
        assert!(session.authed());
        // profile pic
        let user_id = session
            .scrape_shared_data_userinfo("bigluca.marketing")
            .await
            .unwrap()
            .id;
        assert!(session
            .scrape_profile_pic(&user_id)
            .await
            .unwrap()
            .is_some());
        // Stories
        let user_id = session
            .scrape_shared_data_userinfo("tamadogecoin")
            .await
            .unwrap()
            .id;
        let stories = session.scrape_stories(&user_id, 7).await.unwrap();
        assert_eq!(stories.highlight_stories.len(), 7);
        let user_id = session
            .scrape_shared_data_userinfo("tamadogecoin")
            .await
            .unwrap()
            .id;
        // Posts
        assert!(session.scrape_posts(&user_id, 100).await.is_ok());
        let user_id = session
            .scrape_shared_data_userinfo("chiaraferragni")
            .await
            .unwrap()
            .id;
        let latest_posts = session.scrape_posts(&user_id, 10).await.unwrap();
        assert_eq!(latest_posts.len(), 10);
        // Comments
        let last_post = latest_posts.get(0).unwrap();
        assert!(session
            .scrape_comments(&last_post.shortcode, 100)
            .await
            .is_ok());

        // logout
        assert!(session.logout().await.is_ok());
    }

    #[tokio::test]
    async fn should_return_error_if_not_authed() {
        let mut session = Session::default();
        assert!(session
            .scrape_shared_data_userinfo("tamadogecoin")
            .await
            .is_err());
        assert!(session.scrape_comments("53718238932", 10).await.is_err());
        assert!(session.scrape_posts("53718238932", 10).await.is_err());
        assert!(session.scrape_profile_pic("53718238932").await.is_err());
        assert!(session.scrape_stories("53718238932", 10).await.is_err());
    }

    async fn user_login() -> Session {
        let username =
            std::env::var("INSTAGRAM_USERNAME").expect("missing env key INSTAGRAM_USERNAME");
        let password =
            std::env::var("INSTAGRAM_PASSWORD").expect("missing env key INSTAGRAM_PASSWORD");
        let mut session = Session::default();
        assert!(session
            .login(Authentication::UsernamePassword { username, password })
            .await
            .is_ok());

        session
    }
}