1extern crate html2text;
2extern crate regex;
3extern crate reqwest;
4extern crate scraper;
5
6use regex::Regex;
7use scraper::{Html, Selector};
8
9#[derive(Debug, Clone, Copy)]
12pub enum GetLyricsError {
13 UrlError,
14 NoTextError,
15}
16
17#[inline]
18fn get_lyrics_from_doc(doc: &str) -> Result<String, GetLyricsError> {
19 Ok(Regex::new(r"\[\d*]|/.*|[\[\]]|https:.*")
20 .unwrap()
21 .replace_all(
22 html2text::from_read(
23 match Html::parse_document(doc)
24 .select(&Selector::parse("div[id=application]").unwrap())
25 .next()
26 {
27 Some(x) => x,
28 None => return Err(GetLyricsError::NoTextError),
29 }
30 .select(&Selector::parse("div[data-lyrics-container=true]").unwrap())
31 .fold(String::new(), |acc, e| format!("{}{}", acc, e.html()))
32 .as_bytes(),
33 1000,
34 )
35 .as_str(),
36 "",
37 )
38 .as_ref()
39 .trim()
40 .to_string())
41}
42
43#[inline]
64pub async fn get_lyrics_from_url(url: &str) -> Result<String, GetLyricsError> {
65 get_lyrics_from_doc(
66 match reqwest::get(url).await {
67 Ok(x) => x,
68 Err(_) => return Err(GetLyricsError::UrlError),
69 }
70 .text()
71 .await
72 .unwrap()
73 .as_str(),
74 )
75}
76
77#[inline]
91pub fn get_lyrics_from_url_blocking(url: &str) -> Result<String, GetLyricsError> {
92 get_lyrics_from_doc(
93 match reqwest::blocking::get(url) {
94 Ok(x) => x,
95 Err(_) => return Err(GetLyricsError::UrlError),
96 }
97 .text()
98 .unwrap()
99 .as_str(),
100 )
101}