private void AddSongDetailToListView(SongDetail song, string requestSongQuality) { ListViewItem item = new ListViewItem(); item.UseItemStyleForSubItems = false; Color foreColor = (song.Quality == requestSongQuality) ? Color.DarkBlue : (song.Quality == Utils.MP3_128kbps ? Color.DimGray : Color.DarkGreen); string lossless = song.VerifiedLossless ? Utils.VERIFIED : (song.MaximumQuality == Utils.FLAC_LOSSLESS ? Utils.NOT_SURE : Utils.NONE); Font losslessFont = null; // Bold if Verified Lossless if (lossless == Utils.VERIFIED) { losslessFont = new Font(lvSongs.Font, FontStyle.Bold); } else if (lossless == Utils.NONE) { losslessFont = new Font(lvSongs.Font, FontStyle.Italic); } else { losslessFont = lvSongs.Font; } item.SubItems.Add(song.FullName, foreColor, lvSongs.BackColor, lvSongs.Font); item.SubItems.Add(song.DownloadURL, foreColor, lvSongs.BackColor, lvSongs.Font); item.SubItems.Add(song.Quality, foreColor, lvSongs.BackColor, lvSongs.Font); item.SubItems.Add(lossless, foreColor, lvSongs.BackColor, losslessFont); if (song.MaximumQuality == Utils.FLAC_LOSSLESS) { losslessFont = new Font(lvSongs.Font, FontStyle.Bold); } else if (song.MaximumQuality == Utils.MP3_320kbps) { losslessFont = new Font(lvSongs.Font, FontStyle.Italic); } else if (song.MaximumQuality == Utils.MP3_128kbps) { losslessFont = new Font(lvSongs.Font, FontStyle.Strikeout); } else { losslessFont = lvSongs.Font; } item.SubItems.Add(song.MaximumQuality, foreColor, lvSongs.BackColor, losslessFont); item.ToolTipText = "Click on the '" + Utils.NOT_SURE + "' text (under Lossless? column) to view Spectrum of the song."; item.Checked = (requestSongQuality == song.Quality); item.Tag = song.Spectrum; lvSongs.Items.Add(item); Application.DoEvents(); item.EnsureVisible(); Application.DoEvents(); }
private void AddSongDetailToConsole(SongDetail song, string requestSongQuality) { if (!string.IsNullOrEmpty(song.FullName)) { txtConsole.AppendText(" - " + song.FullName); txtConsole.AppendText(Environment.NewLine); string lossless = song.VerifiedLossless ? Utils.VERIFIED : (song.MaximumQuality == Utils.FLAC_LOSSLESS ? Utils.NOT_SURE : Utils.NONE); txtConsole.AppendText(" - Request Quality: " + requestSongQuality + ". Maximum Quality: " + song.MaximumQuality + ". Lossless: " + lossless); } txtConsole.AppendText(Environment.NewLine); Application.DoEvents(); }
private void btnGetLinks_Click(object sender, EventArgs e) { Application.UseWaitCursor = true; Application.DoEvents(); EnableButtons(false); if (stopped) { listLoading = true; txtConsole.Text = string.Empty; txtResponse.Text = string.Empty; lvSongs.Items.Clear(); stopped = false; try { //txtLink.Text = "http://m2.chiasenhac.vn/mp3/vietnam/v-pop/tam-su-tuoi-30~trinh-thang-binh~tsvcsc0dqv4vnm.html"; string response = GetHttpWebResponse(txtLink.Text, null); txtResponse.Text = response; HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument(); htmlDoc.LoadHtml(response); // Song Quality: Lossless, 500kbps, 320kbps, 128kbps List <RadioButton> rbQuality = new List <RadioButton> { rbLossless, rb500kbps, rb320kbps, rb128kbps }; requestSongQuality = Utils.M4A_500kbps; foreach (RadioButton rb in rbQuality) { if (rb.Checked) { requestSongQuality = (string)rb.Tag; } } List <string> songUrls = GetSongUrls(htmlDoc); txtConsole.AppendText(Environment.NewLine); txtConsole.AppendText("============================================"); txtConsole.AppendText(Environment.NewLine); txtConsole.AppendText(albumName); txtConsole.AppendText(Environment.NewLine); txtConsole.AppendText("Maximum request song quality: " + requestSongQuality); txtConsole.AppendText(Environment.NewLine); txtConsole.AppendText("Songs list:"); txtConsole.AppendText(Environment.NewLine); txtConsole.AppendText("========================"); txtConsole.AppendText(Environment.NewLine); List <SongDetail> songs = new List <SongDetail>(); foreach (string songUrl in songUrls) { if (!string.IsNullOrEmpty(songUrl)) { response = GetHttpWebResponse(songUrl, null); txtResponse.Text = response; htmlDoc = new HtmlAgilityPack.HtmlDocument(); htmlDoc.LoadHtml(response); SongDetail song = GetSongDetail(htmlDoc, requestSongQuality, true); songs.Add(song); AddSongDetailToConsole(song, requestSongQuality); AddSongDetailToListView(song, requestSongQuality); } if (stopped) { break; } } } catch (Exception ex) { txtConsole.AppendText("==========================="); txtConsole.AppendText(Environment.NewLine); txtConsole.AppendText(ex.ToString()); txtConsole.AppendText(Environment.NewLine); txtConsole.AppendText("==========================="); txtConsole.AppendText(Environment.NewLine); MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { txtConsole.AppendText("============================================"); txtConsole.AppendText(Environment.NewLine); txtConsole.AppendText(Environment.NewLine); txtConsole.AppendText("Click on the '" + Utils.NOT_SURE + "' text (under Lossless? column) to view Spectrum of the song."); txtConsole.AppendText(Environment.NewLine); txtConsole.AppendText(Environment.NewLine); txtConsole.AppendText("============================================"); txtConsole.AppendText(Environment.NewLine); txtConsole.AppendText(Utils.COPYRIGHT); txtConsole.AppendText(Environment.NewLine); } } stopped = true; EnableButtons(true); listLoading = false; Application.UseWaitCursor = false; Application.DoEvents(); }
// Song Quality: Lossless, 500kbps, 320kbps, 128kbps private SongDetail GetSongDetail(HtmlAgilityPack.HtmlDocument htmlDoc, string songQuality, bool includeSpectrum) { SongDetail song = new SongDetail(); HtmlNode nameNode = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='plt-text']//a[last()]"); song.FullName = nameNode.InnerHtml; HtmlNode losslessNode = htmlDoc.DocumentNode.SelectSingleNode("//img[contains(@src, '/lossless_checked/')]"); song.VerifiedLossless = (losslessNode != null); foreach (string quality in Utils.SongQuality) { HtmlNode maxQualityNode = htmlDoc.DocumentNode.SelectSingleNode("//span[text()='" + quality + "']/parent::a"); if (maxQualityNode != null) { song.MaximumQuality = quality; break; } } HtmlNode downloadNode = htmlDoc.DocumentNode.SelectSingleNode("//span[text()='" + songQuality + "']/parent::a"); if (downloadNode != null) { song.DownloadURL = downloadNode.GetAttributeValue("href", string.Empty); song.Quality = songQuality; if (song.VerifiedLossless) { song.MaximumQuality = Utils.FLAC_LOSSLESS; } } else { downloadNode = htmlDoc.DocumentNode.SelectSingleNode("//span[text()='" + song.MaximumQuality + "']/parent::a"); if (downloadNode != null) { song.DownloadURL = downloadNode.GetAttributeValue("href", string.Empty); if (song.MaximumQuality == Utils.MP3_320kbps) { if (songQuality == Utils.FLAC_LOSSLESS) { song.DownloadURL = song.DownloadURL.Replace("/320/", "/flac/").Replace("[320kbps_MP3].mp3", "[Lossless_FLAC].flac"); } else if (songQuality == Utils.M4A_500kbps) { song.DownloadURL = song.DownloadURL.Replace("/320/", "/m4a/").Replace("[320kbps_MP3].mp3", "[500kbps_M4A].m4a"); } song.Quality = songQuality; } else { song.Quality = song.MaximumQuality; } if (song.VerifiedLossless) { song.MaximumQuality = Utils.FLAC_LOSSLESS; } } } song.DownloadURL = song.DownloadURL.Replace(" ", "%20"); // added to fix bug, cannot download from batch urls if (includeSpectrum && !song.VerifiedLossless && (song.MaximumQuality == Utils.FLAC_LOSSLESS)) { HtmlNode spectrumNode = htmlDoc.DocumentNode.SelectSingleNode("//img[contains(@src, '/spectrum/')]"); if (spectrumNode != null) // fixed nullpointer error { string spectrumUrl = spectrumNode.GetAttributeValue("src", string.Empty); MemoryStream ms = null; WebResponse myResponse = null; try { HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(spectrumUrl); myRequest.Method = "GET"; myResponse = myRequest.GetResponse(); ms = new MemoryStream(); myResponse.GetResponseStream().CopyTo(ms); song.Spectrum = ms.ToArray(); //song.Spectrum = Utils.ReadFully(myResponse.GetResponseStream()); } catch (Exception ex) { txtConsole.AppendText("==========================="); txtConsole.AppendText(Environment.NewLine); txtConsole.AppendText(ex.ToString()); txtConsole.AppendText(Environment.NewLine); txtConsole.AppendText("==========================="); txtConsole.AppendText(Environment.NewLine); } finally { if (ms != null) { ms.Close(); } if (myResponse != null) { myResponse.Close(); } } } } return(song); }