public static void loadCache(string cachePath, string cacheFile) { string line; string[] rslt; if (File.Exists(cachePath+"\\"+cacheFile)) { // Read the file . System.IO.StreamReader file = new System.IO.StreamReader(cachePath + "\\" + cacheFile); while ((line = file.ReadLine()) != null) { rslt = line.Split(splitChar); performerData pData = new performerData(); pData.name = rslt[0]; pData.pageUrl = rslt[1]; pData.thumbUrl = rslt[2]; performerCache.Add(rslt[0], pData); } file.Close(); } else { Console.ForegroundColor = ConsoleColor.Black; Console.BackgroundColor = ConsoleColor.Red; Console.WriteLine(Tag + "First run - no cache found"); Console.ResetColor(); } }
//Search for actor name and return the URL if found static string retrieveUrl(string name, int waitPeriod, string comingSoonPic) { performerData pData; if (Verbose) Console.WriteLine(Tag + "Performing search for actor " + name); //Check if we already have a result for actor name if (performerCache.ContainsKey(name)) { performerData cacheData; performerCache.TryGetValue(name, out cacheData); if (Verbose) Console.WriteLine(Tag + "Actor " + name + " found in the cache"); return cacheData.thumbUrl; } List<performerData> performer = new List<performerData>(); try { if (waitPeriod > 0) { System.Threading.Thread.Sleep(waitPeriod * 1000); } string sContents = Utils.PageFetch("http://www.adultdvdempire.com/performer/search?q=" + System.Web.HttpUtility.UrlEncode(name)); sContents = Utils.prepareString(sContents); if (sContents == "[timeout]") { Console.ForegroundColor = ConsoleColor.Black; Console.BackgroundColor = ConsoleColor.Red; Console.WriteLine(Tag + "it appears that Adult DVD Empire is offline. No search results for actor " + name); Console.ResetColor(); return String.Empty; } if (sContents.StartsWith("[exception: ")) { Console.ForegroundColor = ConsoleColor.Black; Console.BackgroundColor = ConsoleColor.Red; Console.WriteLine(Tag + "Adult DVD Empire is online, but experiencing technical difficulties. No search results for actor " + name); Console.ResetColor(); return String.Empty; } if (sContents.Contains("Your query didn't return any results")) { Console.ForegroundColor = ConsoleColor.Black; Console.BackgroundColor = ConsoleColor.Red; Console.WriteLine(Tag + "No serach results found for actor " + name); Console.ResetColor(); return String.Empty; } string QueryBlock = @"<a href=""([^""]*)"" class=""thumb""><img src=""([^""]*)"" alt=""([^""]*)"" title=""([^""]*)"""; Regex myLoopRegex = new Regex(QueryBlock, RegexOptions.IgnoreCase); foreach (Match myLoopMatch in myLoopRegex.Matches(sContents)) { pData = new performerData(); pData.pageUrl = "http://www.adultdvdempire.com/" + myLoopMatch.Groups[1].ToString(); pData.thumbUrl = myLoopMatch.Groups[2].ToString(); pData.name = Utils.UnHTML(myLoopMatch.Groups[3].ToString()); if (Verbose) Console.WriteLine(Tag + "Search returned a match for " + pData.name + " with HTML page " + pData.pageUrl); if (pData.thumbUrl.Contains(comingSoonPic)) { if (Verbose) Console.WriteLine(Tag + "Detected a 'Coming Soon Picture'...invalidated the thumbnail URL for " + pData.name); pData.thumbUrl = string.Empty; } performer.Add(pData); } /* Weird stuff going on - Query Result can use ' instead of " for the a href tags */ if (performer.Count == 0) { string QueryBlock2 = @"<a href=""([^""]*)"" class=""thumb""><img src='([^']*)' alt='([^']*)' title='([^']*)'"; myLoopRegex = new Regex(QueryBlock2, RegexOptions.IgnoreCase); foreach (Match myLoopMatch in myLoopRegex.Matches(sContents)) { pData = new performerData(); pData.pageUrl = "http://www.adultdvdempire.com/" + myLoopMatch.Groups[1].ToString(); pData.thumbUrl = myLoopMatch.Groups[2].ToString(); pData.name = Utils.UnHTML(myLoopMatch.Groups[3].ToString()); if (Verbose) Console.WriteLine(Tag + "Search returned a match for " + pData.name + " with HTML page " + pData.pageUrl); if (pData.thumbUrl.Contains(comingSoonPic)) { if (Verbose) Console.WriteLine(Tag + "Detected a 'Coming Soon Picture'...invalidated the thumbnail URL for " + pData.name); pData.thumbUrl = string.Empty; } performer.Add(pData); } } //multiple results - select one if (performer.Count > 1) { //Check if each found actor is unique SortedSet<string> uniqueSet = new SortedSet<string>(); bool uniqueResults = true; foreach (performerData d in performer) { if (uniqueSet.Add(d.name) == false) { if (Verbose) Console.WriteLine(Tag + "Search returned duplicate names for actor " + d.name); uniqueResults = false; break; //break from loop } } //Only store when all results are unique - otherwise we will have double results in the cache if (uniqueResults) { if (Verbose) Console.WriteLine(Tag + "Search results for actor " + name + " stored in cache"); foreach (performerData d in performer) { //store in cache if not already there if (!performerCache.ContainsKey(d.name)) performerCache.Add(d.name, d); } } else { if (Verbose) Console.WriteLine(Tag + "Search results not stored in cache"); } //ADVDE search will return all kinds of matches - try and see if our name has 1 exact match int equalCount = 0; performerData tmp = null; foreach (performerData d in performer) { if (d.name.Equals(name)) { equalCount++; tmp = d; } } if (equalCount == 1 && tmp != null) { Console.WriteLine(Tag + "Search returned more results but a unique match has been found for actor " + name); return tmp.thumbUrl; } //Nothing found or more then 1 match - Ask user what to select string s; Console.WriteLine(Tag + "More then 1 match found for actor '" + name + "'. Please select from list below:"); int count = 0; foreach (performerData d in performer) { s = String.Format("{0,-1}. {1,-40} {2}", count.ToString(), d.name, d.pageUrl); Console.WriteLine(s); count++; } s = String.Format("{0,-1}. {1,-40}", count.ToString(), "Skip this actor"); Console.WriteLine(s); while (true) { string choice = Console.ReadLine(); try { int choiceNr = int.Parse(choice); if (choiceNr < 0) Console.WriteLine("Please enter a valid number"); else if (choiceNr > performer.Count) Console.WriteLine("Please enter a valid number"); else if (choiceNr == performer.Count) { if (Verbose) Console.WriteLine(Tag + "Skipped actor"); return string.Empty; } else { if (Verbose) Console.WriteLine(Tag + "Selected " + performer[choiceNr].name); return performer[choiceNr].thumbUrl; } } catch (Exception e) { Console.WriteLine("Please enter a valid number"); } } } //One result - return it if (performer.Count == 1) { if (Verbose) Console.WriteLine(Tag + "Single result for actor " + name + " stored in cache"); //store in cache if not elready there if (!performerCache.ContainsKey(performer[0].name)) { performerCache.Add(performer[0].name, performer[0]); } return performer[0].thumbUrl; } } catch (Exception ex) { Console.WriteLine(Tag + "Ooops, encountered exception: " + Utils.GetAllErrorDetails(ex)); } /* Nothing found - store empty result for caching purposes*/ pData = new performerData(); pData.pageUrl = string.Empty; pData.thumbUrl = string.Empty; pData.name = name; if (!performerCache.ContainsKey(name)) performerCache.Add(name, pData); else if (Verbose) Console.WriteLine(Tag + "Key for " + name + " already exists in cache"); return String.Empty; }