/// <summary> /// Get the youtube url of a video from his hero handmade network episode page url /// </summary> /// <param name="url"></param> /// <returns></returns> async Task <string> GetDayYouTubeVideoUrl(string url) { string html = null, videoId = null; HtmlDocument doc = new HtmlDocument(); try { html = await NetRequestHelper.TryGetUrlHtmlContent(url); doc.LoadHtml(html); videoId = doc.DocumentNode.SelectSingleNode("//div[@id='player-wrapper']/div[@id='player']").Attributes["data-video-id"].Value; if (string.IsNullOrWhiteSpace(videoId)) { return(null); } return(YouTubeConstants.VideoBaseUrl + videoId); } catch (Exception) { throw; } finally { html = null; doc = null; videoId = null; } }
/// <summary> /// Record the video of a hero handmade network day tutorial /// </summary> async void RecordDayVideo() { // Get path Console.WriteLine("Where do you want to record this video ?"); string outputDirectoryPath = Console.ReadLine(); if (string.IsNullOrWhiteSpace(outputDirectoryPath) || !Directory.Exists(outputDirectoryPath)) { Console.WriteLine("An error occured"); return; } // Get day Console.WriteLine("Which day to your want ? (number of the day)"); string dayNumberString = Console.ReadLine(); int dayNumber = 0; if (string.IsNullOrWhiteSpace(dayNumberString) || !Int32.TryParse(dayNumberString, out dayNumber) || dayNumber <= 0) { Console.WriteLine("An error occured"); return; } string html = null, videoTitle = null, videoLink = null, youtubeUrl = null; HtmlDocument doc = new HtmlDocument(); HtmlNodeCollection nodes = null; HtmlDocument tmpDoc = new HtmlDocument(); try { WriteCreditsFiles(outputDirectoryPath); // Browse episode guide to get video url html = await NetRequestHelper.TryGetUrlHtmlContent(HandmadeNetworkConstants.EpisodeGuideUrl); if (string.IsNullOrWhiteSpace(html)) { Console.WriteLine("An error occured"); return; } doc.LoadHtml(html); if (doc == null) { Console.WriteLine("An error occured"); return; } nodes = doc.DocumentNode.SelectNodes("//div[@class='description']/*"); if (nodes == null || nodes.Count == 0) { Console.WriteLine("An error occured"); return; } foreach (HtmlNode nd in nodes) { if (nd.Name != "ul") { continue; } if (string.IsNullOrWhiteSpace(nd.InnerHtml)) { continue; } tmpDoc = new HtmlDocument(); tmpDoc.LoadHtml(nd.InnerHtml.Replace("\n", null)); if (tmpDoc.DocumentNode.ChildNodes.First().SelectNodes("//li") == null) { continue; } if (tmpDoc.DocumentNode.ChildNodes.First().SelectNodes("//li").Count == 0) { continue; } bool theGoodOne = false; bool keepBrowsingDays = true; for (int i = 0; i < tmpDoc.DocumentNode.ChildNodes.First().SelectNodes("//li").Count; i++) { if (!string.IsNullOrWhiteSpace(tmpDoc.DocumentNode.ChildNodes.First().SelectNodes("//li")[i].InnerText) && tmpDoc.DocumentNode.ChildNodes.First().SelectNodes("//li")[i].InnerText.ToLower().Contains("day")) { if (!keepBrowsingDays) { continue; } string[] dayArr2 = tmpDoc.DocumentNode.ChildNodes.First().SelectNodes("//li")[i].InnerText.Split(new char[] { '-', ':' }, StringSplitOptions.RemoveEmptyEntries); foreach (string s in dayArr2) { if (s.ToLower().Contains("day") && s.ToLower().Replace("day", null).Replace(" ", null).Length == 3) { int _i = 0; if (Int32.TryParse(s.ToLower().Replace("day", null), out _i) && _i == dayNumber) { videoLink = HandmadeNetworkUrlHelper.BuildDayEpisodeUrl(_i); theGoodOne = true; } } else if (theGoodOne) { videoTitle = s; keepBrowsingDays = false; } } } } } // Checks if (string.IsNullOrWhiteSpace(videoLink)) { throw new Exception(); } // Get youtube video url youtubeUrl = await GetDayYouTubeVideoUrl(videoLink); if (string.IsNullOrWhiteSpace(youtubeUrl)) { throw new Exception(); } // Download video if (SaveVideoOnDisk(youtubeUrl, outputDirectoryPath)) { Console.WriteLine("Video saved"); } else { Console.WriteLine("On error occurred"); } } catch (Exception ex) { Console.WriteLine("An error occured"); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } finally { html = null; doc = null; nodes = null; tmpDoc = null; videoTitle = null; videoLink = null; youtubeUrl = null; } }
/// <summary> /// Save all videos of a week /// </summary> async void RecordWeekVideos() { // Get path Console.WriteLine("Where do you want to record this video ?"); string outputDirectoryPath = Console.ReadLine(); if (string.IsNullOrWhiteSpace(outputDirectoryPath) || !Directory.Exists(outputDirectoryPath)) { Console.WriteLine("An error occured"); return; } // Get day Console.WriteLine("Which week to your want ? (number of the week)"); string weekNumberString = Console.ReadLine(); int weekNumber = 0; if (string.IsNullOrWhiteSpace(weekNumberString) || !Int32.TryParse(weekNumberString, out weekNumber) || weekNumber <= 0) { Console.WriteLine("An error occured"); return; } string html = null, weekTitle = null; HtmlDocument doc = new HtmlDocument(); HtmlNodeCollection nodes = null; HtmlDocument tmpDoc = new HtmlDocument(); try { WriteCreditsFiles(outputDirectoryPath); // Browse episode guide to get video url html = await NetRequestHelper.TryGetUrlHtmlContent(HandmadeNetworkConstants.EpisodeGuideUrl); if (string.IsNullOrWhiteSpace(html)) { Console.WriteLine("An error occured"); return; } doc.LoadHtml(html); if (doc == null) { Console.WriteLine("An error occured"); return; } nodes = doc.DocumentNode.SelectNodes("//div[@class='description']/*"); if (nodes == null || nodes.Count == 0) { Console.WriteLine("An error occured"); return; } bool curWeek = false; foreach (HtmlNode nd in nodes) { if (nd.Name == "h3") { if (string.IsNullOrWhiteSpace(nd.InnerText)) { continue; } if (!nd.InnerText.ToLower().Contains("week")) { continue; } if (nd.InnerText.Contains(':')) { if (weekNumber == Convert.ToInt32(nd.InnerText.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries)[0].ToLower().Replace("week ", null))) { weekTitle = nd.InnerText.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries)[1]; curWeek = true; } } else { if (weekNumber == Convert.ToInt32(nd.InnerText.ToLower().Replace("week ", null))) { curWeek = true; weekTitle = string.Empty; } } } else if (curWeek && nd.Name == "ul") { tmpDoc = new HtmlDocument(); tmpDoc.LoadHtml(nd.InnerHtml.Replace("\n", null)); for (int i = 0; i < tmpDoc.DocumentNode.ChildNodes.First().SelectNodes("//li").Count; i++) { string _dayVideoLink = null, _dayVideoTitle = null, _dayYouTubeUrl = null; string[] dayArr2 = tmpDoc.DocumentNode.ChildNodes.First().SelectNodes("//li")[i].InnerText.Split(new char[] { '-', ':' }, StringSplitOptions.RemoveEmptyEntries); foreach (string s in dayArr2) { if (s.ToLower().Contains("day") && s.ToLower().Replace("day", null).Replace(" ", null).Length == 3) { int _i = 0; if (Int32.TryParse(s.ToLower().Replace("day", null), out _i)) { _dayVideoLink = HandmadeNetworkUrlHelper.BuildDayEpisodeUrl(_i); } } else { _dayVideoTitle = s; } } _dayYouTubeUrl = await GetDayYouTubeVideoUrl(_dayVideoLink); SaveVideoOnDisk(_dayYouTubeUrl, outputDirectoryPath); _dayVideoLink = null; _dayVideoTitle = null; _dayYouTubeUrl = null; } } } } catch (Exception ex) { Console.WriteLine("An error occured"); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } finally { html = null; doc = null; nodes = null; tmpDoc = null; weekTitle = null; } }