LrcFetcherPage.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Net;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Web;
  6. using System.Windows.Forms;
  7. namespace LrcTool
  8. {
  9. public partial class LrcFetcherPage : UserControl
  10. {
  11. public LrcFetcherPage()
  12. {
  13. InitializeComponent();
  14. }
  15. private void PasteLinkLabel_Click(object sender, EventArgs e)
  16. {
  17. var text = Clipboard.GetText();
  18. var uri = new Uri(text);
  19. var queryString = HttpUtility.ParseQueryString(uri.Query);
  20. var songId = queryString["id"];
  21. SongUrlTextBox.Text = text;
  22. SongIdTextBox.Text = songId;
  23. LrcApiTextBox.Text = $"http://music.163.com/api/song/media?id={songId}";
  24. }
  25. private delegate void Action();
  26. private void FireButton_Click(object sender, EventArgs e)
  27. {
  28. var url = LrcApiTextBox.Text;
  29. FireButton.Enabled = false;
  30. new Thread(() =>
  31. {
  32. void Log(string text)
  33. {
  34. Invoke(new Action(() =>
  35. {
  36. LeftRichTextBox.AppendText($"{text}{Environment.NewLine}");
  37. LeftRichTextBox.SelectionStart = LeftRichTextBox.Text.Length;
  38. LeftRichTextBox.ScrollToCaret();
  39. }));
  40. }
  41. try
  42. {
  43. Log($"Fetching {url}");
  44. var json = new WebClient { Encoding = Encoding.UTF8 }.DownloadString(url);
  45. Log($"Result: {json}");
  46. var jsonObject = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(json);
  47. Log("Parsing...");
  48. var lrc = jsonObject.Value<string>("lyric");
  49. Log("Output...");
  50. Invoke(new Action(() => RightRichTextBox.Text = lrc));
  51. Log("OK");
  52. }
  53. catch (Exception exception)
  54. {
  55. Log("---- BEGIN EXCEPTION ----");
  56. Log(exception.ToString());
  57. Log("---- END EXCEPTION ----");
  58. }
  59. Invoke(new Action(() => FireButton.Enabled = true));
  60. }).Start();
  61. }
  62. private void CopyButton_Click(object sender, EventArgs e)
  63. {
  64. Clipboard.SetText(RightRichTextBox.Text);
  65. }
  66. }
  67. }