/// <summary> /// Implements IDB.CachePlatformdata() Update the local DB cache of platform associated metadata /// </summary> /// <param name="platform">Robin.Platform associated with the DBPlatorm to update.</param> public void CachePlatformData(Platform platform) { GBPlatform gbPlatform = platform.GBPlatform; XDocument xDocument; Reporter.Tic("Attempting to cache " + gbPlatform.Title + "...", out int tic1); using (WebClient webClient = new WebClient()) { string url = GBURL + $"platform/{gbPlatform.ID}/?api_key ={Keys.GiantBomb}"; if (webClient.SafeDownloadStringDB(url, out string downloadText)) { xDocument = XDocument.Parse(downloadText); gbPlatform.Title = xDocument.SafeGetB("results", "name"); gbPlatform.Abbreviation = xDocument.SafeGetB("results", "abbreviation"); if (long.TryParse(xDocument.SafeGetB("results", "company"), out long company)) { gbPlatform.Company = company; } gbPlatform.Deck = xDocument.SafeGetB("results", "deck"); gbPlatform.Price = xDocument.SafeGetB("results", "original_price"); gbPlatform.Date = DateTimeRoutines.SafeGetDate(xDocument.SafeGetB("results", "release_date")); Reporter.Toc(tic1); } else { Reporter.Toc(tic1, "Error communicating with GiantBomb."); } } // end using webclient }
public static DateTime?SafeGetDateTime(string DateString) { DateTimeRoutines.ParsedDateTime dt; if (DateString != null && DateTimeRoutines.TryParseDateTime(DateString, DateTimeRoutines.DateTimeFormat.USA_DATE, out dt)) { return(dt.DateTime); } else { return(null); } }
/// <summary> /// Tries to find date and time within the passed string and return it as ParsedDateTime object. /// </summary> /// <param name="str">string that contains date-time</param> /// <param name="default_format">format to be used preferably in ambivalent instances</param> /// <param name="parsed_date_time">parsed date-time output</param> /// <returns>true if both date and time were found, else false</returns> static public bool TryParseDateTime(this string str, DateTimeFormat default_format, out ParsedDateTime parsed_date_time) { if (DateTimeRoutines.TryParseDateOrTime(str, default_format, out parsed_date_time) && parsed_date_time.IsDateFound && parsed_date_time.IsTimeFound ) { return(true); } parsed_date_time = null; return(false); }
/// <summary> /// Implement IDB.CachePlatfomrReleases(). Go out to gamesdb.com and cache all known releases for the specified platform. Update the list of releases and store metadata for each one. /// </summary> /// <param name="platform">Robin.Platform associated with the GDBPlatform to cache.</param> public void CachePlatformReleases(Platform platform) { Reporter.Tic($"Getting {platform.Title} release list from Games DB...", out int tic1); GDBPlatform gdbPlatform = platform.GDBPlatform; // Update list of GDBReleases for this platform from xml file using (WebClient webclient = new WebClient()) { // API to get xml file containing all gamesdb releases for this platform. string url = $"{baseUrl}GetPlatformGames.php?platform=" + gdbPlatform.ID; // Put existing GDBReleases in a dictionary for lookup performance var existingGDBReleaseDict = R.Data.GDBReleases.ToDictionary(x => x.ID); HashSet <GDBRelease> newGDBReleases = new HashSet <GDBRelease>(); if (webclient.SafeDownloadStringDB(url, out string downloadText)) { XDocument xDocument = XDocument.Parse(downloadText); foreach (XElement element in xDocument.Root.Elements("Game")) { // Don't create this game if the title is null string title = element.Element("GameTitle")?.Value; if (string.IsNullOrEmpty(title)) { continue; } // Check if gbdRelease exists before creating new one. Whether it exists or not, overwrite properties with properties from xml file. long id = long.Parse(element.Element("id")?.Value); if (!existingGDBReleaseDict.TryGetValue(id, out GDBRelease gdbRelease)) { gdbRelease = new GDBRelease { ID = id }; newGDBReleases.Add(gdbRelease); } gdbRelease.Title = title; gdbRelease.Date = DateTimeRoutines.SafeGetDate(element.SafeGetA("ReleaseDate") ?? "01-01-1901"); // If a release has changed platforms, catch it and zero out match if (gdbRelease.GDBPlatform_ID != gdbPlatform.ID) { gdbRelease.GDBPlatform_ID = gdbPlatform.ID; Release release = R.Data.Releases.FirstOrDefault(x => x.ID_GDB == id); if (release != null) { release.ID_GDB = null; } } } } gdbPlatform.GDBReleases.UnionWith(newGDBReleases); Reporter.Toc(tic1); } // Temporarily set wait time to 1 ms while caching tens of thousands of games. Sorry GDB. int waitTimeHolder = DBTimers.GamesDB.WaitTime; DBTimers.GamesDB.WaitTime = 1; int releaseCount = gdbPlatform.GDBReleases.Count; int i = 0; // Cache metadata for each individual game foreach (GDBRelease gdbRelease in gdbPlatform.GDBReleases) { if (releaseCount / 10 != 0 && i++ % (releaseCount / 10) == 0) { Reporter.Report($"{i} / {releaseCount}"); } CacheReleaseData(gdbRelease); } DBTimers.GamesDB.WaitTime = waitTimeHolder; }
/// <summary> /// Cache metadata from gamesdb.com API for a GDBRelease. /// </summary> /// <param name="gdbRelease">GDBRelease whose metadat is to be cached.</param> public void CacheReleaseData(GDBRelease gdbRelease) { // URL of gamesdb API to cache metadata for one release string url = $"{baseUrl}GetGame.php?id=" + gdbRelease.ID; using (WebClient webclient = new WebClient()) { // Pull down the xml file containing game data from gamesdb if (webclient.SafeDownloadStringDB(url, out string downloadText)) { XDocument xDocument = XDocument.Parse(downloadText); gdbRelease.Title = xDocument.SafeGetB("Game", "GameTitle") ?? gdbRelease.Title; gdbRelease.Developer = xDocument.SafeGetB("Game", "Developer") ?? gdbRelease.Developer; gdbRelease.Publisher = xDocument.SafeGetB("Game", "Publisher") ?? gdbRelease.Publisher; gdbRelease.Players = xDocument.SafeGetB("Game", "Players") ?? gdbRelease.Players; gdbRelease.Overview = xDocument.SafeGetB("Game", "Overview") ?? gdbRelease.Overview; gdbRelease.Rating = decimal.Parse(xDocument.SafeGetB("Game", "Rating") ?? "0", CultureInfo.InvariantCulture); gdbRelease.Genre = string.Join(",", xDocument.Root.Descendants("genre").Select(x => x.Value)); gdbRelease.Date = DateTimeRoutines.SafeGetDate(xDocument.SafeGetB("Game", "ReleaseDate")); string coop = xDocument.SafeGetB("Game", "Co-op"); if ((coop != null) && ((coop.ToLower() == "true") || (coop.ToLower() == "yes"))) { gdbRelease.Coop = true; } else { gdbRelease.Coop = false; } string BaseImageUrl = xDocument.SafeGetB("baseImgUrl"); if (BaseImageUrl != null) { url = xDocument.SafeGetBoxArt("front"); if (url != null) { gdbRelease.BoxFrontURL = BaseImageUrl + url; } url = xDocument.SafeGetBoxArt("back"); if (url != null) { gdbRelease.BoxBackURL = BaseImageUrl + url; } url = xDocument.SafeGetB("Game", "Images", "banner"); if (url != null) { gdbRelease.BannerURL = BaseImageUrl + url; } url = xDocument.SafeGetB("Game", "Images", "screenshot", "original"); if (url != null) { gdbRelease.ScreenURL = BaseImageUrl + url; } url = xDocument.SafeGetB("Game", "Images", "clearlogo"); if (url != null) { gdbRelease.LogoURL = BaseImageUrl + url; } } } else { Reporter.Report("Failure getting " + gdbRelease.Title + ", ID " + gdbRelease.ID + " from Games DB."); } } }
/// <summary> /// Implement IDB.CachePlatfomrReleases(). Go out to giantbomb.com and cache all known releases for the specified platform. Update the list of releases and store metadata for each one. /// </summary> /// <param name="platform">Robin.Platform associated with the GBPlatform to cache.</param> public void CachePlatformReleases(Platform platform, bool reset = false) { using (WebClient webClient = new WebClient()) { GBPlatform gbPlatform = platform.GBPlatform; string startDate = reset ? @"1900-01-01 01:01:01" : gbPlatform.CacheDate.ToString(DATEFORMAT); string endDate = DateTime.Now.ToString(DATEFORMAT); int N_results; bool haveResults; XDocument xdoc; Reporter.Report($"Checking GiantBomb for {gbPlatform.Title} releases..."); string url = $"http://www.giantbomb.com/api/releases/?api_key={Keys.GiantBomb}&filter=date_last_updated:{startDate}|{endDate},platform:{gbPlatform.ID}&field_list=id&sort=id:asc"; if (webClient.SafeDownloadStringDB(url, out string downloadText)) { xdoc = XDocument.Parse(downloadText); haveResults = int.TryParse(xdoc.SafeGetB("number_of_total_results"), out N_results); } else { Reporter.Warn("Error communicating with GiantBomb."); return; } // If there are results, go through them if (haveResults && N_results != 0) { Dictionary <long, GBRelease> existingGbReleaseDict = R.Data.GBReleases.ToDictionary(x => x.ID); ILookup <long?, Release> releaseLookupByGB_ID = R.Data.Releases.Where(x => x.ID_GB != null).ToLookup(x => x.ID_GB); HashSet <GBRelease> newGbReleases = new HashSet <GBRelease>(); int N_pages = N_results / 100; Reporter.Report($"Found {N_results} {gbPlatform.Title} releases in GiantBomb"); // Just do the first page again to save code then go through the rest of the results Reporter.Report("Loading sheet "); for (int i = 0; i <= N_pages; i++) { Reporter.ReportInline(" " + i); url = $"http://www.giantbomb.com/api/releases/?api_key={Keys.GiantBomb}&filter=date_last_updated:{startDate}|{endDate}platform:{gbPlatform.ID}&offset={i * 100}&field_list=id,deck,game,image,region,name,maximum_players,release_date&sort=id:asc"; // Put results into the GB Cache database if (webClient.SafeDownloadStringDB(url, out downloadText)) { xdoc = XDocument.Parse(downloadText); foreach (XElement element in xdoc.Root.Element("results").Elements("release")) { // If the ID XML value was found if (int.TryParse(element.SafeGetA("id"), out int id)) { // Don't create this game if the title is null string title = element.SafeGetA("name"); if (string.IsNullOrEmpty(title)) { continue; } // Check if GBRelease is in database prior to this update, else add it if (!existingGbReleaseDict.TryGetValue(id, out GBRelease gbRelease)) { gbRelease = new GBRelease { ID = id, GBPlatform = platform.GBPlatform }; newGbReleases.Add(gbRelease); } // If a release has changed platforms, catch it and zero out match if (gbRelease.GBPlatform_ID != gbPlatform.ID) { gbRelease.GBPlatform_ID = gbPlatform.ID; if (releaseLookupByGB_ID[gbRelease.ID].Any()) { foreach (Release release in releaseLookupByGB_ID[gbRelease.ID]) { release.ID_GB = null; } } } gbRelease.Title = title; gbRelease.Overview = element.SafeGetA("deck"); if (int.TryParse(element.SafeGetA("game", "id"), out id)) { gbRelease.GBGame_ID = id; } if (int.TryParse(element.SafeGetA("maximum_players"), out id)) { gbRelease.Players = id.ToString(); } gbRelease.GBPlatform_ID = gbPlatform.ID; if (int.TryParse(element.SafeGetA("region", "id"), out id)) { gbRelease.Region = R.Data.Regions.FirstOrDefault(x => x.ID_GB == id) ?? R.Data.Regions.FirstOrDefault(x => x.Title.Contains("Unk")); } gbRelease.Date = DateTimeRoutines.SafeGetDate(element.SafeGetA("release_date")); gbRelease.BoxURL = element.SafeGetA("image", "medium_url"); gbRelease.ScreenURL = element.SafeGetA("image", "screen_url"); } } } else { Reporter.Warn("Failure connecting to GiantBomb"); return; } } R.Data.GBReleases.AddRange(newGbReleases); Reporter.ReportInline("Finished."); } else { Reporter.Report("No releases returned by GiantBomb"); return; } } // end using webclient }
public void CachePlatformGames(Platform platform, bool reset = false) { GBPlatform gbPlatform = platform.GBPlatform; string startDate = reset ? @"1900-01-01 01:01:01" : gbPlatform.CacheDate.ToString(DATEFORMAT); string endDate = DateTime.Now.ToString(DATEFORMAT); using (WebClient webClient = new WebClient()) { int N_results; bool haveResults; XDocument xdoc; Reporter.Report("Checking GiantBomb for " + platform.Title + " games"); var url = $"http://www.giantbomb.com/api/games/?api_key={Keys.GiantBomb}&filter=date_last_updated:{startDate}|{endDate},platforms:{gbPlatform.ID}&field_list=id&sort=id:asc"; if (webClient.SafeDownloadStringDB(url, out var downloadText)) { xdoc = XDocument.Parse(downloadText); haveResults = int.TryParse(xdoc.SafeGetB("number_of_total_results"), out N_results); } else { Reporter.Report("Error communicating with GiantBomb."); return; } // If there are results, go through them if (haveResults && N_results != 0) { int N_pages = N_results / 100; Reporter.Report("Found " + N_results + " games in GiantBomb"); Reporter.Report("Loading sheet "); // Just do the first page again to save code then go through the rest of the results for (int i = 0; i <= N_pages; i++) { Reporter.ReportInline(i + " "); url = $"http://www.giantbomb.com/api/games/?api_key={Keys.GiantBomb}&filter=date_last_updated:{startDate}|{endDate},platforms:{gbPlatform.ID}&offset={i * 100}&field_list=id,deck,developers,publishers,genres,image,name,release_date&sort=id:asc"; // Put results into the GB Cache database if (webClient.SafeDownloadStringDB(url, out downloadText)) { xdoc = XDocument.Parse(downloadText); foreach (XElement element in xdoc.Root.Element("results").Elements("game")) { // If the ID XML value was found if (int.TryParse(element.SafeGetA("id"), out var intCatcher)) { GBGame gbGame = R.Data.GBGames.FirstOrDefault(x => x.ID == intCatcher); if (gbGame == null) { gbGame = new GBGame { ID = intCatcher }; gbPlatform.GBGames.Add(gbGame); } gbGame.Title = element.SafeGetA("name"); gbGame.Overview = element.SafeGetA("deck"); gbGame.GBPlatform_ID = gbPlatform.ID; gbGame.Date = DateTimeRoutines.SafeGetDate(element.SafeGetA("release_date")); gbGame.BoxFrontURL = element.SafeGetA("image", "medium_url"); gbGame.ScreenURL = element.SafeGetA("image", "screen_url"); } } } else { Reporter.Warn("Failure connecting to GiantBomb"); return; } } Reporter.Report("Finished."); } else { Reporter.Report("No results returned by GiantBomb"); return; } } // end using webclient }
public void CachePlatformGames(Platform platform) { if (launchboxFile == null) { GetLaunchBoxFile(); } // Create a dictionary of existing Games to speed lookups Dictionary <long, LBGame> existingLBGameDict = R.Data.LBGames.ToDictionary(x => x.ID); // Create a Hashset of LBGames to store any new LBGames that we discover HashSet <LBGame> newLBGames = new HashSet <LBGame>(); List <XElement> platformGameElements = gameElementLookupByPlatform[platform.LBPlatform.Title].ToList(); int gameCount = platformGameElements.Count; Reporter.Report($"Found {gameCount} {platform.LBPlatform.Title} games in LaunchBox zip file."); int j = 0; foreach (XElement gameElement in platformGameElements) { // Reporting only if ((gameCount / 10) != 0 && ++j % (gameCount / 10) == 0) { Reporter.Report(" Working " + j + " / " + gameCount + " " + platform.LBPlatform.Title + " games in the LaunchBox database."); } string title = gameElement.Element("Name")?.Value; // Don't create this game if the title or database ID is null if (string.IsNullOrEmpty(title) || !long.TryParse(gameElement.SafeGetA("DatabaseID"), out long id)) { continue; } // Check if the game alredy exists in the local cache before trying to add it if (!existingLBGameDict.TryGetValue(id, out LBGame lbGame)) { lbGame = new LBGame { ID = id }; newLBGames.Add(lbGame); Debug.WriteLine("New game: " + lbGame.Title); } // If a game has changed platforms, catch it and zero out match if (lbGame.LBPlatform_ID != platform.LBPlatform.ID) { lbGame.LBPlatform = platform.LBPlatform; Release release = R.Data.Releases.FirstOrDefault(x => x.ID_LB == lbGame.ID); if (release != null) { release.ID_LB = null; } } // Set or overwrite game properties lbGame.Title = title; lbGame.Date = DateTimeRoutines.SafeGetDateTime(gameElement.SafeGetA("ReleaseDate") ?? gameElement.SafeGetA("ReleaseYear") + @"-01-01 00:00:00"); lbGame.Overview = gameElement.Element("Overview")?.Value ?? lbGame.Overview; lbGame.Genres = gameElement.Element("Genres")?.Value ?? lbGame.Genres; lbGame.Developer = gameElement.Element("Developer")?.Value ?? lbGame.Developer; lbGame.Publisher = gameElement.Element("Publisher")?.Value ?? lbGame.Publisher; lbGame.VideoURL = gameElement.Element("VideoURL")?.Value ?? lbGame.VideoURL; lbGame.WikiURL = gameElement.Element("WikipediaURL")?.Value ?? lbGame.WikiURL; lbGame.Players = gameElement.Element("MaxPlayers")?.Value ?? lbGame.Players; } R.Data.LBGames.AddRange(newLBGames); }
/// <summary> /// Cache data for a selected LBPlatforms in the local database from an xml file downloaded from Launchbox. /// </summary> /// <param name="platform">Robin.Platform associated with the LBPlatform to cache.</param> public void CachePlatformData(Platform platform) { Reporter.Tic($"Caching data for {platform.Title}...", out int tic1); if (launchboxFile == null) { GetLaunchBoxFile(); } // Create a dictionary of existing LBPlatforms to speed lookups Dictionary <string, LBPlatform> platformDictionary = R.Data.LBPlatforms.ToDictionary(x => x.Title); // Create a Hashset of LBPlatforms to store any new LBPlatforms that we discover HashSet <LBPlatform> newLBPlatforms = new HashSet <LBPlatform>(); foreach (XElement platformElement in platformElements) { string tempTitle = platformElement.Element("Name").Value; // If a bad titl is found or the title is gamewave, just bail if (string.IsNullOrEmpty(tempTitle) || Regex.IsMatch(tempTitle, @"Game*.Wave", RegexOptions.IgnoreCase)) { continue; } #if DEBUG Stopwatch watch1 = Stopwatch.StartNew(); #endif // Check whether the LBPlatform exists before trying to add it. LBPlatforms have no ID, so check by title. If the title isn't found, this might be because the LBPlatform is new or because the title has been changed. The merge window lets the user decide. if (!platformDictionary.TryGetValue(tempTitle, out LBPlatform lbPlatform)) { lbPlatform = new LBPlatform(); Application.Current.Dispatcher.Invoke(() => { MergeWindow mergeWindow = new MergeWindow(tempTitle); switch (mergeWindow.ShowDialog()) { case true: // Merge the new platform with existing lbPlatform = mergeWindow.SelectedLBPlatform; break; case false: // Add the new platform newLBPlatforms.Add(lbPlatform); break; default: throw new ArgumentOutOfRangeException(); } }); } #if DEBUG Debug.WriteLine("PB: " + watch1.ElapsedMilliseconds); watch1.Restart(); #endif // Whether the LBPlatorm is new or old, overwrite everything with the newest data lbPlatform.Title = tempTitle; lbPlatform.Date = DateTimeRoutines.SafeGetDate(platformElement.SafeGetA("Date")); lbPlatform.Developer = platformElement.Element("Developer")?.Value ?? lbPlatform.Developer; lbPlatform.Manufacturer = platformElement.Element("Manufacturer")?.Value ?? lbPlatform.Manufacturer; lbPlatform.Cpu = platformElement.Element("Cpu")?.Value ?? lbPlatform.Cpu; lbPlatform.Memory = platformElement.Element("Memory")?.Value ?? lbPlatform.Memory; lbPlatform.Graphics = platformElement.Element("Graphics")?.Value ?? lbPlatform.Graphics; lbPlatform.Sound = platformElement.Element("Sound")?.Value ?? lbPlatform.Sound; lbPlatform.Display = platformElement.Element("Display")?.Value ?? lbPlatform.Display; lbPlatform.Media = platformElement.Element("Media")?.Value ?? lbPlatform.Media; lbPlatform.Display = platformElement.Element("Display")?.Value ?? lbPlatform.Display; lbPlatform.Controllers = platformElement.Element("MaxControllers")?.Value ?? lbPlatform.Controllers; lbPlatform.Category = platformElement.Element("Category")?.Value ?? lbPlatform.Category; #if DEBUG Debug.WriteLine("PC: " + watch1.ElapsedMilliseconds); watch1.Restart(); #endif } R.Data.LBPlatforms.AddRange(newLBPlatforms); Reporter.Toc(tic1, "all platforms cached."); }