Skip to content

Commit

Permalink
Added lyrics fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Naapperas committed Jun 26, 2024
1 parent 52455e4 commit fd8abd4
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 27 deletions.
165 changes: 160 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ toml = "0.8"
unicode-width = "0.1.13"
url = "2.5"
cursive_buffered_backend = "0.6.1"
cursive-async-view = "0.6.0"

[target.'cfg(unix)'.dependencies]
signal-hook = "0.3.0"
Expand Down
52 changes: 52 additions & 0 deletions src/lyrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use std::sync::Arc;

use crate::{
lyrics_fetcher::LyricsFetcher,
model::{playable::Playable, track::Track},
queue::Queue,
};

#[derive(Clone)]
pub struct LyricsManager {
queue: Arc<Queue>,
fetcher: LyricsFetcher,
// TODO: add a cache
}

impl LyricsManager {
pub fn new(queue: Arc<Queue>, fetcher: LyricsFetcher) -> Self {
LyricsManager { queue, fetcher }
}

/// Saves the given lyrics to the user's filesystem.
///
/// Returns an optional message indicating the outcome of this operation.
pub fn save_lyrics(&self, lyrics: String) -> Option<String> {
Some("".to_string())
}

/// Fetches and returns the lyrics of the given track
pub fn get_lyrics(&self, track: Track) -> String {
// TODO: implement caching

self.fetcher.fetch(&track)
}

/// Fetches and returns the lyrics of the currently playing track
pub fn get_lyrics_for_current(&self) -> String {
match self.get_current_track() {
None => String::from("No track currently playing: could not get lyrics"),
Some(track) => self.get_lyrics(track),
}
}

/// Returns the track being played currently, or nothing if the user is listening to a podcast episodes
fn get_current_track(&self) -> Option<Track> {
let playable = self.queue.get_current().unwrap();

match playable {
Playable::Track(track) => Some(track),
Playable::Episode(_) => None,
}
}
}
Empty file removed src/lyrics/mod.rs
Empty file.
23 changes: 23 additions & 0 deletions src/lyrics_fetcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::model::track::Track;

#[derive(Clone)]
pub struct LyricsFetcher {}

impl LyricsFetcher {
pub fn new() -> LyricsFetcher {
Self {}
}

/// Fetches the lyrics of the given song using the specified lyrics source
pub fn fetch(&self, track: &Track) -> String {
// std::thread::sleep(std::time::Duration::from_secs(2));

format!("Sample Lyrics for {}\n", track.title)
}
}

impl Default for LyricsFetcher {
fn default() -> Self {
LyricsFetcher::new() // TODO: check the prefered fetcher
}
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ mod config;
mod events;
mod ext_traits;
mod library;
mod lyrics;
mod lyrics_fetcher;
mod model;
mod panic;
mod queue;
Expand Down
Loading

0 comments on commit fd8abd4

Please sign in to comment.