]> git.r.bdr.sh - rbdr/lyricli/blob - src/lyrics_engine/mod.rs
Add pod2man
[rbdr/lyricli] / src / lyrics_engine / mod.rs
1 use std::io::Result;
2 use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
3
4 mod genius;
5
6 use crate::Track;
7 use genius::{get_lyrics, search};
8
9 const GENIUS_CLIENT_TOKEN: &str = env!("LYRICLI_GENIUS_TOKEN");
10 const GENIUS_API_URL: &str = "https://api.genius.com/search";
11
12 pub async fn print_lyrics(track: Track, show_title: bool) -> Result<()> {
13 if show_title {
14 println!("{} - {}", track.artist, track.name);
15 }
16
17 let artist = utf8_percent_encode(&track.artist, NON_ALPHANUMERIC).to_string();
18 let name = utf8_percent_encode(&track.name, NON_ALPHANUMERIC).to_string();
19 let url = format!("{}?access_token={}&q={}%20{}", GENIUS_API_URL, GENIUS_CLIENT_TOKEN, artist, name);
20
21 let song_url = search(&url).await?;
22 let lyrics = get_lyrics(&song_url).await?;
23
24 println!("{}", lyrics);
25 Ok(())
26 }