public int createXRAY() { //Process GUID. If in decimal form, convert to hex. if (Regex.IsMatch(guid, "/[a-zA-Z]/")) guid = guid.ToUpper(); else { long guidDec; long.TryParse(guid, out guidDec); guid = guidDec.ToString("X"); } if (guid == "0") { Console.WriteLine("Something bad happened while converting the GUID."); return 1; } Console.WriteLine("Downloading Shelfari page... {0}", useSpoilers ? "SHOWING SPOILERS!" : ""); //Download HTML of Shelfari URL, try 3 times just in case it fails the first time string shelfariHTML = ""; int tries = 3; do { try { //Enable cookies on extended webclient CookieContainer jar = new CookieContainer(); using (WebClientEx client = new WebClientEx(jar)) { //shelfariURL = "http://www.shelfari.com/books/25411/The-Path-of-Daggers"; if (useSpoilers) { //Grab book ID from url (search for 5 digits between slashes) and create spoiler cookie string bookID = Regex.Match(shelfariURL, @"\/\d{5}").Value.Substring(1, 5); Cookie spoilers = new Cookie("ShelfariBookWikiSession", "", "/", "www.shelfari.com"); spoilers.Value = "{\"SpoilerShowAll\":true%2C\"SpoilerShowCharacters\":true%2C\"SpoilerBookId\":" + bookID + "%2C\"SpoilerShowPSS\":true%2C\"SpoilerShowQuotations\":true%2C\"SpoilerShowParents\":true%2C\"SpoilerShowThemes\":true}"; jar.Add(spoilers); } shelfariHTML = client.DownloadString(shelfariURL); break; } } catch { if (tries <= 0) { Console.WriteLine("Failed to connect to Shelfari URL."); return 1; } } } while (tries-- > 0); /*** Constants for wiki processing ***/ Dictionary<string, string> sections = new Dictionary<string, string>{ {"WikiModule_Characters", "character"}, {"WikiModule_Organizations", "topic"}, {"WikiModule_Settings", "topic"}, {"WikiModule_Glossary", "topic"} }; //, {"WikiModule_Themes", "topic"} }; string[] patterns = { @"""" }; //, @"\[\d\]", @"\s*?\(.*\)\s*?" }; //Escape quotes, numbers in brackets, and anything within brackets at all string[] replacements = { @"\""" }; //, @"", @"" }; /************************************/ //Parse elements from various headers listed in sections HtmlAgilityPack.HtmlDocument shelfariDoc = new HtmlAgilityPack.HtmlDocument(); shelfariDoc.LoadHtml(shelfariHTML); foreach (string header in sections.Keys) { if (!shelfariHTML.Contains(header)) continue; //Skip section if not found on page //Select <li> nodes on page from within the <div id=header> tag, under <ul class=li_6> HtmlNodeCollection characterNodes = shelfariDoc.DocumentNode.SelectNodes("//div[@id='" + header + "']//ul[@class='li_6']/li"); foreach (HtmlNode li in characterNodes) { string tmpString = li.InnerText; Term newTerm = new Term(sections[header]); //Create term as either character/topic if (tmpString.Contains(":")) { newTerm.termName = tmpString.Substring(0, tmpString.IndexOf(":")); newTerm.desc = tmpString.Substring(tmpString.IndexOf(":") + 1).Trim(); } else { newTerm.termName = tmpString; } newTerm.termName = newTerm.termName.PregReplace(patterns, replacements); newTerm.desc = newTerm.desc.PregReplace(patterns, replacements); newTerm.descSrc = "shelfari"; //Use either the associated shelfari URL of the term or if none exists, use the book's url //Could use a wikipedia page instead as the xray plugin/site does but I decided not to newTerm.descUrl = (li.InnerHtml.IndexOf("<a href") == 0 ? li.InnerHtml.Substring(9, li.InnerHtml.IndexOf("\"", 9) - 9) : shelfariURL); if (header == "WikiModule_Glossary") newTerm.matchCase = false; //Default glossary terms to be case insensitive when searching through book terms.Add(newTerm); } } //Export list of Shelfari characters to a file to make it easier to create aliases or import the modified aliases if they exist //Could potentially just attempt to automate the creation of aliases, but in some cases it is very subjective... //For example, Shelfari shows the character "Artemis Fowl II", but in the book he is either referred to as "Artemis Fowl", "Artemis", or even "Arty" //Other characters have one name on Shelfari but can have completely different names within the book string aliasFile; if (aliaspath == "") aliasFile = Environment.CurrentDirectory + "\\ext\\" + asin + ".aliases"; else aliasFile = aliaspath; if (!File.Exists(aliasFile)) { saveCharacters(aliasFile); Console.WriteLine("Characters exported to {0} for adding aliases.", aliasFile); } else { loadAliases(aliasFile); Console.WriteLine("Character aliases read from {0}.", aliasFile); } Console.WriteLine("Terms found on Shelfari:"); foreach (Term t in terms) Console.Write("{0}, ", t.termName); if (!unattended) { Console.Write("\b\b \nContinue building using these terms (cancel if you want to add aliases)? (Y/N) "); string input = Console.ReadLine(); if (input.ToLower() != "y") return 1; } return 0; }
public int createXRAY() { //Process GUID. If in decimal form, convert to hex. if (Regex.IsMatch(guid, "/[a-zA-Z]/")) { guid = guid.ToUpper(); } else { long guidDec; long.TryParse(guid, out guidDec); guid = guidDec.ToString("X"); } if (guid == "0") { Console.WriteLine("Something bad happened while converting the GUID."); return(1); } Console.WriteLine("Downloading Shelfari page... {0}", useSpoilers ? "SHOWING SPOILERS!" : ""); //Download HTML of Shelfari URL, try 3 times just in case it fails the first time string shelfariHTML = ""; int tries = 3; do { try { //Enable cookies on extended webclient CookieContainer jar = new CookieContainer(); using (WebClientEx client = new WebClientEx(jar)) { //shelfariURL = "http://www.shelfari.com/books/25411/The-Path-of-Daggers"; if (useSpoilers) { //Grab book ID from url (search for 5 digits between slashes) and create spoiler cookie string bookID = Regex.Match(shelfariURL, @"\/\d{5}").Value.Substring(1, 5); Cookie spoilers = new Cookie("ShelfariBookWikiSession", "", "/", "www.shelfari.com"); spoilers.Value = "{\"SpoilerShowAll\":true%2C\"SpoilerShowCharacters\":true%2C\"SpoilerBookId\":" + bookID + "%2C\"SpoilerShowPSS\":true%2C\"SpoilerShowQuotations\":true%2C\"SpoilerShowParents\":true%2C\"SpoilerShowThemes\":true}"; jar.Add(spoilers); } shelfariHTML = client.DownloadString(shelfariURL); break; } } catch { if (tries <= 0) { Console.WriteLine("Failed to connect to Shelfari URL."); return(1); } } } while (tries-- > 0); /*** Constants for wiki processing ***/ Dictionary <string, string> sections = new Dictionary <string, string> { { "WikiModule_Characters", "character" }, { "WikiModule_Organizations", "topic" }, { "WikiModule_Settings", "topic" }, { "WikiModule_Glossary", "topic" } }; //, {"WikiModule_Themes", "topic"} }; string[] patterns = { @"""" }; //, @"\[\d\]", @"\s*?\(.*\)\s*?" }; //Escape quotes, numbers in brackets, and anything within brackets at all string[] replacements = { @"\""" }; //, @"", @"" }; /************************************/ //Parse elements from various headers listed in sections HtmlAgilityPack.HtmlDocument shelfariDoc = new HtmlAgilityPack.HtmlDocument(); shelfariDoc.LoadHtml(shelfariHTML); foreach (string header in sections.Keys) { if (!shelfariHTML.Contains(header)) { continue; //Skip section if not found on page } //Select <li> nodes on page from within the <div id=header> tag, under <ul class=li_6> HtmlNodeCollection characterNodes = shelfariDoc.DocumentNode.SelectNodes("//div[@id='" + header + "']//ul[@class='li_6']/li"); foreach (HtmlNode li in characterNodes) { string tmpString = li.InnerText; Term newTerm = new Term(sections[header]); //Create term as either character/topic if (tmpString.Contains(":")) { newTerm.termName = tmpString.Substring(0, tmpString.IndexOf(":")); newTerm.desc = tmpString.Substring(tmpString.IndexOf(":") + 1).Trim(); } else { newTerm.termName = tmpString; } newTerm.termName = newTerm.termName.PregReplace(patterns, replacements); newTerm.desc = newTerm.desc.PregReplace(patterns, replacements); newTerm.descSrc = "shelfari"; //Use either the associated shelfari URL of the term or if none exists, use the book's url //Could use a wikipedia page instead as the xray plugin/site does but I decided not to newTerm.descUrl = (li.InnerHtml.IndexOf("<a href") == 0 ? li.InnerHtml.Substring(9, li.InnerHtml.IndexOf("\"", 9) - 9) : shelfariURL); if (header == "WikiModule_Glossary") { newTerm.matchCase = false; //Default glossary terms to be case insensitive when searching through book } terms.Add(newTerm); } } //Export list of Shelfari characters to a file to make it easier to create aliases or import the modified aliases if they exist //Could potentially just attempt to automate the creation of aliases, but in some cases it is very subjective... //For example, Shelfari shows the character "Artemis Fowl II", but in the book he is either referred to as "Artemis Fowl", "Artemis", or even "Arty" //Other characters have one name on Shelfari but can have completely different names within the book string aliasFile; if (aliaspath == "") { aliasFile = Environment.CurrentDirectory + "\\ext\\" + asin + ".aliases"; } else { aliasFile = aliaspath; } if (!File.Exists(aliasFile)) { saveCharacters(aliasFile); Console.WriteLine("Characters exported to {0} for adding aliases.", aliasFile); } else { loadAliases(aliasFile); Console.WriteLine("Character aliases read from {0}.", aliasFile); } Console.WriteLine("Terms found on Shelfari:"); foreach (Term t in terms) { Console.Write("{0}, ", t.termName); } if (!unattended) { Console.Write("\b\b \nContinue building using these terms (cancel if you want to add aliases)? (Y/N) "); string input = Console.ReadLine(); if (input.ToLower() != "y") { return(1); } } return(0); }