Add uploader and id to video information

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/1/head
trivernis 4 years ago
parent 7643ca9faf
commit 7e20aaf4b2
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

File diff suppressed because it is too large Load Diff

@ -7,18 +7,25 @@ lazy_static::lazy_static! {
static ref TITLE_SELECTOR: Selector = Selector::parse(r#"meta[property="og:title"]"#).unwrap(); static ref TITLE_SELECTOR: Selector = Selector::parse(r#"meta[property="og:title"]"#).unwrap();
static ref THUMBNAIL_SELECTOR: Selector = Selector::parse(r#"meta[property="og:image"]"#).unwrap(); static ref THUMBNAIL_SELECTOR: Selector = Selector::parse(r#"meta[property="og:image"]"#).unwrap();
static ref URL_SELECTOR: Selector = Selector::parse(r#"link[rel="canonical"]"#).unwrap(); static ref URL_SELECTOR: Selector = Selector::parse(r#"link[rel="canonical"]"#).unwrap();
static ref CHANNEL_SELECTOR: Selector = Selector::parse(r#"link[itemprop="name"]"#).unwrap();
static ref ID_SELECTOR: Selector = Selector::parse(r#"meta[itemprop="videoId"]"#).unwrap();
} }
/// Parses information about a video from the html /// Parses information about a video from the html
pub fn parse_video_information(html: &str) -> YoutubeResult<VideoInformation> { pub fn parse_video_information(html: &str) -> YoutubeResult<VideoInformation> {
let document = Html::parse_document(html); let document = Html::parse_document(html);
let video_id = try_select_attribute(&document, &ID_SELECTOR, "content")?;
let url = try_select_attribute(&document, &URL_SELECTOR, "href")?; let url = try_select_attribute(&document, &URL_SELECTOR, "href")?;
let author = try_select_attribute(&document, &CHANNEL_SELECTOR, "content")?;
let title = try_select_attribute(&document, &TITLE_SELECTOR, "content")?; let title = try_select_attribute(&document, &TITLE_SELECTOR, "content")?;
let thumbnail = try_select_attribute(&document, &THUMBNAIL_SELECTOR, "content").ok(); let thumbnail = try_select_attribute(&document, &THUMBNAIL_SELECTOR, "content").ok();
Ok(VideoInformation { Ok(VideoInformation {
id: video_id.to_string(),
url: url.to_string(), url: url.to_string(),
title: title.to_string(), title: title.to_string(),
uploader: author.to_string(),
thumbnail: thumbnail.map(|s| s.to_string()), thumbnail: thumbnail.map(|s| s.to_string()),
}) })
} }

@ -5,10 +5,12 @@ async fn test_get_video_information() {
let information = get_video_information("https://www.youtube.com/watch?v=dQw4w9WgXcQ") let information = get_video_information("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
.await .await
.unwrap(); .unwrap();
assert_eq!(information.id, "dQw4w9WgXcQ".to_string());
assert_eq!( assert_eq!(
information.url, information.url,
"https://www.youtube.com/watch?v=dQw4w9WgXcQ".to_string() "https://www.youtube.com/watch?v=dQw4w9WgXcQ".to_string()
); );
assert_eq!(information.uploader, "RickAstleyVEVO".to_string());
assert_eq!( assert_eq!(
information.title, information.title,
"Rick Astley - Never Gonna Give You Up (Video)".to_string() "Rick Astley - Never Gonna Give You Up (Video)".to_string()

@ -1,6 +1,8 @@
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct VideoInformation { pub struct VideoInformation {
pub id: String,
pub url: String, pub url: String,
pub title: String, pub title: String,
pub uploader: String,
pub thumbnail: Option<String>, pub thumbnail: Option<String>,
} }

Loading…
Cancel
Save