/// <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(); 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 }
/// <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 static void CachePlatformReleases(Platform platform, bool reset = false) { using WebClient webClient = new(); 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(); 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 static 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(); 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 }
/// <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(); 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(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."); }
/// <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()) { // 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(); 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(); // 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 = double.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."); } }