// 搜索按钮点击事件 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); } }
// 获取歌曲原文歌词、译文歌词 public HttpStatus GetSongLrc(string jsonStr, ref Song song) { JObject obj = (JObject)JsonConvert.DeserializeObject(jsonStr); HttpStatus httpStatus = GetHttpStatus(obj); if (httpStatus.GetCode() == HTTP_STATUS_SUCCESS) { try { JToken lyric = obj["lrc"]["lyric"]; if (lyric != null) { song.SetLyric(lyric.ToString()); } JToken tlyric = obj["tlyric"]["lyric"]; if (tlyric != null) { song.SetTlyric(tlyric.ToString()); } } catch (Exception ew) { return(new HttpStatus(HTTP_STATUS_NOT_FOUND, "歌词不存在")); } } return(httpStatus); }
// 获取歌曲原文歌词、译文歌词 public HttpStatus GetSongLrc(string jsonStr, ref Song song) { JObject obj = (JObject)JsonConvert.DeserializeObject(jsonStr); HttpStatus httpStatus = GetHttpStatus(obj); if (httpStatus.GetCode() == HTTP_STATUS_SUCCESS) { JToken lyric = obj["lrc"]["lyric"]; if (lyric != null) { song.SetLyric(lyric.ToString()); } else { Console.WriteLine("歌词为空"); } JToken tlyric = obj["tlyric"]["lyric"]; if (tlyric != null) { song.SetTlyric(tlyric.ToString()); } else { Console.WriteLine("译文歌词为空"); } } return(httpStatus); }
// 获取歌曲名、歌手名 public HttpStatus GetSongBasicInfo(string jsonStr, ref Song song) { JObject obj = JObject.Parse(jsonStr); HttpStatus httpStatus = GetHttpStatus(obj); if (httpStatus.GetCode() == HTTP_STATUS_SUCCESS) { try { JToken songName = obj["songs"][0]["name"]; if (songName != null) { song.SetName(songName.ToString()); } else { return(new HttpStatus(HTTP_STATUS_NOT_FOUND, "歌曲不存在")); } } catch (Exception ew) { return(new HttpStatus(HTTP_STATUS_NOT_FOUND, "歌曲不存在")); } try { List <string> singerList = new List <string>(); var names = from staff in obj["songs"][0]["artists"].Children() select(string) staff["name"]; foreach (var name in names) { singerList.Add(name); } song.SetSinger(string.Join(",", singerList.ToArray())); } catch (Exception ew) { return(new HttpStatus(HTTP_STATUS_NOT_FOUND, "歌曲不存在")); } } return(httpStatus); }
// 批量保存按钮点击事件 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); } } }