use std::io::{Result, Error, ErrorKind::Other}; use serde::Deserialize; use reqwest::get; use scraper::{ElementRef, Html, Selector, node::Node::{Text, Element}}; #[derive(Deserialize, Debug)] struct GeniusApiResponse { response: GeniusResponseBlock, } #[derive(Deserialize, Debug)] struct GeniusResponseBlock { hits: Vec, } #[derive(Deserialize, Debug)] struct GeniusHit { #[serde(rename = "type")] hit_type: String, result: GeniusResult, } #[derive(Deserialize, Debug)] struct GeniusResult { url: String, } pub async fn search(url: &str) -> Result { let response = get(url).await .map_err(|_| Error::new(Other, "Could not perform lyrics search in engine."))? .json::().await .map_err(|_| Error::new(Other, "Lyrics engine returned invalid response from search."))?; let url = response.response.hits.into_iter() .find(|hit| hit.hit_type == "song") .map(|hit| hit.result.url) .ok_or_else(|| Error::new(Other, "Could not find a matching track in lyrics engine."))?; Ok(url) } pub async fn get_lyrics(url: &str) -> Result { let song_html = get(url).await .map_err(|_| Error::new(Other, "Could not fetch lyrics from engine."))? .text().await .map_err(|_| Error::new(Other, "Lyrics engine returned invalid response."))?; let document = Html::parse_document(&song_html); let selector = Selector::parse(r#"div[data-lyrics-container="true"]"#).unwrap(); let lyrics_div = document.select(&selector).next() .ok_or_else(|| Error::new(Other, "Could not find lyrics in response."))?; let mut lyrics = String::new(); for node in lyrics_div.children() { match node.value() { Element(element) => { if element.name() == "br" { lyrics.push('\n'); } else { if let Some(element_ref) = ElementRef::wrap(node) { let text = element_ref.text().collect::>().join(""); lyrics.push_str(&text); } } }, Text(text) => { lyrics.push_str(text); }, _ => {} } } Ok(lyrics) }