2013-09-16 19:53:24 +00:00
|
|
|
#include "GamesDBScraper.h"
|
2013-09-17 21:50:49 +00:00
|
|
|
#include "../components/AsyncReqComponent.h"
|
2013-09-19 23:41:14 +00:00
|
|
|
#include "../Log.h"
|
2013-09-20 19:55:44 +00:00
|
|
|
#include "../pugiXML/pugixml.hpp"
|
2013-09-16 19:53:24 +00:00
|
|
|
|
|
|
|
std::vector<MetaDataList> GamesDBScraper::getResults(ScraperSearchParams params)
|
|
|
|
{
|
2013-09-20 19:55:44 +00:00
|
|
|
std::shared_ptr<HttpReq> req = makeHttpReq(params);
|
2013-09-17 21:50:49 +00:00
|
|
|
while(req->status() == HttpReq::REQ_IN_PROGRESS);
|
2013-09-16 19:53:24 +00:00
|
|
|
|
2013-09-17 21:50:49 +00:00
|
|
|
return parseReq(params, req);
|
|
|
|
}
|
|
|
|
|
2013-09-20 19:55:44 +00:00
|
|
|
std::shared_ptr<HttpReq> GamesDBScraper::makeHttpReq(ScraperSearchParams params)
|
2013-09-17 21:50:49 +00:00
|
|
|
{
|
2013-09-20 19:55:44 +00:00
|
|
|
std::string path = "/api/GetGame.php?";
|
|
|
|
|
|
|
|
std::string cleanName = params.nameOverride;
|
|
|
|
if(cleanName.empty())
|
|
|
|
cleanName = params.game->getBaseName();
|
|
|
|
|
|
|
|
path += "name=" + cleanName;
|
|
|
|
//platform TODO, should use some params.system get method
|
|
|
|
|
|
|
|
return std::make_shared<HttpReq>("thegamesdb.net", path);
|
2013-09-17 21:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<MetaDataList> GamesDBScraper::parseReq(ScraperSearchParams params, std::shared_ptr<HttpReq> req)
|
|
|
|
{
|
|
|
|
std::vector<MetaDataList> mdl;
|
|
|
|
|
2013-09-20 19:55:44 +00:00
|
|
|
if(req->status() != HttpReq::REQ_SUCCESS)
|
|
|
|
{
|
|
|
|
LOG(LogError) << "HttpReq error";
|
|
|
|
return mdl;
|
|
|
|
}
|
|
|
|
|
|
|
|
pugi::xml_document doc;
|
|
|
|
pugi::xml_parse_result parseResult = doc.load(req->getContent().c_str());
|
|
|
|
if(!parseResult)
|
|
|
|
{
|
|
|
|
LOG(LogError) << "Error parsing XML";
|
|
|
|
return mdl;
|
|
|
|
}
|
2013-09-19 23:41:14 +00:00
|
|
|
|
2013-09-20 19:55:44 +00:00
|
|
|
pugi::xml_node data = doc.child("Data");
|
|
|
|
|
|
|
|
std::string baseImageUrl = data.child("baseImgUrl").text().get();
|
|
|
|
|
|
|
|
unsigned int resultNum = 0;
|
|
|
|
pugi::xml_node game = data.child("Game");
|
|
|
|
while(game && resultNum < 5)
|
|
|
|
{
|
|
|
|
mdl.push_back(MetaDataList(params.system->getGameMDD()));
|
|
|
|
mdl.back().set("name", game.child("GameTitle").text().get());
|
|
|
|
mdl.back().set("desc", game.child("Overview").text().get());
|
|
|
|
|
|
|
|
resultNum++;
|
|
|
|
game = game.next_sibling("Game");
|
|
|
|
}
|
2013-09-19 23:41:14 +00:00
|
|
|
|
2013-09-17 21:50:49 +00:00
|
|
|
return mdl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GamesDBScraper::getResultsAsync(ScraperSearchParams params, Window* window, std::function<void(std::vector<MetaDataList>)> returnFunc)
|
|
|
|
{
|
2013-09-20 19:55:44 +00:00
|
|
|
std::shared_ptr<HttpReq> httpreq = makeHttpReq(params);
|
2013-09-17 21:50:49 +00:00
|
|
|
AsyncReqComponent* req = new AsyncReqComponent(window, httpreq,
|
|
|
|
[this, params, returnFunc] (std::shared_ptr<HttpReq> r)
|
|
|
|
{
|
|
|
|
returnFunc(parseReq(params, r));
|
|
|
|
}, [] ()
|
|
|
|
{
|
|
|
|
});
|
|
|
|
|
|
|
|
window->pushGui(req);
|
2013-09-16 19:53:24 +00:00
|
|
|
}
|
2013-09-20 19:55:44 +00:00
|
|
|
|