static void WhatIfExamples() { // Get the first What If article WhatIfArticle first = WhatIf.GetFirstArticle(); // Get the latest What If article WhatIfArticle latest = WhatIf.GetLatestArticle(); // Get a random What If article WhatIfArticle random = WhatIf.GetRandomArticle(); // Get a specific What If article by ID WhatIfArticle fortyTwo = WhatIf.GetArticle(42); // Get the next article WhatIfArticle fortyThree = fortyTwo.Next(); // Get the previous article WhatIfArticle fortyOne = fortyTwo.Previous(); // There's nothing before the first article WhatIfArticle badFirst = first.Previous(); // ...and nothing after the latest WhatIfArticle badLast = latest.Next(); // Get the dictionary of What If article entries, keyed on article ID Dictionary <int, WhatIfArchiveEntry> dict = WhatIf.WhatIfDictionary; // Refresh the article dictionary WhatIf.RefreshWhatIfDictionary(); }
/// <summary> /// Get a What If article by ID /// </summary> /// <param name="id">Article ID</param> /// <returns>What If article if found, null otherwise</returns> public static WhatIfArticle GetArticle(int id) { WhatIfArticle article = XkcdDatabase.GetWhatIfArticle(id); if (article != null) { return(article); } if (WhatIfDictionary.Keys.Contains(id)) { return(GetArticle(WhatIfDictionary[id])); } return(null); }
/// <summary> /// Inserts or updates a What If article in the database /// </summary> /// <param name="comic">Article to insert or update</param> internal static void InsertOrUpdateWhatIfArticle(WhatIfArticle article) { using (var db = new LiteDatabase(DatabaseLocation)) { ILiteCollection <WhatIfArticle> collection = db.GetCollection <WhatIfArticle>(WhatIfCollectionName); if (collection.FindById(article.Id) != null) { collection.Update(article); } else { collection.Insert(article); } collection.EnsureIndex(x => x.Id); } }
/// <summary> /// Gets a What If Article from an archive entry /// </summary> /// <param name="archiveEntry">Archive entry</param> /// <returns>Whar If Article</returns> public static WhatIfArticle GetArticle(WhatIfArchiveEntry archiveEntry) { WhatIfArticle article = XkcdDatabase.GetWhatIfArticle(archiveEntry.Id); if (article != null) { return(article); } var web = new HtmlWeb(); HtmlDocument doc = web.Load(archiveEntry.PermaLink); HtmlNode articleContentNode = doc.DocumentNode.Descendants().FirstOrDefault(x => x.Name == "article" && x.HasClass("entry")); article = new WhatIfArticle { Id = archiveEntry.Id, PermaLink = archiveEntry.PermaLink, Title = archiveEntry.Title, Date = archiveEntry.Date, Html = articleContentNode.OuterHtml }; XkcdDatabase.InsertOrUpdateWhatIfArticle(article); return(article); }