Skip to main content

asimov_apify_module/api/
instagram.rs

1use std::vec;
2use asimov_module::prelude::{FromStr, String, ToString, Vec};
3use serde::Serialize;
4use url::{ParseError, Url};
5
6/// See: https://apify.com/apify/instagram-profile-scraper
7#[derive(Clone, Debug, Default, Serialize)]
8pub struct InstagramProfileScrapeRequest {
9    pub usernames: Vec<String>,
10}
11
12impl FromStr for InstagramProfileScrapeRequest {
13    type Err = ParseError;
14
15    fn from_str(input: &str) -> Result<Self, Self::Err> {
16        let url = Url::parse(input)?;
17
18        let username = url
19            .path_segments()
20            .and_then(|mut segments| segments.next())
21            .filter(|s| !s.is_empty())
22            .ok_or(ParseError::InvalidDomainCharacter)?
23            .to_string();
24
25        Ok(Self { usernames: vec![username] })
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn test_from_str() {
35        let request = InstagramProfileScrapeRequest::from_str("https://www.instagram.com/humansofny/").unwrap();
36        assert_eq!(request.usernames[0], "humansofny");
37        assert!(InstagramProfileScrapeRequest::from_str("https://www.instagram.com/").is_err());
38        assert!(InstagramProfileScrapeRequest::from_str("invalid").is_err());
39    }
40}