private void btnAddUrl_Click(object sender, EventArgs e) { YoutubeVideoInfo newVid = YoutubeVideo.New(txtUrlBox.Text); //todo: add bad url handling labelVideoName.Text = newVid.Title; labelVideoDesc.Text = newVid.Description; Program.AppDownloadQueue.AddToQueue(newVid.Title, newVid.URL); //Console.WriteLine("Newvid: " + newVid.Title); }
/// <summary> /// Creates a new YoutubeVideoInfo from a URL /// </summary> /// <param name="URL">String youtube URL</param> /// <returns></returns> public static YoutubeVideoInfo New(string url) { YoutubeVideoInfo newInfo = new YoutubeVideoInfo(); newInfo.URL = url; //check if url is ok //get html string html; using (System.Net.WebClient client = new System.Net.WebClient()) { html = client.DownloadString(url); //todo: add exception handling } //get ytplayer's config var dataRegex = new Regex(@"ytplayer\.config\s*=\s*(\{.+?\});", RegexOptions.Multiline); string extractedJson = dataRegex.Match(html).Result("$1"); JObject ytConfig = JObject.Parse(extractedJson); //Get title newInfo.Title = extractVideoTitle(html); //parse description (todo: find out how to have the description save special unicode chars like emojis) newInfo.Description = extractVideoDescription(html); //Get download urls string[] streamMap = GetDownloadUrls(ytConfig).Split(','); int count = 0; foreach (string s in streamMap) { int itag = int.Parse(Regex.Match(s, @"(?<=itag=)(\d+)").Value); //gets itag from the url string FormattedUrl = ""; FormattedUrl = System.Net.WebUtility.HtmlDecode(FormattedUrl); FormattedUrl = System.Net.WebUtility.HtmlDecode(FormattedUrl); //Console.WriteLine(s); count++; } return(newInfo); }