Commit 03fd148fef
Unsigned
Layout: unified · split
src/global/functions/from_url.rs +42 −56
| @@ -1,72 +1,58 @@ | |||
| 1 | fn is_id_char(c: char) -> bool { | ||
| 2 | c.is_alphanumeric() || matches!(c, '-' | '_') | ||
| 3 | } | ||
| 4 | |||
| 1 | /// gets the channel id from it's url | 5 | /// gets the channel id from it's url |
| 2 | pub fn from_channel_url(identifier: &str) -> Result<String, String> { | 6 | pub 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 | 7 | // if it doesnt look like a url, just assume it is an identifier |
| 4 | // long, then just assume its the channel id | 8 | if identifier.chars().all(is_id_char) { |
| 5 | if identifier.len() == 24 { | 9 | return Ok(identifier.to_string()) |
| 6 | Ok(identifier.to_string()) | 10 | } |
| 11 | |||
| 12 | let index = if let Some(index) = identifier.find("/channel/") { | ||
| 13 | index + 9 | ||
| 7 | } else { | 14 | } else { |
| 8 | // the channel id comes after `/channel/` in an url | 15 | return Err(format!("Cannot find channel id from string `{identifier}`")); |
| 9 | let index = if let Some(index) = identifier.find("/channel/") { | 16 | }; |
| 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!("Cannot find channel id from string `{identifier}`")); | ||
| 14 | } | ||
| 15 | index + 9 | ||
| 16 | } else { | ||
| 17 | return Err(format!("Cannot find channel id from string `{identifier}`")); | ||
| 18 | }; | ||
| 19 | 17 | ||
| 20 | Ok(identifier[index..index + 24].to_string()) | 18 | Ok(identifier[index..].chars().take_while(|c| is_id_char(*c)).collect()) |
| 21 | } | ||
| 22 | } | 19 | } |
| 23 | 20 | ||
| 24 | pub fn from_video_url(identifier: &str) -> Result<String, String> { | 21 | pub fn from_video_url(identifier: &str) -> Result<String, String> { |
| 25 | // a youtube video id is exactly 11 characters long, so if the identifier to 11 chars | 22 | if identifier.chars().all(is_id_char) { |
| 26 | // long, then just assume its the video id | 23 | return Ok(identifier.to_string()) |
| 27 | if identifier.len() == 11 { | 24 | } |
| 28 | Ok(identifier.to_string()) | 25 | |
| 26 | let index = if let Some(index) = identifier.find("?v=") { | ||
| 27 | index + 3 | ||
| 28 | // and also "youtu.be/" | ||
| 29 | } else if let Some(index) = identifier.find("youtu.be/") { | ||
| 30 | index + 9 | ||
| 29 | } else { | 31 | } else { |
| 30 | // the id can come after "?v=" in an url | 32 | return Err(format!("Cannot find video id from string `{identifier}`")); |
| 31 | let index = if let Some(index) = identifier.find("?v=") { | 33 | }; |
| 32 | if identifier.len() < index + 14 { | ||
| 33 | return Err(format!("Cannot find video id from string `{identifier}`")); | ||
| 34 | } | ||
| 35 | index + 3 | ||
| 36 | // and also "youtu.be/" | ||
| 37 | } else if let Some(index) = identifier.find("youtu.be/") { | ||
| 38 | if identifier.len() < index + 20 { | ||
| 39 | return Err(format!("Cannot find video id from string `{identifier}`")); | ||
| 40 | } | ||
| 41 | index + 9 | ||
| 42 | } else { | ||
| 43 | return Err(format!("Cannot find video id from string `{identifier}`")); | ||
| 44 | }; | ||
| 45 | 34 | ||
| 46 | Ok(identifier[index..index + 11].to_string()) | 35 | Ok(identifier[index..].chars().take_while(|c| is_id_char(*c)).collect()) |
| 47 | } | ||
| 48 | } | 36 | } |
| 49 | 37 | ||
| 50 | pub fn from_playlist_url(identifier: &str) -> Result<String, String> { | 38 | pub fn from_playlist_url(identifier: &str) -> Result<String, String> { |
| 51 | // a youtube playlist id is exactly 34 characters long, so if the identifier to 34 chars | 39 | if identifier.chars().all(is_id_char) { |
| 52 | // long, then just assume its the playlist id | 40 | return Ok(identifier.to_string()) |
| 53 | if identifier.len() == 34 { | 41 | } |
| 54 | Ok(identifier.to_string()) | 42 | |
| 55 | } else { | 43 | // the id can come after "list=" in urls |
| 56 | // the id can come after "list=" in urls | 44 | let index = if let Some(index) = identifier.find("list=") { |
| 57 | let index = if let Some(index) = identifier.find("list=") { | 45 | if identifier.len() < index + 39 { |
| 58 | if identifier.len() < index + 39 { | ||
| 59 | return Err(format!( | ||
| 60 | "Cannot find plalyist id from string `{identifier}`" | ||
| 61 | )); | ||
| 62 | } | ||
| 63 | index + 5 | ||
| 64 | } else { | ||
| 65 | return Err(format!( | 46 | return Err(format!( |
| 66 | "Cannot find playlist id from string `{identifier}`" | 47 | "Cannot find plalyist id from string `{identifier}`" |
| 67 | )); | 48 | )); |
| 68 | }; | 49 | } |
| 50 | index + 5 | ||
| 51 | } else { | ||
| 52 | return Err(format!( | ||
| 53 | "Cannot find playlist id from string `{identifier}`" | ||
| 54 | )); | ||
| 55 | }; | ||
| 69 | 56 | ||
| 70 | Ok(identifier[index..index + 34].to_string()) | 57 | Ok(identifier[index..].chars().take_while(|c| is_id_char(*c)).collect()) |
| 71 | } | ||
| 72 | } | 58 | } |