Skip to content

Commit

Permalink
The Lyric Fetch is working
Browse files Browse the repository at this point in the history
  • Loading branch information
newtoallofthis123 committed Nov 19, 2023
1 parent 2be4532 commit afd99e3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
18 changes: 14 additions & 4 deletions src/scraper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>{
fn clean_text(text: &str)->String{
text.replace(['\n', '\t'], "").trim().to_string()
}

pub async fn get_search_options(term: &str)->HashMap<i32, (String, String)>{
let search_html = get_page_html(&format!("{DUMB_URL}/search?q={term}")).await;
let search_doc = Html::parse_document(&search_html);

Expand All @@ -25,11 +29,17 @@ pub async fn get_search_options(term: &str)->HashMap<String, String>{
let search_links = search_doc.select(&selector);
let mut search_options = HashMap::new();

for link in search_links {
let link_text = link.text().collect::<Vec<_>>().join("");
for (i, link) in search_links.enumerate() {
let link_text = clean_text(&link.text().collect::<Vec<_>>().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;
}

0 comments on commit afd99e3

Please sign in to comment.