// 批量歌曲搜索 private void BatchSearch(string[] ids) { Dictionary <string, string> resultMaps = new Dictionary <string, string>(); foreach (string songIdStr in ids) { // 1、参数校验 long songId = NeteaseMusicUtils.CheckInputId(songIdStr, SEARCH_TYPE_ENUM.SONG_ID, out string inputErrorMsg); if (inputErrorMsg != ErrorMsg.SUCCESS) { resultMaps.Add(songIdStr, inputErrorMsg); continue; } // 2、获取歌曲内容 SaveVO result; if (NeteaseMusicResultCache.Contains(songId)) { result = NeteaseMusicResultCache.Get(songId); } else { SongVO songVO = RequestSongVO(songId, out string songErrorMsg); if (songErrorMsg != ErrorMsg.SUCCESS) { resultMaps.Add(songIdStr, songErrorMsg); continue; } LyricVO lyricVO = RequestLyricVO(songId, globalSearchInfo, out string lyricErrorMsg); if (lyricErrorMsg != ErrorMsg.SUCCESS) { resultMaps.Add(songIdStr, lyricErrorMsg); continue; } result = new SaveVO(songIdStr, songVO, lyricVO); NeteaseMusicResultCache.Put(songId, result); } // 3、加入结果集 globalSaveVOMap.Add(songIdStr, NeteaseMusicResultCache.Get(songId)); resultMaps.Add(songIdStr, ErrorMsg.SEARCH_RESULT_STAGE); } // 输出日志 StringBuilder log = new StringBuilder(); log.Append("---Total:" + resultMaps.Count + ", Success:" + globalSaveVOMap.Count + ", Failure:" + (resultMaps.Count - globalSaveVOMap.Count) + "---\r\n"); foreach (KeyValuePair <string, string> kvp in resultMaps) { log.Append("ID: " + kvp.Key + ", Result: " + kvp.Value + "\r\n"); } UpdateLrcTextBox(log.ToString()); }
// 单个歌曲搜索 private void SingleSearchBySongId(string songIdStr) { // 1、参数校验 long songId = NeteaseMusicUtils.CheckInputId(songIdStr, SEARCH_TYPE_ENUM.SONG_ID, out string inputErrorMsg); if (inputErrorMsg != ErrorMsg.SUCCESS) { MessageBox.Show(inputErrorMsg, "提示"); return; } // 2、获取歌曲内容 SaveVO result; if (NeteaseMusicResultCache.Contains(songId)) { result = NeteaseMusicResultCache.Get(songId); } else { SongVO songVO = RequestSongVO(songId, out string songErrorMsg); if (songErrorMsg != ErrorMsg.SUCCESS) { MessageBox.Show(songErrorMsg, "提示"); return; } LyricVO lyricVO = RequestLyricVO(songId, globalSearchInfo, out string lyricErrorMsg); if (lyricErrorMsg != ErrorMsg.SUCCESS) { MessageBox.Show(lyricErrorMsg, "提示"); return; } result = new SaveVO(songIdStr, songVO, lyricVO); NeteaseMusicResultCache.Put(songId, result); } // 3、加入结果集 globalSaveVOMap.Add(songIdStr, NeteaseMusicResultCache.Get(songId)); // 4、前端设置 textBox_song.Text = result.songVO.Name; textBox_singer.Text = result.songVO.Singer; textBox_album.Text = result.songVO.Album; UpdateLrcTextBox(""); }
// 单个保存 private void SingleSave(string songIdStr) { SaveFileDialog saveDialog = new SaveFileDialog(); try { SaveVO saveVO = new SaveVO(); if (!globalSaveVOMap.TryGetValue(songIdStr, out saveVO)) { MessageBox.Show(ErrorMsg.MUST_SEARCH_BEFORE_SAVE, "提示"); return; } string outputFileName = NeteaseMusicUtils.GetOutputName(saveVO.songVO, globalSearchInfo); if (outputFileName == null) { MessageBox.Show(ErrorMsg.FILE_NAME_IS_EMPTY, "提示"); return; } else { outputFileName = NeteaseMusicUtils.GetSafeFilename(outputFileName); } saveDialog.FileName = outputFileName; saveDialog.Filter = "lrc文件(*.lrc)|*.lrc|txt文件(*.txt)|*.txt"; if (saveDialog.ShowDialog() == DialogResult.OK) { StreamWriter sw = new StreamWriter(saveDialog.FileName, false, NeteaseMusicUtils.GetEncoding(globalSearchInfo.Encoding)); sw.Write(textBox_lrc.Text); sw.Flush(); sw.Close(); MessageBox.Show(ErrorMsg.SAVE_SUCCESS, "提示"); } } catch (Exception ew) { MessageBox.Show("保存失败!错误信息:\n" + ew.Message); } }
// 获取直链点击事件 private void songUrlBtn_Click(object sender, EventArgs e) { if (globalSaveVOMap == null || globalSaveVOMap.Count == 0) { MessageBox.Show(ErrorMsg.MUST_SEARCH_BEFORE_COPY_SONG_URL, "提示"); return; } if (globalSaveVOMap.Count > 1) { // 输出日志 StringBuilder log = new StringBuilder(); foreach (var songIdStr in globalSearchInfo.SearchIds) { if (globalSaveVOMap.ContainsKey(songIdStr)) { SaveVO saveVO = new SaveVO(); globalSaveVOMap.TryGetValue(songIdStr, out saveVO); log.Append("ID: " + songIdStr + ", Links: " + saveVO.songVO.Links + "\r\n"); } else { log.Append("ID: " + songIdStr + ", Links: failure\r\n"); } } UpdateLrcTextBox(log.ToString()); } else { // only loop one times foreach (var item in globalSaveVOMap) { Clipboard.SetDataObject(item.Value.songVO.Links); MessageBox.Show(ErrorMsg.SONG_URL_COPY_SUCESS, "提示"); } } }