/// <summary> /// 获取下载JSON信息 /// </summary> /// <param name="tf"></param> public static void GetJsonInfo(SynFileInfo tf) { try { string trackUrl = $"http://www.ximalaya.com/revision/play/tracks?trackIds={tf.DocID}"; var soundInfo = HttpHelper.HttpGet <TrackInfos>(trackUrl); if (soundInfo != null && soundInfo.data != null && soundInfo.data.tracksForAudioPlay != null && soundInfo.data.tracksForAudioPlay.Count > 0) { var downInfo = soundInfo.data.tracksForAudioPlay.FirstOrDefault(); tf.DownPath = downInfo.src; tf.DocName = downInfo.trackName; tf.Album = downInfo.albumName; tf.duration = downInfo.duration; tf.AlbumId = downInfo.albumId.ToString(); var savePath = FileHelper.GetSavePath(); FileHelper.MakeDirectory(savePath + "\\" + tf.Album + "\\"); tf.SavePath = $@"{savePath}\{tf.Album}\{tf.DocName}.mp3"; } } catch (Exception exp) { } }
/// <summary> /// 下载完成提示 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { SynFileInfo synFileInfo = (SynFileInfo)e.UserState; synFileInfo.isComplete = true; var repeatRow = this.m_downlist.Rows.Cast <DataGridViewRow>().FirstOrDefault(a => a.Cells["DocId"].Value.ToString() == synFileInfo.DocID); if (repeatRow != null) { repeatRow.DefaultCellStyle.ForeColor = Color.Green; } //该张专辑全部下载完后,字体变绿 if (m_DownloadList.Where(a => a.AlbumId == synFileInfo.AlbumId).All(a => a.isComplete)) { var parentNode = FindPendingNode(); var nodes = parentNode.Nodes.Find(synFileInfo.AlbumId, false); if (nodes.Count() > 0) { var currentNode = nodes.FirstOrDefault(); currentNode.ForeColor = Color.Green; } var albumItems = this.m_DownloadList.Where(a => a.AlbumId == synFileInfo.AlbumId).ToList(); DownloadHelper.UpdateList(albumItems); } this.StartNewWork(); }
/// <summary> /// 开始下载 /// </summary> /// <param name="sfi"></param> private void StartDownload(SynFileInfo sfi) { try { if (sfi.DownPath == null) { this.showinfo("下载地址不正确,该音频可能为付费音频,不能下载!"); sfi.isComplete = true; var repeatRow = this.m_downlist.Rows.Cast <DataGridViewRow>().FirstOrDefault(a => a.Cells["DocId"].Value.ToString() == sfi.DocID); if (repeatRow != null) { repeatRow.DefaultCellStyle.ForeColor = Color.Red; } this.StartNewWork(); } sfi.LastTime = DateTime.Now; WebClient webClient = new WebClient(); webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(this.client_DownloadProgressChanged); webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(this.client_DownloadFileCompleted); webClient.DownloadFileAsync(new Uri(sfi.DownPath), sfi.SavePath, sfi); } catch (Exception exp) { var content = "下载出现错误,该音频可能为付费音频,不能下载!若仍有问题,请联系作者!"; LogHelper.WriteLog("StartDownload" + content + exp.Message); this.showinfo(content); return; } }
private void AddToPendingList(SynFileInfo info) { var parentNode = FindPendingNode(); var key = string.Format("{0}_{1}", info.AlbumId, info.XmlName); if (!parentNode.Nodes.ContainsKey(key)) { parentNode.Nodes.Add(key, info.Album); } }
/// <summary> /// 分析单个 /// </summary> private void AnalyzeSigleDoc(TrackInfos soundInfo) { SynFileInfo tf = new SynFileInfo { Selected = true }; if (soundInfo != null && soundInfo.data != null && soundInfo.data.tracksForAudioPlay != null && soundInfo.data.tracksForAudioPlay.Count > 0) { var downInfo = soundInfo.data.tracksForAudioPlay.FirstOrDefault(); tf.DownPath = downInfo.src; tf.DocID = downInfo.trackId.ToString(); tf.DocName = downInfo.trackName; tf.Album = downInfo.albumName; tf.duration = downInfo.duration; tf.AlbumId = downInfo.albumId.ToString(); var savePath = FileHelper.GetSavePath(); FileHelper.MakeDirectory(savePath + "\\" + tf.Album + "\\"); tf.SavePath = $@"{savePath}\{tf.Album}\{tf.DocName}.mp3"; } string[] values = new string[] { "true", tf.DocID, tf.Album, tf.DocName }; //列表中不存在,才进行添加 if (!this.m_DownloadList.Any(a => a.DocID == tf.DocID)) { int index = this.m_downlist.Rows.Add(values); DataGridViewRow rowObject = this.m_downlist.Rows[index]; tf.RowObject = rowObject; tf.RowObject.Cells["duration"].Value = (tf.duration / 60).ToString() + "分钟"; this.m_DownloadList.Add(tf); } else { this.showinfo("列表中已经存在" + tf.DocID + "的声音"); } AddToPendingList(tf); return; }
/// <summary> /// 下载进度更新 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { try { SynFileInfo synFileInfo = (SynFileInfo)e.UserState; synFileInfo.SynProgress = e.ProgressPercentage + "%"; double totalSeconds = (DateTime.Now - synFileInfo.LastTime).TotalSeconds; synFileInfo.SynSpeed = FileOperate.GetAutoSizeString(Convert.ToDouble((double)e.BytesReceived / totalSeconds), 2) + "/s"; if (synFileInfo.RowObject.Cells["FileSize"].Value == null) { synFileInfo.FileSize = e.TotalBytesToReceive; synFileInfo.RowObject.Cells["FileSize"].Value = FileOperate.GetAutoSizeString((double)e.TotalBytesToReceive, 2); } synFileInfo.RowObject.Cells["SynProgress"].Value = synFileInfo.SynProgress; synFileInfo.RowObject.Cells["SynSpeed"].Value = synFileInfo.SynSpeed; } catch (Exception exp) { var content = "下载出现错误,请重试!若仍有问题,请联系作者!"; LogHelper.WriteLog("client_DownloadProgressChanged" + content + exp.Message); this.showinfo(content); } }
/// <summary> /// 开始新的下载 /// </summary> private void StartNewWork() { try { SynFileInfo next = this.GetNext(); if (next == null) { var msg = "所有文件下载完毕!"; if (!IsDownload) { msg = "已停止下载"; } MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } this.showinfo("分析:" + next.DocName); DownloadHelper.GetJsonInfo(next); next.RowObject.Cells["duration"].Value = (next.duration / 60).ToString() + "分钟"; this.showinfo("下载:" + next.DocName); //如果专辑名称不一样,则保存起来 if (next.Album != lastAlbum) { lastAlbum = next.Album; FileHelper.SaveAlbum(lastAlbum); } this.StartDownload(next); } catch (Exception exp) { var content = "下载出现错误,请重试!若仍有问题,请联系作者!"; LogHelper.WriteLog("StartNewWork" + content + exp.Message); this.showinfo(content); } }
/// <summary> /// 分析整张专辑 /// </summary> private void AnalyzeAlbum(string link) { try { var lstSynFileInfo = new List <SynFileInfo>(); var albumId = 0; var ids = link.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries).ToList(); albumId = int.Parse(ids.LastOrDefault()); var albumUrl = $@"http://www.ximalaya.com/revision/album?albumId={albumId}"; var albumInfo = HttpHelper.HttpGet <AlbumInfo>(albumUrl); //专辑 if (albumInfo != null && albumInfo.msg == "成功" && albumInfo.data != null) { var albumTitle = string.Empty; var totalTrack = 0; var pageSize = 30; var totalPage = 0; if (albumInfo != null && albumInfo.data != null) { albumId = albumInfo.data.albumId; if (albumInfo.data.mainInfo != null) { albumTitle = albumInfo.data.mainInfo.albumTitle; } if (albumInfo.data.tracksInfo != null) { totalTrack = albumInfo.data.tracksInfo.trackTotalCount; totalPage = totalTrack / pageSize + 1; } } for (int i = 1; i <= totalPage; i++) { var pageAlbumUrl = $"http://www.ximalaya.com/revision/album/getTracksList?albumId={albumId}&pageNum={i}"; var pageAlbumInfo = HttpHelper.HttpGet <PageAlbumInfo>(pageAlbumUrl); if (pageAlbumInfo != null && pageAlbumInfo.data != null && pageAlbumInfo.data.tracks != null && pageAlbumInfo.data.tracks.Count > 0) { foreach (var track in pageAlbumInfo.data.tracks) { if (!this.m_DownloadList.Any(a => a.DocID == track.trackId.ToString())) { int index2 = this.m_downlist.Rows.Add(new string[] { "true", track.trackId.ToString(), albumTitle, track.title }); DataGridViewRow rowObject2 = this.m_downlist.Rows[index2]; SynFileInfo item = new SynFileInfo { DocID = track.trackId.ToString(), DocName = track.title.Trim(), RowObject = rowObject2, Album = albumTitle, Selected = true, AlbumId = albumId.ToString() }; this.m_DownloadList.Add(item); } } } } } else//单曲 { string trackUrl = $"http://www.ximalaya.com/revision/play/tracks?trackIds={albumId}"; SynFileInfo tf = new SynFileInfo { Selected = true }; var soundInfo = HttpHelper.HttpGet <TrackInfos>(trackUrl); if (soundInfo != null && soundInfo.data != null) { AnalyzeSigleDoc(soundInfo); } } this.showinfo("共" + this.m_DownloadList.Count.ToString() + "条声音"); } catch (Exception exp) { var content = "内容解析失败,请查看链接地址是否正确!"; LogHelper.WriteLog(content + exp.Message); this.showinfo(content); } }