/// <summary> /// QQ音乐搜索歌曲 /// </summary> /// <param name="Key"></param> /// <returns></returns> List <MusicInfo> QQSearch(string Key) { List <MusicInfo> res = new List <MusicInfo>(); string url = QQApiUrl + "search?key=" + Key + "&pageSize=60"; string resjson = ""; using (WebClientPro wc = new WebClientPro()) { StreamReader sr = new StreamReader(wc.OpenRead(url)); resjson = sr.ReadToEnd(); } QQMusicDetails.Root json = JsonConvert.DeserializeObject <QQMusicDetails.Root>(resjson); for (int i = 0; i < json.data.list.Count; i++) { string singers = ""; foreach (QQMusicDetails.singer singer in json.data.list[i].singer) { singers += singer.name + "、"; } singers = singers.Substring(0, singers.Length - 1); res.Add( new MusicInfo { Album = json.data.list[i].albumname, Id = json.data.list[i].songmid, Title = json.data.list[i].songname, LrcUrl = QQApiUrl + "lyric?songmid=" + json.data.list[i].songmid, PicUrl = "https://y.gtimg.cn/music/photo_new/T002R300x300M000" + json.data.list[i].albummid + ".jpg", Singer = singers, Api = 2, strMediaMid = json.data.list[i].strMediaMid }); } return(res); }
/// <summary> /// 获取qq音乐榜单 /// </summary> /// <param name="id">参考接口 /top/category </param> /// <returns></returns> public List <MusicInfo> GetQQTopList(string id) { string url = QQApiUrl + "top?id=" + id; using (WebClientPro wc = new WebClientPro()) { StreamReader sr = new StreamReader(wc.OpenRead(url)); QQTopList.Root json = JsonConvert.DeserializeObject <QQTopList.Root>(sr.ReadToEnd()); List <MusicInfo> re = new List <MusicInfo>(); for (int i = 0; i < json.data.list.Count; i++) { re.Add(new MusicInfo { Album = json.data.list[i].album.title, Api = 2, Id = json.data.list[i].mid, Singer = json.data.list[i].singerName, strMediaMid = json.data.list[i].file.media_mid, Title = json.data.list[i].title, LrcUrl = QQApiUrl + "lyric?songmid=" + json.data.list[i].mid, PicUrl = "https://y.gtimg.cn/music/photo_new/T002R300x300M000" + json.data.list[i].album.mid + ".jpg", }); } return(re); } }
/// <summary> /// 获取更新数据 这个方法是获取程序更新信息 二次开发请修改 /// </summary> /// <returns></returns> public string Update() { WebClientPro wc = new WebClientPro(); StreamReader sr = null; try { sr = new StreamReader(wc.OpenRead(UpdateJsonUrl)); // 读取一个在线文件判断接口状态获取网易云音乐Cookie,可以写死 } catch { return("Error"); } Update update = JsonConvert.DeserializeObject <Update>(sr.ReadToEnd()); if (update.Cookie != null) { _cookie = update.Cookie; cookie = update.Cookie; } bool needupdate = true; if (update.Version[0] < version[0]) { needupdate = false; } else if (update.Version[0] == version[0]) { if (update.Version[1] < version[1]) { needupdate = false; } else if (update.Version[1] == version[1]) { if (update.Version[2] < version[2]) { needupdate = false; } else if (update.Version[2] == version[2]) { needupdate = false; } } } if (needupdate) { return("Needupdate"); } else { return(""); } }
/// <summary> /// 网易云音乐带cookie访问 /// </summary> /// <param name="url"></param> /// <returns></returns> string GetHTML(string url) { try { WebClientPro wc = new WebClientPro(); wc.Headers.Add(HttpRequestHeader.Cookie, cookie); Stream s = wc.OpenRead(url); StreamReader sr = new StreamReader(s); return(sr.ReadToEnd()); } catch { return(null); } }
/// <summary> /// 获取更新数据 这个方法是获取程序更新信息 二次开发请修改 /// </summary> /// <returns></returns> public void Update() { WebClientPro wc = new WebClientPro(); StreamReader sr = null; try { sr = new StreamReader(wc.OpenRead(UpdateJsonUrl)); // 读取一个在线文件判断接口状态获取网易云音乐Cookie,可以写死 } catch { NotifyConnectError(); return; } Update update = JsonConvert.DeserializeObject <Update>(sr.ReadToEnd()); _cookie = update.Cookie; cookie = update.Cookie; bool needupdate = true; if (update.Version[0] < version[0]) { needupdate = false; } else if (update.Version[0] == version[0]) { if (update.Version[1] < version[1]) { needupdate = false; } else if (update.Version[1] == version[1]) { if (update.Version[2] < version[2]) { needupdate = false; } else if (update.Version[2] == version[2]) { needupdate = false; } } } if (needupdate) { NotifyUpdate(); } }
/// <summary> /// 解析专辑 /// </summary> /// <param name="id"></param> /// <returns></returns> public List <MusicInfo> GetAlbum(string id, int api) { if (api == 1) { List <MusicInfo> res = new List <MusicInfo>(); string url = NeteaseApiUrl + "album?id=" + id; NeteaseAlbum.Root json; try { json = JsonConvert.DeserializeObject <NeteaseAlbum.Root>(GetHTML(url)); } catch { return(null); } for (int i = 0; i < json.songs.Count; i++) { string singer = ""; for (int x = 0; x < json.songs[i].ar.Count; x++) { singer += json.songs[i].ar[x].name + "、"; } MusicInfo mi = new MusicInfo() { Title = json.songs[i].name, Album = json.album.name, Id = json.songs[i].id.ToString(), LrcUrl = NeteaseApiUrl + "lyric?id=" + json.songs[i].id.ToString(), PicUrl = json.songs[i].al.picUrl + "?param=300y300", Singer = singer.Substring(0, singer.Length - 1), Api = 1 }; res.Add(mi); } return(res); } if (api == 2) { string url = QQApiUrl + "album/songs?albummid=" + id; using (WebClientPro wc = new WebClientPro()) { StreamReader sr = new StreamReader(wc.OpenRead(url)); string httpres = sr.ReadToEnd(); QQAlbum.Root json = null; try { json = JsonConvert.DeserializeObject <QQAlbum.Root>(httpres); } catch { return(null); } List <MusicInfo> res = new List <MusicInfo>(); if (json.data.list == null || json.data.list.Count == 0) { return(null); } for (int i = 0; i < json.data.list.Count; i++) { string singers = ""; foreach (QQAlbum.singer singer in json.data.list[i].singer) { singers += singer.title + "、"; } singers = singers.Substring(0, singers.Length - 1); MusicInfo mi = new MusicInfo() { Title = json.data.list[i].title, Album = json.data.list[i].album.title, Id = json.data.list[i].mid, LrcUrl = QQApiUrl + "lyric?songmid=" + json.data.list[i].mid, PicUrl = "https://y.gtimg.cn/music/photo_new/T002R300x300M000" + json.data.list[i].album.mid + ".jpg", Singer = singers, Api = 2, strMediaMid = json.data.list[i].ksong.mid }; res.Add(mi); } return(res); } } return(null); }
/// <summary> ///解析歌单,为了稳定每次请求100歌曲信息,所以解析歌单的方法分为两部分,这个方法根据歌曲数量分解请求 /// </summary> public List <MusicInfo> GetMusicList(string Id, int api) { if (api == 1) { Musiclist.Root musiclistjson = new Musiclist.Root(); try { musiclistjson = JsonConvert.DeserializeObject <Musiclist.Root>(GetHTML(NeteaseApiUrl + "playlist/detail?id=" + Id)); } catch { return(null); } string ids = ""; for (int i = 0; i < musiclistjson.playlist.trackIds.Count; i++) { ids += musiclistjson.playlist.trackIds[i].id.ToString() + ","; } ids = ids.Substring(0, ids.Length - 1); if (musiclistjson.playlist.trackIds.Count > 100) { string[] _id = ids.Split(','); int times = musiclistjson.playlist.trackIds.Count / 100; int remainder = musiclistjson.playlist.trackIds.Count % 100; if (remainder != 0) { times++; } List <MusicInfo> re = new List <MusicInfo>(); for (int i = 0; i < times; i++) { string _ids = ""; if (i != times - 1) { for (int x = 0; x < 100; x++) { _ids += _id[i * 100 + x] + ","; } } else { for (int x = 0; x < remainder; x++) { _ids += _id[i * 100 + x] + ","; } } re.AddRange(_GetNeteaseMusicList(_ids.Substring(0, _ids.Length - 1))); } return(re); } else { return(_GetNeteaseMusicList(ids)); } } else if (api == 2) { string url = QQApiUrl + "songlist?id=" + Id; using (WebClientPro wc = new WebClientPro()) { StreamReader sr = new StreamReader(wc.OpenRead(url)); string httpres = sr.ReadToEnd(); if (httpres == null) { return(null); } QQmusiclist.Root json = JsonConvert.DeserializeObject <QQmusiclist.Root>(httpres); List <MusicInfo> re = new List <MusicInfo>(); if (json.data.songlist == null) { return(null); } for (int i = 0; i < json.data.songlist.Count; i++) { string singers = ""; foreach (QQmusiclist.singer singer in json.data.songlist[i].singer) { singers += singer.name + "、"; } singers = singers.Substring(0, singers.Length - 1); re.Add(new MusicInfo() { Album = json.data.songlist[i].albumname, Api = 2, Id = json.data.songlist[i].songmid, LrcUrl = QQApiUrl + "lyric?songmid=" + json.data.songlist[i].songmid, PicUrl = "https://y.gtimg.cn/music/photo_new/T002R300x300M000" + json.data.songlist[i].albummid + ".jpg", Singer = singers, strMediaMid = json.data.songlist[i].strMediaMid, Title = json.data.songlist[i].songname } ); } return(re); } } return(null); }
/// <summary> /// 下载线程 /// </summary> private void _Download() { while (downloadlist.Count != 0) { string Lrc = ""; downloadlist[0].State = "正在下载音乐"; if (downloadlist[0].Url == null) { downloadlist[0].State = "无版权"; UpdateDownloadPage(); downloadlist.RemoveAt(0); continue; } UpdateDownloadPage(); string savepath = ""; string filename = "";; switch (setting.SaveNameStyle) { case 0: if (downloadlist[0].Url.IndexOf("flac") != -1) { filename = NameCheck(downloadlist[0].Title) + " - " + NameCheck(downloadlist[0].Singer) + ".flac"; } else { filename = NameCheck(downloadlist[0].Title) + " - " + NameCheck(downloadlist[0].Singer) + ".mp3"; } break; case 1: if (downloadlist[0].Url.IndexOf("flac") != -1) { filename = NameCheck(downloadlist[0].Singer) + " - " + NameCheck(downloadlist[0].Title) + ".flac"; } else { filename = NameCheck(downloadlist[0].Singer) + " - " + NameCheck(downloadlist[0].Title) + ".mp3"; } break; } switch (setting.SavePathStyle) { case 0: savepath = setting.SavePath; break; case 1: savepath = setting.SavePath + "\\" + NameCheck(downloadlist[0].Singer); break; case 2: savepath = setting.SavePath + "\\" + NameCheck(downloadlist[0].Singer) + "\\" + NameCheck(downloadlist[0].Album); break; } if (!Directory.Exists(savepath)) { Directory.CreateDirectory(savepath); } if (downloadlist[0].IfDownloadMusic) { if (System.IO.File.Exists(savepath + "\\" + filename)) { downloadlist[0].State = "音乐已存在"; UpdateDownloadPage(); } else { using (WebClientPro wc = new WebClientPro()) { try { wc.DownloadFile(downloadlist[0].Url, savepath + "\\" + filename); } catch { downloadlist[0].State = "音乐下载错误"; downloadlist.RemoveAt(0); UpdateDownloadPage(); continue; } } } } if (downloadlist[0].IfDownloadLrc) { downloadlist[0].State = "正在下载歌词"; UpdateDownloadPage(); using (WebClientPro wc = new WebClientPro()) { try { if (downloadlist[0].Api == 1) { string savename = savepath + "\\" + filename.Replace(".flac", ".lrc").Replace(".mp3", ".lrc"); StreamReader sr = new StreamReader(wc.OpenRead(downloadlist[0].LrcUrl)); string json = sr.ReadToEnd(); NeteaseLrc.Root lrc = JsonConvert.DeserializeObject <NeteaseLrc.Root>(json); Lrc = lrc.lrc.lyric ?? ""; if (Lrc != "") { StreamWriter sw = new StreamWriter(savename); sw.Write(Lrc); sw.Flush(); sw.Close(); } else { downloadlist[0].State = "歌词下载错误"; UpdateDownloadPage(); } } else if (downloadlist[0].Api == 2) { string savename = savepath + "\\" + filename.Replace(".flac", ".lrc").Replace(".mp3", ".lrc"); StreamReader sr = new StreamReader(wc.OpenRead(downloadlist[0].LrcUrl)); string json = sr.ReadToEnd(); QQLrc.Root lrc = JsonConvert.DeserializeObject <QQLrc.Root>(json); Lrc = lrc.data.lyric ?? ""; if (Lrc != "") { StreamWriter sw = new StreamWriter(savename); sw.Write(Lrc); sw.Flush(); sw.Close(); } else { downloadlist[0].State = "歌词下载错误"; UpdateDownloadPage(); } } } catch { downloadlist[0].State = "歌词下载错误"; UpdateDownloadPage(); } } } if (downloadlist[0].IfDownloadPic) { downloadlist[0].State = "正在下载图片"; UpdateDownloadPage(); using (WebClientPro wc = new WebClientPro()) { try { wc.DownloadFile(downloadlist[0].PicUrl, savepath + "\\" + filename.Replace(".flac", ".jpg").Replace(".mp3", ".jpg")); } catch { downloadlist[0].State = "图片下载错误"; UpdateDownloadPage(); } } } if (filename.IndexOf(".mp3") != -1) { using (var tfile = TagLib.File.Create(savepath + "\\" + filename)) { //tfile.Tag.Title = downloadlist[0].Title; //tfile.Tag.Performers = new string[] { downloadlist[0].Singer }; //tfile.Tag.Album = downloadlist[0].Album; //if (downloadlist[0].IfDownloadLrc && Lrc != "" && Lrc != null) //{ // tfile.Tag.Lyrics = Lrc; //} if (downloadlist[0].IfDownloadPic && System.IO.File.Exists(savepath + "\\" + filename.Replace(".flac", "").Replace(".mp3", "") + ".jpg")) { Tool.PngToJpg(savepath + "\\" + filename.Replace(".flac", "").Replace(".mp3", "") + ".jpg"); TagLib.Picture pic = new TagLib.Picture(); pic.Type = TagLib.PictureType.FrontCover; pic.MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg; pic.Data = TagLib.ByteVector.FromPath(savepath + "\\" + filename.Replace(".flac", "").Replace(".mp3", "") + ".jpg"); tfile.Tag.Pictures = new TagLib.IPicture[] { pic }; } tfile.Save(); } } else { using (var tfile = TagLib.Flac.File.Create(savepath + "\\" + filename)) { tfile.Tag.Title = downloadlist[0].Title; tfile.Tag.Performers = new string[] { downloadlist[0].Singer }; tfile.Tag.Album = downloadlist[0].Album; if (downloadlist[0].IfDownloadLrc && Lrc != "" && Lrc != null) { tfile.Tag.Lyrics = Lrc; } if (downloadlist[0].IfDownloadPic && System.IO.File.Exists(savepath + "\\" + filename.Replace(".flac", "").Replace(".mp3", "") + ".jpg")) { Tool.PngToJpg(savepath + "\\" + filename.Replace(".flac", "").Replace(".mp3", "") + ".jpg"); TagLib.Picture pic = new TagLib.Picture(); pic.Type = TagLib.PictureType.FrontCover; pic.MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg; pic.Data = TagLib.ByteVector.FromPath(savepath + "\\" + filename.Replace(".flac", "").Replace(".mp3", "") + ".jpg"); tfile.Tag.Pictures = new TagLib.IPicture[] { pic }; } tfile.Save(); } } downloadlist[0].State = "下载完成"; UpdateDownloadPage(); downloadlist.RemoveAt(0); } }
/// <summary> /// 下载方法 /// </summary> /// <param name="dl"></param> public void Download(List <DownloadList> dl, int api) { string ids = ""; if (api == 1) { int times = dl.Count / 500; int remainder = dl.Count % 500; if (remainder == 0) { remainder = 500; } if (times == 0) { times = 1; } for (int i = 0; i < times; i++) { if (i == times - 1 && times >= 1) { ids = ""; for (int x = 0; x < remainder; x++) { ids += dl[i * 500 + x].Id + ","; } ids = ids.Substring(0, ids.Length - 1); string u = NeteaseApiUrl + "song/url?id=" + ids + "&br=" + dl[0].Quality; Json.GetUrl.Root urls = JsonConvert.DeserializeObject <Json.GetUrl.Root>(GetHTML(u)); for (int x = 0; x < remainder; x++) { for (int y = 0; y < dl.Count; y++) { if (urls.data[x].id.ToString() == dl[y].Id) { dl[y].Url = urls.data[x].url; dl[y].State = "准备下载"; } } } } else { ids = ""; for (int x = 0; x < 500; x++) { ids += dl[i * 500 + x].Id + ","; } ids = ids.Substring(0, ids.Length - 1); string u = NeteaseApiUrl + "song/url?id=" + ids + "&br=" + dl[0].Quality; Json.GetUrl.Root urls = JsonConvert.DeserializeObject <Json.GetUrl.Root>(GetHTML(u)); for (int x = 0; x < 100; x++) { for (int y = 0; y < dl.Count; y++) { if (urls.data[x].id.ToString() == dl[y].Id) { dl[y].Url = urls.data[x].url; dl[y].State = "准备下载"; } } } } } } else if (api == 2) { for (int i = 0; i < dl.Count; i++) { string url = QQApiUrl + "song/url?id=" + dl[i].Id + "&type=" + dl[i].Quality.Replace("128000", "128").Replace("320000", "320").Replace("999000", "flac") + "&mediaId=" + dl[i].strMediaMid; using (WebClientPro wc = new WebClientPro()) { StreamReader sr = new StreamReader(wc.OpenRead(url)); string httpjson = sr.ReadToEnd(); QQmusicdetails json = JsonConvert.DeserializeObject <QQmusicdetails>(httpjson); if (json.result != 100) { url = url.Replace("flac", "320").Replace("128", "320"); sr = new StreamReader(wc.OpenRead(url)); httpjson = sr.ReadToEnd(); json = JsonConvert.DeserializeObject <QQmusicdetails>(httpjson); if (url.IndexOf("flac") != -1 && json.result == 100) { dl[i].Quality = "320000"; } } dl[i].Url = json.data; dl[i].State = "准备下载"; } } } downloadlist.AddRange(dl); UpdateDownloadPage(); if (th_Download == null || th_Download?.ThreadState == ThreadState.Stopped) { th_Download = new Thread(_Download); th_Download.Start(); } }