From afd99e3561686211ea5646d993cf1a5f3007953c Mon Sep 17 00:00:00 2001 From: newtoallofthis123 Date: Sun, 19 Nov 2023 14:21:58 +0530 Subject: [PATCH] The Lyric Fetch is working --- src/main.rs | 2 +- src/scraper.rs | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index b6c5ccb..71a6bc3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ async fn main() { setup_panic!(); cli::splash_screen(); let x = scraper::get_search_options("Falling in love with you").await; - println!("{:?}", x); + println!("{:?}", scraper::get_lyrics(x.get(&0).unwrap().1.as_str()).await ); /* let (input, cmd) = cli::parse_args(); if cmd == "-h" || cmd == "--help" { cli::print_help(); diff --git a/src/scraper.rs b/src/scraper.rs index d844c3a..dd43588 100644 --- a/src/scraper.rs +++ b/src/scraper.rs @@ -16,7 +16,11 @@ async fn get_page_html(url: &str) -> String { res.text().await.unwrap() } -pub async fn get_search_options(term: &str)->HashMap{ +fn clean_text(text: &str)->String{ + text.replace(['\n', '\t'], "").trim().to_string() +} + +pub async fn get_search_options(term: &str)->HashMap{ let search_html = get_page_html(&format!("{DUMB_URL}/search?q={term}")).await; let search_doc = Html::parse_document(&search_html); @@ -25,11 +29,17 @@ pub async fn get_search_options(term: &str)->HashMap{ let search_links = search_doc.select(&selector); let mut search_options = HashMap::new(); - for link in search_links { - let link_text = link.text().collect::>().join(""); + for (i, link) in search_links.enumerate() { + let link_text = clean_text(&link.text().collect::>().join("")); let link_href = link.value().attr("href").unwrap().to_string(); - search_options.insert(link_text, link_href); + search_options.insert(i as i32, (link_text, link_href)); } search_options } + +pub async fn get_lyrics(slug: &str)->String{ + let lyrics_html = get_page_html(&format!("{DUMB_URL}{slug}")).await; + + return lyrics_html; +}