FreakOut.classes.Scraper.TheGamesDB.GetGameByID C# (CSharp) Method

GetGameByID() public method

public GetGameByID ( int TGDB_id ) : Game
TGDB_id int
return Game
        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;
        }

Usage Example

        public Nintendo64_PostProcess(string DirectoryToProcess, char SpaceChar, bool MetaEnabled, bool RenamingEnabled, bool MovingEnabled, DatabaseConnector DBConnection)
        {
            MetaEnabled = true;
            RenamingEnabled = true;
            MovingEnabled = true;

            string[] FilesToProcess = System.IO.Directory.GetFiles(@DirectoryToProcess);
            Progress ProgressForm = new Progress();
            ProgressForm.Show();
            ProgressForm.Step = ProgressForm.CalculateStep(FilesToProcess.Length);

            foreach (string SingleFile in FilesToProcess)
            {
                ProgressForm.UpdateProcessingName = SingleFile;

                //Get the Directory including the file
                string FileDirectory = System.IO.Directory.GetParent(SingleFile).FullName;

                //Get the filename without the path
                string Filename = SingleFile.Replace(FileDirectory + "\\", "");
                string Searchname = Filename;
                //Strip and Save the extension
                Regex FileExtensionRegex = new Regex("\\.(...)$");
                string Extension = FileExtensionRegex.Match(Searchname).Value;
                Searchname = FileExtensionRegex.Replace(Searchname, "");

                //Replace "SpaceChar" with actual spaces for our search
                Searchname = Searchname.Replace(SpaceChar, " ".ToCharArray()[0]);
                LogFacility.WriteToFile(4, "Title to search: " + Searchname, LogSource);

                //Initiate scraper
                Scraper.TheGamesDB InfoScraper = new Scraper.TheGamesDB(LogFacility);
                List<Game> Results = InfoScraper.Search(Searchname, "Nintendo 64");

                //We will only take the first result, let's hope that it's the best one...
                if (Results != null)
                    DBConnection.InsertGame(InfoScraper.GetGameByID(Results[0].scraper_gdb_id));
                else
                    LogFacility.WriteToFile(1, "Could not find an item for " + Searchname, LogSource);

                if (Results != null)
                {
                    Filename = Results[0].Name.Replace(" ".ToCharArray()[0], SpaceChar);
                    Filename += Extension;

                    LogFacility.WriteToFile(4, "Resulted Filename: " + FileDirectory + "\\" + Filename, LogSource);

                    if (RenamingEnabled && !MovingEnabled)
                        System.IO.File.Move(SingleFile, FileDirectory + "\\" + Filename);

                    if (MovingEnabled)
                        System.IO.File.Move(SingleFile, FileDirectory + "\\" + Filename);
                }

                ProgressForm.UpdateProgressBar();
            }

            ProgressForm.Close();
        }
All Usage Examples Of FreakOut.classes.Scraper.TheGamesDB::GetGameByID