// add search tab private void AddSearchTab(string query, string site) { // convert query to lower string query = query.ToLower(); int foundIndex = -1; int tabIconIndex = IconList.GetIconIndex(site); // find tab for (int i = 1; i < tabContainer.TabPages.Count; i++) { if (tabContainer.TabPages[i].Tag.ToString() == query && tabContainer.TabPages[i].ImageIndex == tabIconIndex) { foundIndex = i; break; } } if (foundIndex == -1) { SearchTab st = new SearchTab(query, site); TabPage tab = new TabPage(query); tab.ImageIndex = tabIconIndex; tab.Controls.Add(st); // add tab tabContainer.Controls.Add(tab); // focus tab tabContainer.SelectTab(tab); // resize tabContainer.TabPages[tabContainer.SelectedIndex].Controls[0].Size = tabContainer.TabPages[tabContainer.SelectedIndex].Size; // set header text tabContainer.TabPages[tabContainer.SelectedIndex].Tag = query; } else { // focus tab tabContainer.SelectedIndex = foundIndex; } }
// 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(); }