public static bool GetGameData(string gameQuery, string platformName, string imageFolderPath, bool overwriteFile, JsonWriter writer) { const int MAX_SEARCH = 10; GiantBombRestClient giantBomb = new GiantBombRestClient(apiToken); try { IEnumerable<Game> games = giantBomb.SearchForGames(gameQuery); int maxCount = (games.Count() > MAX_SEARCH) ? MAX_SEARCH : games.Count(); float minGameDistance = Int32.MaxValue; int minGameIndex = -1; for (int i = 0; i < maxCount; i++) { float distance = SiftDistance(gameQuery, games.ElementAt(i).Name, games.ElementAt(i).Name.Length); if (distance < minGameDistance) { minGameDistance = distance; minGameIndex = i; } } float distanceThreshold = gameQuery.Length; if (minGameDistance <= distanceThreshold) { IEnumerable<Release> releases = giantBomb.GetReleasesForGame(games.ElementAt(minGameIndex).Id); string itemImageURL = ""; string itemDeck = ""; string itemDescription = ""; string itemReleaseDate = ""; bool retrievedImage = false; if (releases.Count() > 0) { float minReleasePlatformDistance = Int32.MaxValue; float minReleaseGameDistance = Int32.MaxValue; int minReleaseIndex = -1; for (int releaseNum = 0; releaseNum < (releases.Count() - 1); releaseNum++) { float platformDistance = SiftDistance(platformName.ToLower(), releases.ElementAt(releaseNum).Platform.Name.ToLower(), releases.ElementAt(releaseNum).Platform.Name.Length); float gameDistance = SiftDistance(gameQuery.ToLower(), releases.ElementAt(releaseNum).Name.ToLower(), releases.ElementAt(releaseNum).Name.Length); if (releases.ElementAt(releaseNum).Image != null) { if (platformDistance <= minReleasePlatformDistance) // At or below minimum platform distance - note: cares about game over platform { if (gameDistance <= minReleaseGameDistance) // At or below game distance { if ((platformDistance == minReleasePlatformDistance) && (gameDistance == minReleaseGameDistance)) { } else //set unless both distances are the same { minReleasePlatformDistance = platformDistance; minReleaseGameDistance = gameDistance; minReleaseIndex = releaseNum; } } } } } if (minReleaseIndex != -1) // First try to get image URL from Game Release object { if (releases.ElementAt(minReleaseIndex).Image.MediumUrl != null) { itemImageURL = releases.ElementAt(minReleaseIndex).Image.MediumUrl; } if (releases.ElementAt(minReleaseIndex).Deck != null) { itemDeck = releases.ElementAt(minReleaseIndex).Deck; } if (releases.ElementAt(minReleaseIndex).Description != null) { itemDescription = releases.ElementAt(minReleaseIndex).Description; } if (releases.ElementAt(minReleaseIndex).ReleaseDate != null) { itemReleaseDate = releases.ElementAt(minReleaseIndex).ReleaseDate.ToString(); } retrievedImage = (itemImageURL != ""); } } //Get game information from Game Release object if not set if (games.ElementAt(minGameIndex).Image != null) { if (games.ElementAt(minGameIndex).Image.MediumUrl != null) { itemImageURL = (itemImageURL != "") ? itemImageURL : games.ElementAt(minGameIndex).Image.MediumUrl; } } if (games.ElementAt(minGameIndex).Deck != null) { itemDeck = (itemDeck != "") ? itemDeck : games.ElementAt(minGameIndex).Deck; } if (games.ElementAt(minGameIndex).Description != null) { itemDescription = (itemDescription != "") ? itemDescription : games.ElementAt(minGameIndex).Description; } if (games.ElementAt(minGameIndex).OriginalReleaseDate != null) { itemReleaseDate = (itemReleaseDate != "") ? itemReleaseDate : games.ElementAt(minGameIndex).OriginalReleaseDate.ToString(); } string extension = itemImageURL.Split('.').Last(); if (extension.Length > 4) { extension = "jpg"; } string itemImageLocation = imageFolderPath + gameQuery + "." + extension; if (itemImageURL != "")//download image if needed { if ((!File.Exists(itemImageLocation)) || (File.Exists(itemImageLocation) && overwriteFile)) { WebClient webClient = new WebClient(); try { webClient.DownloadFile(itemImageURL, itemImageLocation); } catch (WebException) { itemImageLocation = ""; } } } else { itemImageLocation = ""; } writer.WriteStartObject(); writer.WritePropertyName("Name"); writer.WriteValue(gameQuery); writer.WritePropertyName("Description"); writer.WriteValue(itemDescription); writer.WritePropertyName("ShortDescription"); writer.WriteValue(itemDeck); writer.WritePropertyName("ReleaseDate"); writer.WriteValue(itemReleaseDate); writer.WritePropertyName("ImageLocation"); writer.WriteValue(itemImageLocation); writer.WriteEndObject(); return true; } } catch (Exception) { } return false; }
public void GetItemImage(string gameQuery, string saveDir, bool overwriteFile) { const int MAX_SEARCH = 5; GiantBombRestClient giantBomb = new GiantBombRestClient(apiToken); IEnumerable<Game> games = giantBomb.SearchForGames(gameQuery); int maxCount = (games.Count() > MAX_SEARCH) ? MAX_SEARCH : games.Count(); float minDistance = Int32.MaxValue; int minIndex = -1; for (int i = 0; i < maxCount; i++) { float distance = SiftDistance(gameQuery, games.ElementAt(i).Name, games.ElementAt(i).Name.Length); if (distance < minDistance) { minDistance = distance; minIndex = i; } } float distanceThreshold = 30; if (minDistance <= distanceThreshold) { string itemImageURL = games.ElementAt(minIndex).Image.MediumUrl; string extension = itemImageURL.Split('.').Last(); string saveToFilePath = saveDir + gameQuery + "." + extension; if ((!File.Exists(saveToFilePath)) || (File.Exists(saveToFilePath) && overwriteFile)) { WebClient webClient = new WebClient(); webClient.DownloadFile(itemImageURL, saveToFilePath); } int a = 0; int b = a + a; } }
public static ObservableCollection<List<GameData>> GetGameDataSetLists(string gameQuery) { const int MAX_SEARCH = 10; GiantBombRestClient giantBomb = new GiantBombRestClient(apiToken); try { IEnumerable<Game> games = giantBomb.SearchForGames(gameQuery); int maxCountGames = (games.Count() > MAX_SEARCH) ? MAX_SEARCH : games.Count(); ObservableCollection<List<GameData>> currGames = new ObservableCollection<List<GameData>>(); for (int i = 0; i < maxCountGames; i++) { List<GameData> currGameList = new List<GameData>(); GameData currGame = new GameData(); currGame = new GameData(); currGame.GameName = games.ElementAt(i).Name; currGame.GameDescription = games.ElementAt(i).Deck; currGame.ReleaseDate = games.ElementAt(i).OriginalReleaseDate.ToString(); if (games.ElementAt(i).Image != null) { if (games.ElementAt(i).Image.SuperUrl != null) { currGame.ImageURL = games.ElementAt(i).Image.SuperUrl; } else { if (games.ElementAt(i).Image.MediumUrl != null) { currGame.ImageURL = games.ElementAt(i).Image.MediumUrl; } else { if (games.ElementAt(i).Image.SmallUrl != null) { currGame.ImageURL = games.ElementAt(i).Image.SmallUrl; } else { currGame.ImageURL = null; } } } } else { currGame.ImageURL = null; } if (currGame.ImageURL != null) { if (ItemImageExists(currGame.ImageURL)) { currGameList.Add(currGame); } } IEnumerable<Release> releases = giantBomb.GetReleasesForGame(games.ElementAt(i)); int maxCountReleases = (releases.Count() > MAX_SEARCH) ? MAX_SEARCH : releases.Count(); for (int j = 0; j < maxCountReleases; j++) { GameData currRelease = new GameData(); currRelease.GameName = releases.ElementAt(j).Name; currRelease.ReleaseDate = releases.ElementAt(j).ReleaseDate.ToString(); currRelease.PlatformName = releases.ElementAt(j).Platform.Name; if (releases.ElementAt(j).Deck != null) { currRelease.GameDescription = releases.ElementAt(j).Deck; } else { currRelease.GameDescription = games.ElementAt(i).Deck; } if (releases.ElementAt(j).Image != null) { if (releases.ElementAt(j).Image.SuperUrl != null) { currRelease.ImageURL = releases.ElementAt(j).Image.SuperUrl; } else { if (releases.ElementAt(j).Image.MediumUrl != null) { currRelease.ImageURL = releases.ElementAt(j).Image.MediumUrl; } else { if (releases.ElementAt(j).Image.SmallUrl != null) { currRelease.ImageURL = releases.ElementAt(j).Image.SmallUrl; } else { currRelease.ImageURL = null; } } } } else { currRelease.ImageURL = null; } if (currRelease.ImageURL != null) { if (ItemImageExists(currGame.ImageURL)) { currGameList.Add(currRelease); } } } if (currGameList.Count() > 0) { currGames.Add(currGameList); } } return currGames; } catch (Exception ) { return null; } }