diff --git a/CMakeLists.txt b/CMakeLists.txt index 9661f97f9..3d74b2386 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -179,6 +179,7 @@ set(ES_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/src/components/GuiSettingsMenu.h ${CMAKE_CURRENT_SOURCE_DIR}/src/scrapers/Scraper.h ${CMAKE_CURRENT_SOURCE_DIR}/src/scrapers/GamesDBScraper.h + ${CMAKE_CURRENT_SOURCE_DIR}/src/scrapers/TheArchiveScraper.h ${CMAKE_CURRENT_SOURCE_DIR}/src/pugiXML/pugiconfig.hpp ${CMAKE_CURRENT_SOURCE_DIR}/src/pugiXML/pugixml.hpp ${CMAKE_CURRENT_SOURCE_DIR}/src/resources/ResourceManager.h @@ -230,6 +231,7 @@ set(ES_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/components/GuiMenu.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/GuiSettingsMenu.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/scrapers/GamesDBScraper.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/scrapers/TheArchiveScraper.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/pugiXML/pugixml.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/resources/ResourceManager.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/resources/TextureResource.cpp diff --git a/src/Settings.cpp b/src/Settings.cpp index 211679d12..75f0277af 100644 --- a/src/Settings.cpp +++ b/src/Settings.cpp @@ -4,6 +4,7 @@ #include "platform.h" #include #include "scrapers/GamesDBScraper.h" +#include "scrapers/TheArchiveScraper.h" Settings* Settings::sInstance = NULL; diff --git a/src/scrapers/TheArchiveScraper.cpp b/src/scrapers/TheArchiveScraper.cpp new file mode 100644 index 000000000..fadbfeba3 --- /dev/null +++ b/src/scrapers/TheArchiveScraper.cpp @@ -0,0 +1,84 @@ +#include "TheArchiveScraper.h" +#include "../components/GuiGameScraper.h" +#include "../components/AsyncReqComponent.h" +#include "../Log.h" +#include "../pugiXML/pugixml.hpp" + +std::vector TheArchiveScraper::getResults(ScraperSearchParams params) +{ + std::shared_ptr req = makeHttpReq(params); + while(req->status() == HttpReq::REQ_IN_PROGRESS); + + return parseReq(params, req); +} + +std::shared_ptr TheArchiveScraper::makeHttpReq(ScraperSearchParams params) +{ + std::string path = "/2.0/Archive.search/xml/7TTRM4MNTIKR2NNAGASURHJOZJ3QXQC5/"; + + std::string cleanName = params.nameOverride; + if(cleanName.empty()) + cleanName = params.game->getCleanName(); + + path += HttpReq::urlEncode(cleanName); + //platform TODO, should use some params.system get method + + return std::make_shared("api.archive.vg", path); +} + +std::vector TheArchiveScraper::parseReq(ScraperSearchParams params, std::shared_ptr req) +{ + std::vector mdl; + + 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; + } + + pugi::xml_node data = doc.child("OpenSearchDescription").child("games"); + + unsigned int resultNum = 0; + pugi::xml_node game = data.child("game"); + while(game && resultNum < MAX_SCRAPER_RESULTS) + { + mdl.push_back(MetaDataList(params.system->getGameMDD())); + mdl.back().set("name", game.child("title").text().get()); + mdl.back().set("desc", game.child("description").text().get()); + + pugi::xml_node image = game.child("box_front"); + pugi::xml_node thumbnail = game.child("box_front_small"); + + if (image) + mdl.back().set("image",image.text().get()); + if (thumbnail) + mdl.back().set("thumbnail", thumbnail.text().get()); + + resultNum++; + game = game.next_sibling("game"); + } + + return mdl; +} + +void TheArchiveScraper::getResultsAsync(ScraperSearchParams params, Window* window, std::function)> returnFunc) +{ + std::shared_ptr httpreq = makeHttpReq(params); + AsyncReqComponent* req = new AsyncReqComponent(window, httpreq, + [this, params, returnFunc] (std::shared_ptr r) + { + returnFunc(parseReq(params, r)); + }, [] () + { + }); + + window->pushGui(req); +} diff --git a/src/scrapers/TheArchiveScraper.h b/src/scrapers/TheArchiveScraper.h new file mode 100644 index 000000000..e88c23e98 --- /dev/null +++ b/src/scrapers/TheArchiveScraper.h @@ -0,0 +1,16 @@ +#pragma once + +#include "Scraper.h" +#include "../HttpReq.h" + +class TheArchiveScraper : public IScraper +{ +public: + std::vector getResults(ScraperSearchParams params) override; + void getResultsAsync(ScraperSearchParams params, Window* window, std::function)> returnFunc) override; + +private: + std::shared_ptr makeHttpReq(ScraperSearchParams params); + std::vector parseReq(ScraperSearchParams params, std::shared_ptr); +}; +