From b2f615347e81c685d02a95eae7b97b7fa584f39d Mon Sep 17 00:00:00 2001 From: Aloshi Date: Tue, 24 Sep 2013 13:06:13 -0500 Subject: [PATCH] IScraper renamed to Scraper. Scraper now contains a partial implementation since almost all scrapers will follow the pattern of create HttpReq -> create and wait for AsyncReqComponent -> parse HttpReq contents. --- CMakeLists.txt | 1 + src/Settings.cpp | 2 +- src/Settings.h | 4 ++-- src/scrapers/GamesDBScraper.cpp | 22 ---------------------- src/scrapers/GamesDBScraper.h | 11 +++-------- src/scrapers/Scraper.cpp | 25 +++++++++++++++++++++++++ src/scrapers/Scraper.h | 12 ++++++++---- src/scrapers/TheArchiveScraper.cpp | 22 ---------------------- src/scrapers/TheArchiveScraper.h | 10 +++------- 9 files changed, 43 insertions(+), 66 deletions(-) create mode 100644 src/scrapers/Scraper.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d74b2386..6d781c99a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -230,6 +230,7 @@ set(ES_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/components/GuiInputConfig.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/GuiMenu.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/GuiSettingsMenu.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/scrapers/Scraper.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 diff --git a/src/Settings.cpp b/src/Settings.cpp index 75f0277af..78f1c96d8 100644 --- a/src/Settings.cpp +++ b/src/Settings.cpp @@ -92,7 +92,7 @@ void Settings::loadFile() setFloat(node.attribute("name").as_string(), node.attribute("value").as_float()); } -IScraper* Settings::getScraper() +Scraper* Settings::getScraper() { return mScraper; } diff --git a/src/Settings.h b/src/Settings.h index c00c7af86..fad641a6e 100644 --- a/src/Settings.h +++ b/src/Settings.h @@ -23,7 +23,7 @@ public: void setInt(const std::string& name, int value); void setFloat(const std::string& name, float value); - IScraper* getScraper(); + Scraper* getScraper(); private: static Settings* sInstance; @@ -35,7 +35,7 @@ private: std::map mBoolMap; std::map mIntMap; std::map mFloatMap; - IScraper* mScraper; + Scraper* mScraper; }; #endif diff --git a/src/scrapers/GamesDBScraper.cpp b/src/scrapers/GamesDBScraper.cpp index 7e60038d3..27c375cd3 100644 --- a/src/scrapers/GamesDBScraper.cpp +++ b/src/scrapers/GamesDBScraper.cpp @@ -4,14 +4,6 @@ #include "../Log.h" #include "../pugiXML/pugixml.hpp" -std::vector GamesDBScraper::getResults(ScraperSearchParams params) -{ - std::shared_ptr req = makeHttpReq(params); - while(req->status() == HttpReq::REQ_IN_PROGRESS); - - return parseReq(params, req); -} - std::shared_ptr GamesDBScraper::makeHttpReq(ScraperSearchParams params) { std::string path = "/api/GetGame.php?"; @@ -75,17 +67,3 @@ std::vector GamesDBScraper::parseReq(ScraperSearchParams params, s return mdl; } - -void GamesDBScraper::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/GamesDBScraper.h b/src/scrapers/GamesDBScraper.h index f518c246f..3c80cde50 100644 --- a/src/scrapers/GamesDBScraper.h +++ b/src/scrapers/GamesDBScraper.h @@ -3,14 +3,9 @@ #include "Scraper.h" #include "../HttpReq.h" -class GamesDBScraper : public IScraper +class GamesDBScraper : public Scraper { -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); + std::shared_ptr makeHttpReq(ScraperSearchParams params) override; + std::vector parseReq(ScraperSearchParams params, std::shared_ptr) override; }; - diff --git a/src/scrapers/Scraper.cpp b/src/scrapers/Scraper.cpp new file mode 100644 index 000000000..1a8b26086 --- /dev/null +++ b/src/scrapers/Scraper.cpp @@ -0,0 +1,25 @@ +#include "Scraper.h" +#include "../components/AsyncReqComponent.h" + +std::vector Scraper::getResults(ScraperSearchParams params) +{ + std::shared_ptr req = makeHttpReq(params); + while(req->status() == HttpReq::REQ_IN_PROGRESS); + + return parseReq(params, req); +} + +void Scraper::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)); + }, [returnFunc] () + { + returnFunc(std::vector()); + }); + + window->pushGui(req); +} diff --git a/src/scrapers/Scraper.h b/src/scrapers/Scraper.h index 4e34a3684..9bb2cff7a 100644 --- a/src/scrapers/Scraper.h +++ b/src/scrapers/Scraper.h @@ -3,6 +3,7 @@ #include "../MetaData.h" #include "../SystemData.h" #include "../GameData.h" +#include "../HttpReq.h" #include #include @@ -16,11 +17,14 @@ struct ScraperSearchParams std::string nameOverride; }; -class IScraper +class Scraper { public: //Get a list of potential results. - virtual std::vector getResults(ScraperSearchParams params) = 0; - virtual void getResultsAsync(ScraperSearchParams params, Window* window, std::function)> returnFunc) = 0; -}; + virtual std::vector getResults(ScraperSearchParams params); + virtual void getResultsAsync(ScraperSearchParams params, Window* window, std::function)> returnFunc); +private: + virtual std::shared_ptr makeHttpReq(ScraperSearchParams params) = 0; + virtual std::vector parseReq(ScraperSearchParams params, std::shared_ptr) = 0; +}; diff --git a/src/scrapers/TheArchiveScraper.cpp b/src/scrapers/TheArchiveScraper.cpp index fadbfeba3..73ad0dbc6 100644 --- a/src/scrapers/TheArchiveScraper.cpp +++ b/src/scrapers/TheArchiveScraper.cpp @@ -4,14 +4,6 @@ #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/"; @@ -68,17 +60,3 @@ std::vector TheArchiveScraper::parseReq(ScraperSearchParams params 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 index e88c23e98..a9a027ee8 100644 --- a/src/scrapers/TheArchiveScraper.h +++ b/src/scrapers/TheArchiveScraper.h @@ -3,14 +3,10 @@ #include "Scraper.h" #include "../HttpReq.h" -class TheArchiveScraper : public IScraper +class TheArchiveScraper : public Scraper { -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); + std::shared_ptr makeHttpReq(ScraperSearchParams params) override; + std::vector parseReq(ScraperSearchParams params, std::shared_ptr) override; };