From 754056c27b2bd4d6a33733cce23525ab36253605 Mon Sep 17 00:00:00 2001 From: Giovanni Enrico Loni <65892886+genricoloni@users.noreply.github.com> Date: Mon, 16 Sep 2024 18:43:06 +0200 Subject: [PATCH 1/2] Fixed error in lyric_finder/lib.rs that, for some songs, shown the translation of a lyric instead of the actual one --- lyric_finder/src/lib.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/lyric_finder/src/lib.rs b/lyric_finder/src/lib.rs index 7d8db3ea..a99bc3e6 100644 --- a/lyric_finder/src/lib.rs +++ b/lyric_finder/src/lib.rs @@ -111,25 +111,30 @@ impl Client { /// Get the lyric of a song satisfying a given `query`. pub async fn get_lyric(&self, query: &str) -> anyhow::Result { - // The function first searches songs satisfying the query - // then it retrieves the song's lyric by crawling the "genius.com" website. - - let result = { - let mut results = self.search_songs(query).await?; - log::debug!("search results: {results:?}"); - if results.is_empty() { - return Ok(LyricResult::None); - } - results.remove(0) + // Perform the search for songs + let results = self.search_songs(query).await?; + + // Filter to find the first result where the artist names do not contain 'Genius' + let result = results.into_iter() + .find(|result| !result.artist_names.contains("Genius")); + + // If no valid result is found, return LyricResult::None + let result = match result { + Some(res) => res, + None => return Ok(LyricResult::None), }; - + + // Retrieve the song lyrics from the URL of the result let lyric = self.retrieve_lyric(&result.url).await?; + + // Return a LyricResult::Some with the song information Ok(LyricResult::Some { track: result.title, artists: result.artist_names, lyric: Self::process_lyric(lyric), }) } + } impl Default for Client { From 7b36b57adc7f2a0213660adcf645837436815a25 Mon Sep 17 00:00:00 2001 From: Giovanni Enrico Loni <65892886+genricoloni@users.noreply.github.com> Date: Mon, 23 Sep 2024 08:41:51 +0200 Subject: [PATCH 2/2] Fix formatting issues as per cargo fmt --- lyric_finder/src/lib.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lyric_finder/src/lib.rs b/lyric_finder/src/lib.rs index a99bc3e6..0ea992bf 100644 --- a/lyric_finder/src/lib.rs +++ b/lyric_finder/src/lib.rs @@ -113,20 +113,21 @@ impl Client { pub async fn get_lyric(&self, query: &str) -> anyhow::Result { // Perform the search for songs let results = self.search_songs(query).await?; - + // Filter to find the first result where the artist names do not contain 'Genius' - let result = results.into_iter() + let result = results + .into_iter() .find(|result| !result.artist_names.contains("Genius")); - + // If no valid result is found, return LyricResult::None let result = match result { Some(res) => res, None => return Ok(LyricResult::None), }; - + // Retrieve the song lyrics from the URL of the result let lyric = self.retrieve_lyric(&result.url).await?; - + // Return a LyricResult::Some with the song information Ok(LyricResult::Some { track: result.title, @@ -134,7 +135,6 @@ impl Client { lyric: Self::process_lyric(lyric), }) } - } impl Default for Client {