use super::super::{ServiceProvider, SongTag};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
#[allow(unused)]
pub fn to_lyric(json: &str) -> Option<String> {
if let Ok(value) = serde_json::from_str::<Value>(json) {
if value.get("code")?.eq(&200) {
let mut vec: Vec<String> = Vec::new();
let lyric = value.get("lrc")?.get("lyric")?.as_str()?.to_owned();
return Some(lyric);
}
}
None
}
#[derive(Debug, Deserialize, Serialize)]
pub struct SingerInfo {
pub id: u64,
pub name: String,
pub pic_url: String,
}
#[allow(unused)]
pub fn to_singer_info(json: &str) -> Option<Vec<SingerInfo>> {
if let Ok(value) = serde_json::from_str::<Value>(json) {
if value.get("code")?.eq(&200) {
let mut vec: Vec<SingerInfo> = Vec::new();
let array = value.get("result")?.get("artists")?.as_array()?;
for v in array.iter() {
if let Some(singer_info) = parse_singer_info(v) {
vec.push(singer_info);
}
}
return Some(vec);
}
}
None
}
fn parse_singer_info(v: &Value) -> Option<SingerInfo> {
Some(SingerInfo {
id: v.get("id")?.as_u64()?,
name: v.get("name")?.as_str()?.to_owned(),
pic_url: v
.get("picUrl")
.unwrap_or(&json!(""))
.as_str()
.unwrap_or("")
.to_owned(),
})
}
#[derive(Debug, Deserialize, Serialize)]
pub struct SongUrl {
pub id: u64,
pub url: String,
pub rate: u64,
}
pub fn to_song_url(json: &str) -> Option<Vec<SongUrl>> {
if let Ok(value) = serde_json::from_str::<Value>(json) {
if value.get("code")?.eq(&200) {
let mut vec: Vec<SongUrl> = Vec::new();
let array = value.get("data")?.as_array()?;
for v in array.iter() {
if let Some(url) = parse_song_url(v) {
vec.push(url);
}
}
return Some(vec);
}
}
None
}
fn parse_song_url(v: &Value) -> Option<SongUrl> {
let url = v
.get("url")
.unwrap_or(&json!(""))
.as_str()
.unwrap_or("")
.to_owned();
if !url.is_empty() {
return Some(SongUrl {
id: v.get("id")?.as_u64()?,
url,
rate: v.get("br")?.as_u64()?,
});
}
None
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct SongInfo {
pub id: u64,
pub name: String,
pub singer: String,
pub album: String,
pub pic_url: String,
pub duration: String,
pub song_url: String,
}
pub fn to_song_info(json: &str, parse: Parse) -> Option<Vec<SongTag>> {
if let Ok(value) = serde_json::from_str::<Value>(json) {
if value.get("code")?.eq(&200) {
let mut vec: Vec<SongTag> = Vec::new();
if let Parse::Search = parse {
let array = value.get("result")?.as_object()?.get("songs")?.as_array()?;
for v in array.iter() {
if let Some(item) = parse_song_info(v) {
vec.push(item);
}
}
}
return Some(vec);
}
}
None
}
fn parse_song_info(v: &Value) -> Option<SongTag> {
Some(SongTag {
artist: Some(
v.get("artists")?
.get(0)?
.get("name")
.unwrap_or(&json!("Unknown Artist"))
.as_str()
.unwrap_or("Unknown Artist")
.to_owned(),
),
title: Some(v.get("name")?.as_str()?.to_owned()),
album: Some(
v.get("album")?
.get("name")
.unwrap_or(&json!("Unknown Album"))
.as_str()
.unwrap_or("Unknown Album")
.to_owned(),
),
lang_ext: Some(String::from("netease")),
lyric_id: Some(v.get("id")?.as_u64()?.to_string()),
song_id: Some(v.get("id")?.as_u64()?.to_string()),
service_provider: Some(ServiceProvider::Netease),
url: Some(
if v.get("fee")?.as_u64()? == 0 {
"Downloadable"
} else {
"No copyright"
}
.to_string(),
),
pic_id: Some(
v.get("album")?
.get("picId")
.unwrap_or(&json!("Unknown"))
.as_u64()?
.to_string(),
),
album_id: Some(
v.get("album")?
.get("picId")
.unwrap_or(&json!("Unknown"))
.as_u64()?
.to_string(),
),
})
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct SongList {
pub id: u64,
pub name: String,
pub cover_img_url: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct LoginInfo {
pub code: i32,
pub uid: u64,
pub nickname: String,
pub avatar_url: String,
pub msg: String,
}
#[allow(unused)]
#[derive(Clone, Copy, Debug)]
pub enum Method {
Post,
Get,
}
#[allow(unused)]
#[derive(Debug, Clone, Copy)]
pub enum Parse {
Search,
Usl,
}