src/global/functions/from_url.rs

80 lines · 2937 bytes

 1/// gets the channel id from it's url
 2pub fn from_channel_url(identifier: &str) -> Result<String, String> {
 3    // a youtube channel id is exactly 24 characters long, so if the identifier to 24 chars
 4    // long, then just assume its the channel id
 5    if identifier.len() == 24 {
 6        Ok(identifier.to_string())
 7    } else {
 8        // the channel id comes after `/channel/` in an url
 9        let index = if let Some(index) = identifier.find("/channel/") {
10            // if there isnt 24 characters after `/channel/`, that url must not have
11            // contained a channel id
12            if identifier.len() < index + 33 {
13                return Err(format!(
14                    "Cannot find channel id from string `{}`",
15                    identifier
16                ));
17            }
18            index + 9
19        } else {
20            return Err(format!(
21                "Cannot find channel id from string `{}`",
22                identifier
23            ));
24        };
25
26        Ok(identifier[index..index + 24].to_string())
27    }
28}
29
30pub fn from_video_url(identifier: &str) -> Result<String, String> {
31    // a youtube video id is exactly 11 characters long, so if the identifier to 11 chars
32    // long, then just assume its the video id
33    if identifier.len() == 11 {
34        Ok(identifier.to_string())
35    } else {
36        // the id can come after "?v=" in an url
37        let index = if let Some(index) = identifier.find("?v=") {
38            if identifier.len() < index + 15 {
39                return Err(format!("Cannot find video id from string `{}`", identifier));
40            }
41            index + 3
42        // and also "youtu.be/"
43        } else if let Some(index) = identifier.find("youtu.be/") {
44            if identifier.len() < index + 21 {
45                return Err(format!("Cannot find video id from string `{}`", identifier));
46            }
47            index + 9
48        } else {
49            return Err(format!("Cannot find video id from string `{}`", identifier));
50        };
51
52        Ok(identifier[index..index + 11].to_string())
53    }
54}
55
56pub fn from_playlist_url(identifier: &str) -> Result<String, String> {
57    // a youtube playlist id is exactly 34 characters long, so if the identifier to 34 chars
58    // long, then just assume its the playlist id
59    if identifier.len() == 34 {
60        Ok(identifier.to_string())
61    } else {
62        // the id can come after "list=" in urls
63        let index = if let Some(index) = identifier.find("list=") {
64            if identifier.len() < index + 39 {
65                return Err(format!(
66                    "Cannot find plalyist id from string `{}`",
67                    identifier
68                ));
69            }
70            index + 5
71        } else {
72            return Err(format!(
73                "Cannot find playlist id from string `{}`",
74                identifier
75            ));
76        };
77
78        Ok(identifier[index..index + 34].to_string())
79    }
80}