public Search ( string GameName, string Platform ) : List |
||
GameName | string | |
Platform | string | |
return | List |
public List<Game> Search(string GameName, string Platform)
{
//Search for a game by it's name in theGameDB
//return a maximum of 50 results
LogInstance.WriteToFile(1, "Searching for " + GameName + " in TheGamesDB.", LogSource);
List<Game> ResultList = new List<Game>();
XmlDocument Result = new XmlDocument();
WebRequest ResultRequest;
WebResponse ResultResponse;
//Try to reach TheGameDB - return null if this fails (internet connection is not established, GameDB is down, etc.)
try
{
ResultRequest = WebRequest.Create("http://thegamesdb.net/api/GetGamesList.php?name=" + GameName + "&platform=" + Platform);
ResultResponse = ResultRequest.GetResponse();
}
catch
{
return null;
}
System.IO.Stream ResultStream = ResultResponse.GetResponseStream();
Result.Load(ResultStream);
if (Result.InnerXml == null)
return null;
//Try to match the date and make it usable for our purposes (tempstring below)
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;
Game tempresult;
foreach (XmlNode ResultNode in Result.SelectNodes("Data/Game"))
{
tempresult = new Game();
tempresult.Name = ResultNode.SelectSingleNode("GameTitle").InnerText;
tempresult.scraper_gdb_id = int.Parse(ResultNode.SelectSingleNode("id").InnerText);
if (ResultNode.InnerXml.Contains("<Platform>"))
{
tempresult.Platform = ResultNode.SelectSingleNode("Platform").InnerText;
}
if (ResultNode.InnerXml.Contains("<ReleaseDate>"))
{
if (DateFormatExact.IsMatch(ResultNode.SelectSingleNode("ReleaseDate").InnerText))
{
tempstring = ResultNode.SelectSingleNode("ReleaseDate").InnerText;
tempstring = tempstring.Split(Char.Parse("/"))[1] + "/" + tempstring.Split(Char.Parse("/"))[0] + "/" + tempstring.Split(Char.Parse("/"))[2];
}
else
{
if (DateFormatYear.IsMatch(ResultNode.SelectSingleNode("ReleaseDate").InnerText))
tempstring = "01/01/" + ResultNode.SelectSingleNode("ReleaseDate").InnerText;
else
tempstring = "01/01/1960";
}
tempresult.ReleaseDate = DateTime.Parse(tempstring);
}
ResultList.Add(tempresult);
}
//Sort the results with a simple sort-logic
for (int i = 0; i < ResultList.Count; i++ )
{
if (ResultList[i].Name.EndsWith(GameName) || ResultList[i].Name.EndsWith(GameName.ToLower()))
ResultList[i].scraper_gdb_score = 6;
if (ResultList[i].Name.Contains(GameName) || ResultList[i].Name.Contains(GameName.ToLower()) && !ResultList[i].Name.EndsWith(GameName) && !ResultList[i].Name.EndsWith(GameName.ToLower()))
ResultList[i].scraper_gdb_score = 5;
if (ResultList[i].Name.StartsWith(GameName) || ResultList[i].Name.StartsWith(GameName.ToLower()))
ResultList[i].scraper_gdb_score = 4;
if (ResultList[i].Name.EndsWith(" " + GameName) || ResultList[i].Name.EndsWith(" " + GameName.ToLower()))
ResultList[i].scraper_gdb_score = 3;
if (ResultList[i].Name.Contains(" " + GameName + " ") || ResultList[i].Name.Contains(" " + GameName.ToLower() + " ") && !ResultList[i].Name.EndsWith(" " + GameName) && !ResultList[i].Name.EndsWith(" " + GameName.ToLower()))
ResultList[i].scraper_gdb_score = 2;
if (ResultList[i].Name.StartsWith(GameName + " ") || ResultList[i].Name.StartsWith(GameName.ToLower() + " "))
ResultList[i].scraper_gdb_score = 1;
if (ResultList[i].Name == GameName)
ResultList[i].scraper_gdb_score = 0;
}
Game temp;
//Bubblesort the rating we gave in the step before
for (int y = ResultList.Count - 1; y >= 0; y--)
{
for (int j = 0; j <= y; j++)
{
if ((j + 1) <= y)
{
if (ResultList[j] != null && ResultList[j].scraper_gdb_score > ResultList[j + 1].scraper_gdb_score)
{
temp = ResultList[j];
ResultList[j] = ResultList[j + 1];
ResultList[j + 1] = temp;
}
}
}
}
//In case we have two results with same score, compare the dates
//again: Simple bubblesort
for (int y = ResultList.Count - 1; y >= 0; y--)
{
for (int j = 0; j <= y; j++)
{
if ((j + 1) <= y)
{
if (ResultList[j] != null && (ResultList[j].scraper_gdb_score == ResultList[j + 1].scraper_gdb_score) && (ResultList[j].ReleaseDate.CompareTo(ResultList[j + 1].ReleaseDate) < 0))
{
temp = ResultList[j];
ResultList[j] = ResultList[j + 1];
ResultList[j + 1] = temp;
}
}
}
}
//If the user gave us a platform to work with, we'll try to sort out the correct platforms only
if (Platform != "")
{
List<Game> Storage = ResultList;
int count = 0;
for (int i = 0; i < ResultList.Count; i++)
{
if (ResultList[i].Platform != Platform)
ResultList[i] = null;
else
count++;
}
if (count == 0)
return Storage;
else
{
Storage = new List<Game>();
count = 0;
for (int i = 0; i < ResultList.Count; i++)
{
if (ResultList[i] != null)
{
Storage.Add(ResultList[i]);
count++;
}
}
return Storage;
}
}
else
return ResultList;
}
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(); }