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() {
vec.push(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(),
});
}
return Some(vec);
}
}
None
}
#[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() {
let url = v
.get("url")
.unwrap_or(&json!(""))
.as_str()
.unwrap_or("")
.to_owned();
if !url.is_empty() {
vec.push(SongUrl {
id: v.get("id")?.as_u64()?,
url,
rate: v.get("br")?.as_u64()?,
});
}
}
return Some(vec);
}
}
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<SongInfo> = Vec::new();
if let Parse::Search = parse {
let array = value.get("result")?.as_object()?.get("songs")?.as_array()?;
for v in array.iter() {
let duration = v.get("duration")?.as_u64()?;
let pic_id = v
.get("album")?
.get("picId")
.unwrap_or(&json!("Unknown"))
.as_u64()?;
let fee = v.get("fee")?.as_u64()?;
let url = if fee == 0 {
"Downloadable".to_string()
} else {
"Copyright Protected.".to_string()
};
vec.push(SongInfo {
id: v.get("id")?.as_u64()?,
name: v.get("name")?.as_str()?.to_owned(),
singer: v
.get("artists")?
.get(0)?
.get("name")
.unwrap_or(&json!("Unknown Artist"))
.as_str()
.unwrap_or("Unknown Artist")
.to_owned(),
album: v
.get("album")?
.get("name")
.unwrap_or(&json!("Unknown Album"))
.as_str()
.unwrap_or("Unknown Album")
.to_owned(),
pic_url: pic_id.to_string(),
duration: format!(
"{:0>2}:{:0>2}",
duration / 1000 / 60,
duration / 1000 % 60
),
song_url: url,
});
}
}
let mut song_tags: Vec<SongTag> = Vec::new();
for v in &vec {
let song_tag = SongTag {
artist: Some(v.singer.clone()),
title: Some(v.name.clone()),
album: Some(v.album.clone()),
lang_ext: Some(String::from("netease")),
lyric_id: Some(v.id.to_string()),
song_id: Some(v.id.to_string()),
service_provider: Some(ServiceProvider::Netease),
url: Some(v.song_url.clone()),
pic_id: Some(v.pic_url.clone()),
album_id: Some(v.pic_url.clone()),
};
song_tags.push(song_tag);
}
return Some(song_tags);
}
}
None
}
#[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,
}