From 7da7604139362673556168cdcb41136d901b81cd Mon Sep 17 00:00:00 2001 From: hinyb <40139991+hinyb@users.noreply.github.com> Date: Tue, 26 Mar 2024 18:48:31 +0800 Subject: [PATCH 1/4] migrate to new API --- engines/pyncm/src/lib.rs | 62 +++++++--------------------------------- 1 file changed, 11 insertions(+), 51 deletions(-) diff --git a/engines/pyncm/src/lib.rs b/engines/pyncm/src/lib.rs index d6c60cd..0020afe 100644 --- a/engines/pyncm/src/lib.rs +++ b/engines/pyncm/src/lib.rs @@ -13,16 +13,6 @@ use url::Url; #[derive(Deserialize)] #[non_exhaustive] struct PyNCMResponse { - /// The status code of this response. - pub code: i32, - pub data: Vec, -} - -#[derive(Deserialize)] -#[non_exhaustive] -struct PyNCMResponseEntry { - /// The NCM ID of this song. - pub id: i64, /// The URL of this song. pub url: Option, } @@ -43,24 +33,13 @@ impl Engine for PyNCMEngine { info!("Searching with PyNCM engine…"); let response = fetch_song_info(&info.id, ctx).await?; - - if response.code == 200 { - // We return the URL we got from PyNCM as the song identifier, - // so we can return the URL in retrieve() easily. - let match_result = find_match(&response.data, &info.id)?.map(|url| { - SongSearchInformation::builder() - .source(ENGINE_ID.into()) - .identifier(url) - .build() - }); - - Ok(match_result) - } else { - Err(anyhow::anyhow!( - "failed to request. code: {}", - response.code - )) - } + let match_result = response.url.map(|url| { + SongSearchInformation::builder() + .source(ENGINE_ID.into()) + .identifier(url) + .build() + }); + Ok(match_result) } async fn retrieve<'a>( @@ -82,31 +61,15 @@ impl Engine for PyNCMEngine { async fn fetch_song_info(id: &str, ctx: &Context) -> anyhow::Result { debug!("Fetching the song information…"); - let bitrate = if ctx.enable_flac { 999000 } else { 320000 }; + let bitrate = if ctx.enable_flac { 999 } else { 320 }; let url = Url::parse_with_params( - "https://pyncmd.apis.imouto.in/api/pyncm?module=track&method=GetTrackAudio", - &[("song_ids", id), ("bitrate", &bitrate.to_string())], + "https://music.gdstudio.xyz/api.php?types=url&source=netease", + &[("id", id), ("br", &bitrate.to_string())], )?; - let client = build_client(ctx.proxy_uri.as_deref())?; let response = client.get(url).send().await?; Ok(response.json::().await?) } - -/// Find the matched song from an array of [`PyNCMResponseEntry`]. -fn find_match(data: &[PyNCMResponseEntry], song_id: &str) -> anyhow::Result> { - info!("Finding the matched song…"); - - data.iter() - .find(|entry| { - // Test if the ID of this entry matched what we want to fetch, - // and there is content in its URL. - entry.id.to_string() == song_id && entry.url.is_some() - }) - .map(|v| v.url.clone()) - .ok_or_else(|| anyhow::anyhow!("no matched song")) -} - #[cfg(test)] mod tests { use unm_types::ContextBuilder; @@ -119,10 +82,7 @@ mod tests { let result = fetch_song_info(song_id, &ContextBuilder::default().build().unwrap()).await; if let Ok(response) = result { - assert_eq!(response.code, 200); - assert_eq!(response.data.len(), 1); - assert_eq!(response.data[0].id.to_string(), song_id); - assert!(response.data[0].url.is_some()); + assert!(response.url.is_some()); } else { panic!("failed to fetch song info"); } From 693df1c9b04a5fdc93b5544aa8ce3fe1d6aaf609 Mon Sep 17 00:00:00 2001 From: hinyb <40139991+hinyb@users.noreply.github.com> Date: Thu, 2 May 2024 00:09:31 +0800 Subject: [PATCH 2/4] Update engines/pyncm/src/lib.rs Co-authored-by: pan93412 --- engines/pyncm/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engines/pyncm/src/lib.rs b/engines/pyncm/src/lib.rs index 0020afe..92f9366 100644 --- a/engines/pyncm/src/lib.rs +++ b/engines/pyncm/src/lib.rs @@ -63,7 +63,7 @@ async fn fetch_song_info(id: &str, ctx: &Context) -> anyhow::Result Date: Thu, 2 May 2024 00:22:33 +0800 Subject: [PATCH 3/4] credit to music.gdstudio.xyz --- engines/pyncm/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/engines/pyncm/src/lib.rs b/engines/pyncm/src/lib.rs index 92f9366..63d5c26 100644 --- a/engines/pyncm/src/lib.rs +++ b/engines/pyncm/src/lib.rs @@ -62,6 +62,7 @@ async fn fetch_song_info(id: &str, ctx: &Context) -> anyhow::Result Date: Sat, 8 Jun 2024 10:32:34 +0800 Subject: [PATCH 4/4] Update rest-api-build.yml --- .github/workflows/rest-api-build.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/rest-api-build.yml b/.github/workflows/rest-api-build.yml index 1b92771..0b335cc 100644 --- a/.github/workflows/rest-api-build.yml +++ b/.github/workflows/rest-api-build.yml @@ -3,6 +3,7 @@ name: Build RESTful API binaries on: push: workflow_dispatch: + pull_request: permissions: actions: write