diff --git a/.gitignore b/.gitignore index 1c034d7..ec91d47 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /target .env .idea -.spotify_token_cache.json \ No newline at end of file +.spotify_token_cache.json +playlist.txt \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 90736dc..0b75011 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1258,6 +1258,7 @@ dependencies = [ "anyhow", "dotenv", "env_logger", + "lazy_static", "log", "md5", "rand", diff --git a/Cargo.toml b/Cargo.toml index eb5acd2..5d4b6bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ authors = ["Jika"] [dependencies] anyhow = "1.0" thiserror = "1.0" +lazy_static = "1.4" log = "0.4" env_logger = "0.9" diff --git a/playlist.txt b/playlist.txt deleted file mode 100644 index 3a252ba..0000000 --- a/playlist.txt +++ /dev/null @@ -1,4 +0,0 @@ -https://open.spotify.com/playlist/1yhfU0llvFG8VlMD3CvAtb?si=febfaeaaa8804c48 -https://open.spotify.com/playlist/4noj74Daxdb03B6YxFyAeH?si=b3c9bd771c794e5a -https://open.spotify.com/playlist/2Ule0vZ816HKbLvtfMLwxs?si=20bc747bd2b643c8 -https://open.spotify.com/playlist/7LmNrfHlNGccOHMephfcTJ?si=4388c164436d4995 \ No newline at end of file diff --git a/src/spotify.rs b/src/spotify.rs old mode 100644 new mode 100755 index 3e70c48..e3d2921 --- a/src/spotify.rs +++ b/src/spotify.rs @@ -37,8 +37,7 @@ pub async fn get_spotify_playlists(playlists_id: &[PlaylistId]) -> Result = serde_json::from_value( - response - .get("searchResult3") - .unwrap() - .as_object() - .unwrap() - .get("song") - .unwrap_or(&Value::Array(vec![])) - .to_owned(), - )?; + let mut found = self.search_track(title).await?; if found.is_empty() { - debug!("Nothing found"); - bail!(Error::NoMatch(track.to_string())) + debug!("Nothing found, searching again"); + let mut title = format!("{name} {artist}"); + title = RE.replace_all(&title,"").to_string(); + found = self.search_track(title).await?; + if found.is_empty(){ + bail!(Error::NoMatch(track.to_string())) + } } found = found @@ -246,4 +247,22 @@ impl Client { Ok(found) } + + async fn search_track(&self, query:String) ->Result>{ + let args = vec![(String::from("query"), query)]; + let response = self.request_args("search3", Some(args)).await?; + + let found = serde_json::from_value( + response + .get("searchResult3") + .unwrap() + .as_object() + .unwrap() + .get("song") + .unwrap_or(&Value::Array(vec![])) + .to_owned(), + )?; + Ok(found) + + } }