/// <summary> /// Doing search query with given parameters. Returns a List<> object. /// </summary> /// <param name="queryString"></param> /// <param name="queryPages"></param> /// <returns></returns> public List <VideoInformation> SearchQuery(string queryString, int queryPages) { items = new List <VideoInformation>(); webclient = new WebClient { Encoding = System.Text.Encoding.UTF8 }; // Do search for (int i = 1; i <= queryPages; i++) { // Search address string html = webclient.DownloadString("https://www.youtube.com/results?search_query=" + queryString + "&page=" + i + "&sp=EgIQAQ%253D%253D"); // Search string string pattern = "<div class=\"yt-lockup-content\">.*?title=\"(?<NAME>.*?)\".*?</div></div></div></li>"; //THIS PATTERN SPLITS SEARCH RESULT DIVS BY VIDEOS. EACH MEMBER OF result IS A SEPERATE VIDEO RESULT MatchCollection result = Regex.Matches(html, pattern, RegexOptions.Singleline); for (int ctr = 0; ctr <= result.Count - 1; ctr++) { // Title title = result[ctr].Groups[1].Value; // Author // yt-uix-sessionlink spf-link \" data-sessionlink=\"itct= // </a></div><div class=\"yt-lockup-meta \"> author = VideoItemHelper.Cull(result[ctr].Value, "yt-uix-sessionlink spf-link \" data-sessionlink=\"itct=", "</a>"); //.Substring(44); //.Replace('"', ' ').TrimStart().TrimEnd(); author = author.Substring(45); // Duration duration = VideoItemHelper.Cull(VideoItemHelper.Cull(result[ctr].Value, "id=\"description-id-", "span"), ": ", ".<"); // Url url = string.Concat("http://www.youtube.com/watch?v=", VideoItemHelper.Cull(result[ctr].Value, "watch?v=", "\"")); // Thumbnail thumbnail = "https://i.ytimg.com/vi/" + VideoItemHelper.Cull(result[ctr].Value, "watch?v=", "\"") + "/mqdefault.jpg"; //// Remove playlists //if (title != "__title__") //{ // if (duration != "") // { // // Add item to list // items.Add(new VideoInformation() { Title = title, Url = url, Duration = duration, Thumbnail = thumbnail, Author = author }); // } //} items.Add(new VideoInformation() { Title = title, Url = url, Duration = duration, Thumbnail = thumbnail, Author = author }); } } return(items); }
/// <summary> /// Doing search query with given parameters. Returns a List<> object. /// </summary> /// <param name="querystring"></param> /// <param name="querypages"></param> /// <returns></returns> public List <VideoInformation> SearchQuery(string querystring, int querypages) { items = new List <VideoInformation>(); webclient = new WebClient(); // Do search for (int i = 1; i <= querypages; i++) { // Search address string html = webclient.DownloadString("https://www.youtube.com/results?search_query=" + querystring + "&page=" + i); // Search string string pattern = "<div class=\"yt-lockup-content\">.*?title=\"(?<NAME>.*?)\".*?</div></div></div></li>"; MatchCollection result = Regex.Matches(html, pattern, RegexOptions.Singleline); for (int ctr = 0; ctr <= result.Count - 1; ctr++) { // Title title = result[ctr].Groups[1].Value; // Author author = VideoItemHelper.cull(result[ctr].Value, "/user/", "class").Replace('"', ' ').TrimStart().TrimEnd(); // Description description = VideoItemHelper.cull(result[ctr].Value, "dir=\"ltr\" class=\"yt-uix-redirect-link\">", "</div>"); // Duration duration = VideoItemHelper.cull(VideoItemHelper.cull(result[ctr].Value, "id=\"description-id-", "span"), ": ", "<").Replace(".", ""); // Url url = string.Concat("http://www.youtube.com/watch?v=", VideoItemHelper.cull(result[ctr].Value, "watch?v=", "\"")); // Thumbnail thumbnail = "https://i.ytimg.com/vi/" + VideoItemHelper.cull(result[ctr].Value, "watch?v=", "\"") + "/mqdefault.jpg"; // Remove playlists if (title != "__title__") { if (duration != "") { // Add item to list items.Add(new VideoInformation() { Title = title, Author = author, Description = description, Duration = duration, Url = url, Thumbnail = thumbnail, }); } } } } return(items); }
private void ProcessPage(string htmlPage) { MatchCollection result = Regex.Matches(htmlPage, Pattern, RegexOptions.Singleline); for (int ctr = 0; ctr <= result.Count - 1; ctr++) { // Title title = result[ctr].Groups[1].Value; // Author author = VideoItemHelper.cull(result[ctr].Value, "/user/", "class").Replace('"', ' ').TrimStart().TrimEnd(); // Description description = VideoItemHelper.cull(result[ctr].Value, "dir=\"ltr\" class=\"yt-uix-redirect-link\">", "</div>"); // Duration duration = VideoItemHelper.cull(VideoItemHelper.cull(result[ctr].Value, "id=\"description-id-", "span"), ": ", "<").Replace(".", ""); // Url url = string.Concat(YtWatchUrl, VideoItemHelper.cull(result[ctr].Value, "watch?v=", "\"")); // Thumbnail thumbnail = YtThumbnailUrl + VideoItemHelper.cull(result[ctr].Value, "watch?v=", "\"") + "/mqdefault.jpg"; // Remove playlists if (title != "__title__") { if (duration != "") { // Add item to list items.Add(new VideoInformation() { Title = title, Author = author, Description = description, Duration = duration, Url = url, Thumbnail = thumbnail, }); } } } }
private void ProcessPage(string htmlPage) { MatchCollection result = Regex.Matches(htmlPage, Pattern, RegexOptions.Singleline); for (int ctr = 0; ctr <= result.Count - 1; ctr++) { if (result[ctr].Value.Contains("yt-uix-button-subscription-container\">") || result[ctr].Value.Contains("\"instream\":true")) { continue; // Don't add to the list of search results if the value is a channel or live stream. } // Title title = result[ctr].Groups[1].Value; // Author author = VideoItemHelper.cull(result[ctr].Value, "/user/", "class").Replace('"', ' ').TrimStart() .TrimEnd(); if (string.IsNullOrEmpty(author)) { author = VideoItemHelper.cull(result[ctr].Value, " >", "</a>"); if (string.IsNullOrEmpty(author)) { noAuthor = true; } } // Description description = VideoItemHelper.cull(result[ctr].Value, "dir=\"ltr\" class=\"yt-uix-redirect-link\">", "</div>"); if (string.IsNullOrEmpty(description)) { description = VideoItemHelper.cull(result[ctr].Value, "<div class=\"yt-lockup-description yt-ui-ellipsis yt-ui-ellipsis-2\" dir=\"ltr\">", "</div>"); if (string.IsNullOrEmpty(description)) { noDesc = true; } } // Duration duration = VideoItemHelper .cull(VideoItemHelper.cull(result[ctr].Value, "id=\"description-id-", "span"), ": ", "<") .Replace(".", ""); // Url url = string.Concat(YtWatchUrl, VideoItemHelper.cull(result[ctr].Value, "watch?v=", "\"")); // Thumbnail thumbnail = YtThumbnailUrl + VideoItemHelper.cull(result[ctr].Value, "watch?v=", "\"") + "/mqdefault.jpg"; // View Count { string strView = VideoItemHelper.cull(result[ctr].Value, "</li><li>", "</li></ul></div>"); if (!string.IsNullOrEmpty(strView) && !string.IsNullOrWhiteSpace(strView)) { string[] strParsedArr = strView.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); string parsedText = strParsedArr[0]; parsedText = parsedText.Trim().Replace(",", "."); viewCount = parsedText; } } // Remove playlists if (title != "__title__" && duration != "") { // Add item to list items.Add(new VideoInformation() { Title = title, Author = author, Description = description, Duration = duration, Url = url, Thumbnail = thumbnail, NoAuthor = noAuthor, NoDescription = noDesc, ViewCount = viewCount }); } // Reset values to default for next loop. noAuthor = false; noDesc = false; } }