public Game GetGameByID(int TGDB_id)
{
//Get a single game by it's ID in TheGameDB
LogInstance.WriteToFile(1, "Looking up the game with the ID " + TGDB_id + " in TheGamesDB", LogSource);
Game Result = new Game();
Result.scraper_gdb_id = TGDB_id;
Result.InfoXML = new XmlDocument();
string[] ItemCache = System.IO.Directory.GetFiles("cache\\thegamedb", TGDB_id.ToString() + "*.xml");
foreach (string cachedItem in ItemCache)
{
if (File.GetCreationTime(cachedItem) <= DateTime.Now.AddHours(-24))
{
File.Delete(cachedItem);
}
}
ItemCache = System.IO.Directory.GetFiles("cache\\thegamedb", TGDB_id.ToString() + "*.xml");
if (ItemCache.Length < 1)
{
WebRequest ResultRequest;
WebResponse ResultResponse;
try
{
ResultRequest = WebRequest.Create("http://thegamesdb.net/api/GetGame.php?id=" + TGDB_id);
ResultResponse = ResultRequest.GetResponse();
}
catch
{
LogInstance.WriteToFile(2, "Unable to fetch file from http://thegamesdb.net/api/GetGame.php?id=" + TGDB_id, LogSource);
return null;
}
System.IO.Stream ResultStream = ResultResponse.GetResponseStream();
Result.InfoXML.Load(ResultStream);
Result.Name = Result.InfoXML.SelectSingleNode("Data/Game/GameTitle").InnerText.Replace("'", "");
string XMLString = Result.InfoXML.SelectSingleNode("Data").InnerXml;
XMLString = XMLString.Replace("<baseImgUrl>http://thegamesdb.net/banners/</baseImgUrl>", "");
XMLString = XMLString.Replace("<id>", "<gamesdbid>");
XMLString = XMLString.Replace("</id>", "</gamesdbid>");
XMLString = XMLString.Replace("fanart/original/", "http://thegamesdb.net/banners/fanart/original/");
XMLString = XMLString.Replace("fanart/thumb/", "http://thegamesdb.net/banners/fanart/thumb/");
XMLString = XMLString.Replace("boxart/original/", "http://thegamesdb.net/banners/boxart/original/");
XMLString = XMLString.Replace("graphical/", "http://thegamesdb.net/banners/graphical");
XMLString = XMLString.Replace("screenshots/", "http://thegamesdb.net/banners/screenshots/");
XMLString = XMLString.Replace("'", "");
Result.InfoXML.LoadXml(XMLString);
string CacheFileName = Result.Name.Replace(":", "-");
XmlWriter CacheXml = XmlWriter.Create("cache\\thegamedb\\" + TGDB_id + "_" + CacheFileName + ".xml");
//caching the result
Result.InfoXML.WriteContentTo(CacheXml);
CacheXml.Flush();
CacheXml.Close();
//try to fetch the fanart and cache it
if (XMLString.Contains("http://thegamesdb.net/banners/fanart/original/"))
{
string FanartURI = Result.InfoXML.SelectSingleNode("Game/Images/fanart/original").InnerText;
try
{
WebClient FanartDownloader = new WebClient();
FanartDownloader.DownloadFile(FanartURI, @Directory.GetCurrentDirectory() + "\\cache\\thegamedb\\" + TGDB_id + "_" + Result.Name + ".jpg");
}
catch
{
LogInstance.WriteToFile(2, "Failed to download fanart for " + Result.Name + ".", LogSource);
}
}
//try to fetch the boxart and cache it
if (XMLString.Contains("http://thegamesdb.net/banners/boxart/original/front/"))
{
string BoxartURI = Result.InfoXML.SelectSingleNode("Game/Images/boxart[@side='front']").InnerText;
try
{
WebClient BoxartDownloader = new WebClient();
BoxartDownloader.DownloadFile(BoxartURI, @Directory.GetCurrentDirectory() + "\\cache\\thegamedb\\" + TGDB_id + "_" + Result.Name + "_Poster.jpg");
}
catch
{
LogInstance.WriteToFile(2, "Failed to download boxart for " + Result.Name + ".", LogSource);
}
}
ResultStream.Close();
}
else
{
LogInstance.WriteToFile(1, "Using cached xml " + ItemCache[0], LogSource);
XmlReader ReadCachedXml = XmlReader.Create(ItemCache[0]);
Result.InfoXML.Load(ReadCachedXml);
Result.Name = Result.InfoXML.SelectSingleNode("Game/GameTitle").InnerText;
}
System.Text.RegularExpressions.Regex DateFormatExact = new System.Text.RegularExpressions.Regex("^(0[1-9]|1[0-2])/(0[1-9]|1[0-9]|2[0-9]|3[01])/([1-9][0-9][0-9][0-9])$");
System.Text.RegularExpressions.Regex DateFormatYear = new System.Text.RegularExpressions.Regex("^([1-9][1-9][1-9][1-9])$");
string tempstring;
if (Result.InfoXML.InnerXml.Contains("<Platform>"))
Result.Platform = Result.InfoXML.SelectSingleNode("Game/Platform").InnerText;
if (Result.InfoXML.InnerXml.Contains("<ReleaseDate>"))
{
if (DateFormatExact.IsMatch(Result.InfoXML.SelectSingleNode("Game/ReleaseDate").InnerText))
{
tempstring = Result.InfoXML.SelectSingleNode("Game/ReleaseDate").InnerText;
tempstring = tempstring.Split(Char.Parse("/"))[1] + "/" + tempstring.Split(Char.Parse("/"))[0] + "/" + tempstring.Split(Char.Parse("/"))[2];
}
else
{
tempstring = Result.InfoXML.SelectSingleNode("Game/ReleaseDate").InnerText;
}
Result.ReleaseDate = DateTime.Parse(tempstring);
}
else
{
Result.ReleaseDate = DateTime.Parse("01/01/1960");
}
return Result;
}