1 use std::io::{Result, Error, ErrorKind::Other};
2 use serde::Deserialize;
4 use scraper::{ElementRef, Html, Selector, node::Node::{Text, Element}};
6 #[derive(Deserialize, Debug)]
7 struct GeniusApiResponse {
8 response: GeniusResponseBlock,
11 #[derive(Deserialize, Debug)]
12 struct GeniusResponseBlock {
16 #[derive(Deserialize, Debug)]
18 #[serde(rename = "type")]
23 #[derive(Deserialize, Debug)]
28 pub async fn search(url: &str) -> Result<String> {
29 let response = get(url).await
30 .map_err(|_| Error::new(Other, "Could not perform lyrics search in engine."))?
31 .json::<GeniusApiResponse>().await
32 .map_err(|_| Error::new(Other, "Lyrics engine returned invalid response from search."))?;
33 let url = response.response.hits.into_iter()
34 .find(|hit| hit.hit_type == "song")
35 .map(|hit| hit.result.url)
36 .ok_or_else(|| Error::new(Other, "Could not find a matching track in lyrics engine."))?;
41 pub async fn get_lyrics(url: &str) -> Result<String> {
42 let song_html = get(url).await
43 .map_err(|_| Error::new(Other, "Could not fetch lyrics from engine."))?
45 .map_err(|_| Error::new(Other, "Lyrics engine returned invalid response."))?;
47 let document = Html::parse_document(&song_html);
48 let selector = Selector::parse(r#"div[data-lyrics-container="true"]"#).unwrap();
49 let lyrics_div = document.select(&selector).next()
50 .ok_or_else(|| Error::new(Other, "Could not find lyrics in response."))?;
52 let mut lyrics = String::new();
53 for node in lyrics_div.children() {
56 if element.name() == "br" {
59 if let Some(element_ref) = ElementRef::wrap(node) {
60 let text = element_ref.text().collect::<Vec<_>>().join("");
61 lyrics.push_str(&text);
66 lyrics.push_str(text);