// 搜索按钮点击事件 private void searchBtn_Click(object sender, EventArgs e) { string id = textBox_id.Text.Trim(); if (id == "" || id == null || !CheckNum(id)) { MessageBox.Show("【警告】ID 为空或格式非法!", "提示"); return; } try { //歌曲类实例 Song song = new Song(); string lrc_url = string.Format(LRC_URL, id); HttpStatus status = GetSongLrc(HttpHelper(lrc_url), ref song); if (status.GetCode() != HTTP_STATUS_SUCCESS) { MessageBox.Show("搜索失败:" + status.GetMsg(), "异常"); return; } // 双语歌词处理 string[] lrcResult = ParserDiglossiaLrc(ref song); // 强制两位小数 if (dotCheckBox.CheckState == CheckState.Checked) { //设置时间戳两位小数 SetTimeStamp2Dot(ref lrcResult); } string song_url = string.Format(SONG_INFO_URL, id, id); status = GetSongBasicInfo(HttpHelper(song_url), ref song); if (status.GetCode() != HTTP_STATUS_SUCCESS) { MessageBox.Show("搜索失败:" + status.GetMsg(), "异常"); return; } currentSong = song; textBox_lrc.Text = TrimLrc(lrcResult); textBox_song.Text = song.GetName(); textBox_singer.Text = song.GetSinger(); } catch (Exception ew) { Console.WriteLine("搜索失败,错误信息:" + ew.StackTrace); MessageBox.Show("搜索失败,错误信息:\n" + ew.Message); } }
// 批量保存按钮点击事件 private void batchBtn_Click(object sender, EventArgs e) { string[] ids = idsTextbox.Text.Split(','); Dictionary <string, string> resultMaps = new Dictionary <string, string>(); Dictionary <string, string> failureMaps = new Dictionary <string, string>(); // 准备数据 foreach (string id in ids) { Song song = new Song(); try { if (id == "" || id == null || !CheckNum(id)) { throw new Exception("ID不合法"); } HttpStatus status = GetSongLrc(HttpHelper(string.Format(LRC_URL, id)), ref song); if (status.GetCode() == HTTP_STATUS_SUCCESS) { // 双语歌词处理 string[] lrcResult = ParserDiglossiaLrc(ref song); // 强制两位小数 if (dotCheckBox.CheckState == CheckState.Checked) { SetTimeStamp2Dot(ref lrcResult); } status = GetSongBasicInfo(HttpHelper(string.Format(SONG_INFO_URL, id, id)), ref song); if (status.GetCode() == HTTP_STATUS_SUCCESS) { string outputFileName = GetOutputFileName(ref song); resultMaps.Add(outputFileName, TrimLrc(lrcResult)); } else { throw new Exception(status.GetMsg()); } } else { throw new Exception(status.GetMsg()); } } catch (Exception ew) { failureMaps.Add(id, ew.Message); Console.WriteLine(ew); } } // 输出歌词缓存日志 StringBuilder log = new StringBuilder(); log.Append("---Total:" + ids.Length + ", Success:" + resultMaps.Count + ", Failure:" + failureMaps.Count + "---\r\n"); if (failureMaps.Count > 0) { foreach (KeyValuePair <string, string> kvp in failureMaps) { log.Append("ID: " + kvp.Key + ", Reason: " + kvp.Value + "\r\n"); } } textBox_lrc.Text = log.ToString(); // 保存 if (resultMaps.Count > 0) { SaveFileDialog saveDialog = new SaveFileDialog(); try { saveDialog.FileName = "直接选择保存路径即可,无需修改此处内容"; saveDialog.Filter = "lrc文件(*.lrc)|*.lrc|txt文件(*.txt)|*.txt"; if (saveDialog.ShowDialog() == DialogResult.OK) { string localFilePath = saveDialog.FileName.ToString(); // 获取文件后缀 string fileSuffix = localFilePath.Substring(localFilePath.LastIndexOf(".")); //获取文件路径,不带文件名 string filePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\")); foreach (KeyValuePair <string, string> kvp in resultMaps) { string path = filePath + "/" + GetSafeFilename(kvp.Key) + fileSuffix; StreamWriter sw = new StreamWriter(path, false, Encoding.GetEncoding(outputFileEncode)); sw.Write(kvp.Value); sw.Flush(); sw.Close(); } MessageBox.Show("保存成功!", "提示"); } } catch (Exception ew) { MessageBox.Show("批量保存失败,错误信息:\n" + ew.Message); } } }