/// <summary> /// Saves the episode by the specified file path. /// </summary> /// <param name="episodeFilePath">The episode file path.</param> /// <param name="episode">The episode.</param> /// <param name="overwrite">if set to <c>true</c> overwrites the existing file.</param> public static void SaveEpisode(string episodeFilePath, Episode episode, bool overwrite) { bool fileExists = File.Exists(episodeFilePath); if (!fileExists || overwrite) { string json = JsonConvert.SerializeObject(episode, Formatting.Indented); File.WriteAllText(episodeFilePath, json); } }
/// <summary> /// Gets the episode content items. /// </summary> /// <param name="episode">The episode.</param> /// <returns>The list of all content items of the episode.</returns> public static List<Item> GetEpisodeContentItems(Episode episode) { List<Item> items = new List<Item>(); items.AddRange(episode.News.Articles); items.AddRange(episode.Tools.Articles); items.AddRange(episode.Novices.Articles); items.AddRange(episode.Videos.Articles); items.AddRange(episode.Blogs.Articles); items.AddRange(episode.Events.Articles); return items; }
/// <summary> /// Tries the get episode from blog. // Simple parse, puts all links in the Blog category. // TODO: Parse html into an Episode using proper categories /// </summary> /// <param name="episodePostUrl">The episode post URL.</param> /// <param name="episode">The episode.</param> private void TryGetEpisodeFromBlog(string episodePostUrl, out Episode episode) { try { string pageContent = DownloadPage(episodePostUrl); List<string> linksList = new List<string>(); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(pageContent); foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]")) { string blogLink = link.GetAttributeValue("href", string.Empty); blogLink = GetCleanUrl(blogLink); if (!linksList.Contains(blogLink)) linksList.Add(blogLink); } episode = new Episode(); foreach (string link in linksList) { if (IsContentLink(link)) { episode.Blogs.Articles.Add(new ContentItem { Url = link }); } } episode.DigestTitle = episodePostUrl; } catch (ArgumentException) { ConsoleHelper.Warning("Episode with the Url: {0} not found", episodePostUrl); episode = null; } }
/// <summary> /// Displays possible stop words in the episode. /// </summary> /// <param name="episode">The last episode.</param> /// <param name="stopWords">The stop words.</param> private void DisplayStopWords(Episode episode, string[] stopWords) { List<Item> episodeItems = EpisodeHelper.GetEpisodeContentItems(episode); string regexExpression = string.Empty; for (int i = 0; i < stopWords.Length; i++) { regexExpression += stopWords[i]; if (i != stopWords.Length - 1) { regexExpression += "|"; } } regexExpression = $@"\b({regexExpression})\b"; foreach (ContentItem contentItem in episodeItems) { FindStopWords("Title", contentItem.Title, regexExpression); FindStopWords("Text", contentItem.Text, regexExpression); } }
/// <summary> /// Displays the duplicates between links in the episode, if any found. /// </summary> /// <param name="episode">The episode.</param> private void DisplayDuplicates(Episode episode) { bool duplicatesFound = false; List<Item> episodeItems = EpisodeHelper.GetEpisodeContentItems(episode); foreach (ContentItem contentItem in episodeItems) { if (!string.IsNullOrEmpty(contentItem.Url)) { string url = GetCleanUrl(contentItem.Url.TrimEnd('/')); foreach (ContentItem ci in episodeItems) { if ((contentItem != ci) && !string.IsNullOrEmpty(contentItem.Url) && !string.IsNullOrEmpty(ci.Url)) { string url2 = GetCleanUrl(ci.Url.TrimEnd('/')); if (url.Equals(url2, StringComparison.OrdinalIgnoreCase)) { ConsoleHelper.Warning("Current episode titles: \"{0}\" and \"{1}\"", contentItem.Title, ci.Title); duplicatesFound = true; } } } } } if (!duplicatesFound) { ConsoleHelper.Success("Yay! No duplicates found."); } }
/// <summary> /// Displays the duplicates. /// </summary> /// <param name="episode">The episode.</param> /// <param name="episodesList">The episodes list.</param> /// <returns></returns> private void DisplayDuplicates(Episode episode, List<Episode> episodesList) { bool duplicatesFound = false; foreach (Episode pastEpisode in episodesList) { List<Item> pastEpisodeItems = EpisodeHelper.GetEpisodeContentItems(pastEpisode); List<Item> episodeItems = EpisodeHelper.GetEpisodeContentItems(episode); foreach (ContentItem pastEpisodeItem in pastEpisodeItems) { string pastEpisodeUrl = GetCleanUrl(pastEpisodeItem.Url.TrimEnd('/')); foreach (ContentItem episodeItem in episodeItems) { if (!string.IsNullOrEmpty(episodeItem.Url)) { string episodeUrl = GetCleanUrl(episodeItem.Url.TrimEnd('/')); if (pastEpisodeUrl.Equals(episodeUrl, StringComparison.OrdinalIgnoreCase)) { ConsoleHelper.Warning("Ep. {0}, Url {1}. Current episode title: {2}".PadRight(10), pastEpisode.Number, pastEpisodeItem.Url, episodeItem.Title); duplicatesFound = true; } } } } } if (!duplicatesFound) { ConsoleHelper.Success("Yay! No duplicates found."); } }