// search and show private void SearchAndShow() { string query = HttpUtility.UrlPathEncode(searchQuery); string searchURL; XmlDocument xmlDoc; int total = 0; int page = 1; int totalPages = 1; if (searchSite == "sannhac.com") { searchURL = "http://sannhac.com/ajax.php?cmd=search&x_strSearch=" + query + "&typep=2&x_order=1&ord=ASC&page="; try { while (true) { if (page > totalPages) { break; } xmlDoc = GlobalCode.GetXML(searchURL + page); if (page == 1) { XmlNodeList xmlConfig = xmlDoc.GetElementsByTagName("config"); total = Convert.ToInt32(xmlConfig[0].Attributes["total"].Value); if (total == 0) { AddSearchResult(null); break; } totalPages = (int)Math.Ceiling((decimal)total / 25); } XmlNodeList xnlRec = xmlDoc.GetElementsByTagName("rec"); foreach (XmlNode node in xnlRec) { XmlAttributeCollection attr = node.Attributes; BeatInfo beatInfo = new BeatInfo(); beatInfo.Title = attr["song_name"].Value.Replace("&", "&"); beatInfo.Artist = attr["singer_name"].Value.Replace("&", "&"); beatInfo.Genre = SearchTab.StripHTML(attr["genre"].Value); beatInfo.Link = attr["linkSong"].Value; AddSearchResult(beatInfo); } ++page; } } catch (Exception ex) { StopSearching(); } } else { query = query.Replace("%20", "+"); string webContent; MatchCollection patternMatches; try { while (true) { searchURL = "http://star.zing.vn/star/search/search." + page + ".html?q=" + query; webContent = GlobalCode.GetContent(searchURL); if (page == 1) { // tìm tổng số trang patternMatches = Regex.Matches(webContent, @"href='/star/search/[^\.]+\.([0-9]+)\.html\?q=[^']+' class=''><img src='[^']+icon_lastpage\.gif'", RegexOptions.IgnoreCase); if (patternMatches.Count == 0) { totalPages = 1; } else { totalPages = Convert.ToInt32(patternMatches[0].Groups[1].Value); } } patternMatches = Regex.Matches(webContent, @"<tr class=""[^""]*"">[^<]+(<td ><a onMouseOut=""hideddrivetip\(\)""(?:[^<]+|<(?!/tr>))*)</tr>", RegexOptions.Multiline | RegexOptions.IgnoreCase); // ko có kết quả if (patternMatches.Count == 0) { AddSearchResult(null); break; } else { Match matchTemp; string[] data = new string[5]; foreach (Match matchRow in patternMatches) { data = new string[6]; matchTemp = Regex.Match(matchRow.Groups[1].Value, @"ddrivetip\('<b>([^<]+)</b>[\w\W]*\.([0-9]+)\.html[\w\W]*<a title=""([^""]+)""[\w\W]*>([^<]+)</a></td>", RegexOptions.Multiline | RegexOptions.IgnoreCase); BeatInfo beatInfo = new BeatInfo(); beatInfo.Title = matchTemp.Groups[1].Value.Replace(@"\'", "'").Replace("&", "&"); beatInfo.Artist = matchTemp.Groups[3].Value.Replace(@"\'", "'").Replace("&", "&"); beatInfo.Genre = matchTemp.Groups[4].Value.Replace("&", "&"); beatInfo.Link = matchTemp.Groups[2].Value; AddSearchResult(beatInfo); } } ++page; } } catch (Exception ex) { //MessageBox.Show(ex.Message); StopSearching(); } } StopSearching(); }
public void Download() { Int64 fileSize = 0; string fullSavePath = _SavePath.TrimEnd('\\') + @"\" + _BeatInfo.Artist + " - " + _BeatInfo.Title; try { if (m_EventStopThread.WaitOne(0, true)) { throw new Exception(); } // get beat if (_BeatInfo.Beat.Length == 0 || _BeatInfo.Beat == null) { string webContent; Match matchURL; if (_BeatInfo.Site == "sannhac.com") { // get content + ungzip webContent = GlobalCode.GetContent(_BeatInfo.Link, true); matchURL = Regex.Match(webContent, @"<div class=""ttct\-download""><a href=""([^""]+)""", RegexOptions.IgnoreCase); if (matchURL.Groups.Count != 0) { _BeatInfo.Beat = matchURL.Groups[1].Value; } } // zing else { webContent = GlobalCode.GetContent("http://star.zing.vn/includes/fnGetSongInfo.php?id=" + _BeatInfo.Link); matchURL = Regex.Match(webContent, @"<karaokelink>([^<]+)</karaokelink>", RegexOptions.IgnoreCase); if (matchURL.Groups.Count != 0) { _BeatInfo.Beat = matchURL.Groups[1].Value; } } } // download beat if (_BeatInfo.Beat.Length == 0 || _BeatInfo.Beat == null) { OnError(); return; } if (_BeatInfo.Beat.EndsWith(".mp3", StringComparison.CurrentCultureIgnoreCase) || _BeatInfo.Beat.EndsWith(".jpg", StringComparison.CurrentCultureIgnoreCase)) { _BeatInfo.BeatType = ".mp3"; } else { _BeatInfo.BeatType = ".swf"; } if (m_EventStopThread.WaitOne(0, true)) { throw new Exception(); } // DOWNLOAD using (WebClient wcDownload = new WebClient()) { webRequest = (HttpWebRequest)HttpWebRequest.Create(_BeatInfo.Beat); webRequest.Credentials = CredentialCache.DefaultCredentials; webRequest.KeepAlive = false; //webRequest.Timeout = 30000; webResponse = (HttpWebResponse)webRequest.GetResponse(); if (m_EventStopThread.WaitOne(0, true)) { throw new Exception(); } fileSize = webResponse.ContentLength; // Open the URL for download strResponse = webResponse.GetResponseStream(); OnUpdateProgress(new UpdateProgressEventArgs(fileSize, 0)); if (!m_EventStopThread.WaitOne(0, true)) { strLocal = new FileStream(fullSavePath + _BeatInfo.BeatType, FileMode.Create, FileAccess.Write, FileShare.None); int bytesSize = 0; byte[] downBuffer = new byte[2048]; while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0) { if (m_EventStopThread.WaitOne(0, true)) { m_EventThreadStopped.Set(); } else { // write to file strLocal.Write(downBuffer, 0, bytesSize); // update progress bar downloadedSize = strLocal.Length; OnUpdateProgress(new UpdateProgressEventArgs(fileSize, downloadedSize)); } } } } } catch { //m_EventThreadStopped.Set(); //MessageBox.Show(ex.Message); } finally { if (webRequest != null) { webRequest.Abort(); } if (strResponse != null) { strResponse.Close(); } if (strLocal != null) { strLocal.Close(); } if (m_EventThreadStopped.WaitOne(0, true)) { /* * try * { * //File.Delete(fullSavePath + _BeatInfo.BeatType); * } * catch * { * } */ OnStopped(); } else { bool hasError = (downloadedSize != fileSize); if (hasError) { OnError(); /* * try * { * //File.Delete(fullSavePath + _BeatInfo.BeatType); * } * catch * { * } */ } else { try { if (_BeatInfo.BeatType == ".swf") { MP3.ExtractFromSwf(fullSavePath); } MP3.UpdateInfo(fullSavePath, _BeatInfo); OnCompleted(); } catch { //File.Delete(fullSavePath + ".mp3"); OnError(); } } } } }