has better chance to find to correct song

This commit is contained in:
vandechat96
2022-08-29 19:40:06 +02:00
parent fae5d0db8e
commit 90fbb7dbd6
6 changed files with 38 additions and 21 deletions

3
.gitignore vendored
View File

@@ -1,4 +1,5 @@
/target
.env
.idea
.spotify_token_cache.json
.spotify_token_cache.json
playlist.txt

1
Cargo.lock generated
View File

@@ -1258,6 +1258,7 @@ dependencies = [
"anyhow",
"dotenv",
"env_logger",
"lazy_static",
"log",
"md5",
"rand",

View File

@@ -18,6 +18,7 @@ authors = ["Jika"]
[dependencies]
anyhow = "1.0"
thiserror = "1.0"
lazy_static = "1.4"
log = "0.4"
env_logger = "0.9"

View File

@@ -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

3
src/spotify.rs Normal file → Executable file
View File

@@ -37,8 +37,7 @@ pub async fn get_spotify_playlists(playlists_id: &[PlaylistId]) -> Result<Vec<Si
let creds = Credentials::from_env().unwrap();
let oauth = OAuth::from_env(scopes!(
"playlist-read-private",
"playlist-read-collaborative",
"playlist-read-public"
"playlist-read-collaborative"
))
.unwrap();

47
src/subsonic.rs Normal file → Executable file
View File

@@ -7,8 +7,13 @@ use reqwest::Url;
use serde::Deserialize;
use serde_json::{Map, Value};
use std::fmt::Write as _;
use lazy_static::lazy_static;
use regex::Regex;
use thiserror::Error;
lazy_static! {
static ref RE: Regex = Regex::new(r"[-()\[\];']").unwrap();
}
const SALT_SIZE: usize = 36;
#[derive(Error, Debug)]
@@ -216,23 +221,19 @@ impl Client {
let name = &track.name;
let artist = &track.artist;
let album = &track.album;
let args = vec![(String::from("query"), format!("{name} {artist} {album}"))];
let response = self.request_args("search3", Some(args)).await?;
let mut title = format!("{name} {artist} {album}");
title = RE.replace_all(&title,"").to_string();
let mut found: Vec<SubTrack> = 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<Vec<SubTrack>>{
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)
}
}