use super::Query;
use crate::error::Result;
use crate::request::{ApiClient, ApiResponse, CryptoType};
use serde_json::json;
impl ApiClient {
pub async fn song_url_v1_302(&self, query: &Query) -> Result<ApiResponse> {
let id = query.get_or("id", "0");
let level = query.get_or("level", "standard");
let data = json!({
"id": &id,
"immerseType": "c51",
"level": &level,
});
let response = self
.request(
"/api/song/enhance/download/url/v1",
data,
query.to_option(CryptoType::default()),
)
.await?;
let url = response
.body
.get("data")
.and_then(|d| d.get(0))
.and_then(|item| item.get("url"))
.and_then(|u| u.as_str())
.map(|s| s.to_string());
if let Some(url) = url {
return Ok(ApiResponse {
status: 302,
body: json!({ "redirectUrl": url }),
cookie: response.cookie,
});
}
let mut fallback_data = json!({
"ids": format!("[{}]", id),
"level": &level,
"encodeType": "flac",
});
if level == "sky" {
fallback_data["immerseType"] = json!("c51");
}
let fallback = self
.request(
"/api/song/enhance/player/url/v1",
fallback_data,
query.to_option(CryptoType::default()),
)
.await?;
let fallback_url = fallback
.body
.get("data")
.and_then(|d| d.get(0))
.and_then(|item| item.get("url"))
.and_then(|u| u.as_str())
.map(|s| s.to_string());
match fallback_url {
Some(url) => Ok(ApiResponse {
status: 302,
body: json!({ "redirectUrl": url }),
cookie: fallback.cookie,
}),
None => Ok(fallback),
}
}
}