/// <summary> /// Search videos /// </summary> /// <param name="querystring"></param> /// <param name="querypages"></param> /// <returns></returns> public async Task <List <VideoSearchComponents> > GetVideos(string querystring, int querypages) { items = new List <VideoSearchComponents>(); // Do search for (int i = 1; i <= querypages; i++) { // Search address string content = await Web.getContentFromUrl("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(content, pattern, RegexOptions.Singleline); for (int ctr = 0; ctr <= result.Count - 1; ctr++) { if (Log.getMode()) { Log.println(Helper.Folder, "Match: " + result[ctr].Value); } // Title title = result[ctr].Groups[1].Value; if (Log.getMode()) { Log.println(Helper.Folder, "Title: " + title); } // Author author = Helper.ExtractValue(result[ctr].Value, "/user/", "class").Replace('"', ' ').TrimStart().TrimEnd(); if (string.IsNullOrEmpty(author)) { author = Helper.ExtractValue(result[ctr].Value, " >", "</a>"); } if (Log.getMode()) { Log.println(Helper.Folder, "Author: " + author); } // Description description = Helper.ExtractValue(result[ctr].Value, "dir=\"ltr\" class=\"yt-uix-redirect-link\">", "</div>"); if (string.IsNullOrEmpty(description)) { description = Helper.ExtractValue(result[ctr].Value, "<div class=\"yt-lockup-description yt-ui-ellipsis yt-ui-ellipsis-2\" dir=\"ltr\">", "</div>"); } if (Log.getMode()) { Log.println(Helper.Folder, "Description: " + description); } // Duration duration = Helper.ExtractValue(Helper.ExtractValue(result[ctr].Value, "id=\"description-id-", "span"), ": ", "<").Replace(".", ""); if (Log.getMode()) { Log.println(Helper.Folder, "Duration: " + duration); } // Url url = string.Concat("http://www.youtube.com/watch?v=", Helper.ExtractValue(result[ctr].Value, "watch?v=", "\"")); if (Log.getMode()) { Log.println(Helper.Folder, "Url: " + url); } // Thumbnail thumbnail = "https://i.ytimg.com/vi/" + Helper.ExtractValue(result[ctr].Value, "watch?v=", "\"") + "/mqdefault.jpg"; if (Log.getMode()) { Log.println(Helper.Folder, "Thumbnail: " + thumbnail); } // View count { string strView = Helper.ExtractValue(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; } } if (Log.getMode()) { Log.println(Helper.Folder, "Viewcount: " + viewcount); } // Remove playlists if (title != "__title__" && title != " ") { if (duration != "" && duration != " ") { // Add item to list items.Add(new VideoSearchComponents(Utilities.HtmlDecode(title), Utilities.HtmlDecode(author), Utilities.HtmlDecode(description), duration, url, thumbnail, viewcount)); } } } } return(items); }
public async Task <List <PlaylistSearchComponents> > GetPlaylistsPaged(string querystring, int querypagenum) { items = new List <PlaylistSearchComponents>(); // Do search // Search address string content = await Web.getContentFromUrlWithProperty("https://www.youtube.com/results?search_query=" + querystring.Replace(" ", "+") + "&sp=EgIQAw%253D%253D&page=" + querypagenum); // Search string string pattern = "playlistRenderer\":\\{\"playlistId\":\"(?<ID>.*?)\",\"title\":\\{\"simpleText\":\"(?<TITLE>.*?)\"},\"thumbnails\":\\[\\{\"thumbnails\":\\[\\{\"url\":\"(?<THUMBNAIL>.*?)\".*?videoCount\":\"(?<VIDEOCOUNT>.*?)\".*?\\{\"webCommandMetadata\":\\{\"url\":\"(?<URL>.*?)\".*?\"shortBylineText\":\\{\"runs\":\\[\\{\"text\":\"(?<AUTHOR>.*?)\""; MatchCollection result = Regex.Matches(content, pattern, RegexOptions.Singleline); for (int ctr = 0; ctr <= result.Count - 1; ctr++) { if (Log.getMode()) { Log.println(Helper.Folder, "Match: " + result[ctr].Value); } // Id Id = result[ctr].Groups[1].Value; if (Log.getMode()) { Log.println(Helper.Folder, "Id: " + Id); } // Title Title = result[ctr].Groups[2].Value.Replace(@"\u0026", "&");; if (Log.getMode()) { Log.println(Helper.Folder, "Title: " + Title); } // Author Author = result[ctr].Groups[6].Value.Replace(@"\u0026", "&");; if (Log.getMode()) { Log.println(Helper.Folder, "Author: " + Author); } // VideoCount VideoCount = result[ctr].Groups[4].Value; if (Log.getMode()) { Log.println(Helper.Folder, "VideoCount: " + VideoCount); } // Thumbnail Thumbnail = result[ctr].Groups[3].Value; if (Log.getMode()) { Log.println(Helper.Folder, "Thumbnail: " + Thumbnail); } // Url Url = "http://youtube.com" + result[ctr].Groups[5].Value; if (Log.getMode()) { Log.println(Helper.Folder, "Url: " + Url); } // Add item to list items.Add(new PlaylistSearchComponents(Id, Utilities.HtmlDecode(Title), Utilities.HtmlDecode(Author), VideoCount, Thumbnail, Url)); } return(items); }