Partial implementation for TheGamesDB scraper.

Still needs a way to display error messages.
This commit is contained in:
Aloshi 2013-09-20 14:55:44 -05:00
parent 3105073e50
commit 9ce511cc71
2 changed files with 44 additions and 13 deletions

View file

@ -1,40 +1,70 @@
#include "GamesDBScraper.h"
#include "../components/AsyncReqComponent.h"
#include "../Log.h"
#include "../pugiXML/pugixml.hpp"
std::vector<MetaDataList> GamesDBScraper::getResults(ScraperSearchParams params)
{
std::shared_ptr<HttpReq> req = makeHttpReq();
std::shared_ptr<HttpReq> req = makeHttpReq(params);
while(req->status() == HttpReq::REQ_IN_PROGRESS);
return parseReq(params, req);
}
std::shared_ptr<HttpReq> GamesDBScraper::makeHttpReq()
std::shared_ptr<HttpReq> GamesDBScraper::makeHttpReq(ScraperSearchParams params)
{
return std::make_shared<HttpReq>("cdn.garcya.us", "/wp-content/uploads/2010/04/TD250.jpg");
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);
}
std::vector<MetaDataList> GamesDBScraper::parseReq(ScraperSearchParams params, std::shared_ptr<HttpReq> req)
{
std::vector<MetaDataList> mdl;
MetaDataList md(params.system->getGameMDD());
md.set("name", "JUNK RESULT #1");
md.set("desc", "Black triangles");
mdl.push_back(md);
if(req->status() != HttpReq::REQ_SUCCESS)
{
LOG(LogError) << "HttpReq error";
return mdl;
}
MetaDataList md2(params.system->getGameMDD());
md2.set("name", "JUNK RESULT #2");
md2.set("desc", "Test results are very exciting. Sort of. A little. If you squint. A lot.");
mdl.push_back(md2);
pugi::xml_document doc;
pugi::xml_parse_result parseResult = doc.load(req->getContent().c_str());
if(!parseResult)
{
LOG(LogError) << "Error parsing XML";
return mdl;
}
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");
}
return mdl;
}
void GamesDBScraper::getResultsAsync(ScraperSearchParams params, Window* window, std::function<void(std::vector<MetaDataList>)> returnFunc)
{
std::shared_ptr<HttpReq> httpreq = makeHttpReq();
std::shared_ptr<HttpReq> httpreq = makeHttpReq(params);
AsyncReqComponent* req = new AsyncReqComponent(window, httpreq,
[this, params, returnFunc] (std::shared_ptr<HttpReq> r)
{
@ -45,3 +75,4 @@ void GamesDBScraper::getResultsAsync(ScraperSearchParams params, Window* window,
window->pushGui(req);
}

View file

@ -10,7 +10,7 @@ public:
void getResultsAsync(ScraperSearchParams params, Window* window, std::function<void(std::vector<MetaDataList>)> returnFunc) override;
private:
std::shared_ptr<HttpReq> makeHttpReq();
std::shared_ptr<HttpReq> makeHttpReq(ScraperSearchParams params);
std::vector<MetaDataList> parseReq(ScraperSearchParams params, std::shared_ptr<HttpReq>);
};