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;
}