From 9ce511cc7155b0bf24e848125878265275ce641e Mon Sep 17 00:00:00 2001 From: Aloshi Date: Fri, 20 Sep 2013 14:55:44 -0500 Subject: [PATCH] Partial implementation for TheGamesDB scraper. Still needs a way to display error messages. --- src/scrapers/GamesDBScraper.cpp | 55 ++++++++++++++++++++++++++------- src/scrapers/GamesDBScraper.h | 2 +- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/scrapers/GamesDBScraper.cpp b/src/scrapers/GamesDBScraper.cpp index 0da2b3f8c..84143d7d8 100644 --- a/src/scrapers/GamesDBScraper.cpp +++ b/src/scrapers/GamesDBScraper.cpp @@ -1,40 +1,70 @@ #include "GamesDBScraper.h" #include "../components/AsyncReqComponent.h" #include "../Log.h" +#include "../pugiXML/pugixml.hpp" std::vector GamesDBScraper::getResults(ScraperSearchParams params) { - std::shared_ptr req = makeHttpReq(); + std::shared_ptr req = makeHttpReq(params); while(req->status() == HttpReq::REQ_IN_PROGRESS); return parseReq(params, req); } -std::shared_ptr GamesDBScraper::makeHttpReq() +std::shared_ptr GamesDBScraper::makeHttpReq(ScraperSearchParams params) { - return std::make_shared("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("thegamesdb.net", path); } std::vector GamesDBScraper::parseReq(ScraperSearchParams params, std::shared_ptr req) { std::vector 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)> returnFunc) { - std::shared_ptr httpreq = makeHttpReq(); + std::shared_ptr httpreq = makeHttpReq(params); AsyncReqComponent* req = new AsyncReqComponent(window, httpreq, [this, params, returnFunc] (std::shared_ptr r) { @@ -45,3 +75,4 @@ void GamesDBScraper::getResultsAsync(ScraperSearchParams params, Window* window, window->pushGui(req); } + diff --git a/src/scrapers/GamesDBScraper.h b/src/scrapers/GamesDBScraper.h index 5b96fb253..f518c246f 100644 --- a/src/scrapers/GamesDBScraper.h +++ b/src/scrapers/GamesDBScraper.h @@ -10,7 +10,7 @@ public: void getResultsAsync(ScraperSearchParams params, Window* window, std::function)> returnFunc) override; private: - std::shared_ptr makeHttpReq(); + std::shared_ptr makeHttpReq(ScraperSearchParams params); std::vector parseReq(ScraperSearchParams params, std::shared_ptr); };