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.
This commit is contained in:
Aloshi 2013-09-24 13:06:13 -05:00
parent fdb1358a7f
commit b2f615347e
9 changed files with 43 additions and 66 deletions

View file

@ -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

View file

@ -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;
}

View file

@ -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<std::string, bool> mBoolMap;
std::map<std::string, int> mIntMap;
std::map<std::string, float> mFloatMap;
IScraper* mScraper;
Scraper* mScraper;
};
#endif

View file

@ -4,14 +4,6 @@
#include "../Log.h"
#include "../pugiXML/pugixml.hpp"
std::vector<MetaDataList> GamesDBScraper::getResults(ScraperSearchParams params)
{
std::shared_ptr<HttpReq> req = makeHttpReq(params);
while(req->status() == HttpReq::REQ_IN_PROGRESS);
return parseReq(params, req);
}
std::shared_ptr<HttpReq> GamesDBScraper::makeHttpReq(ScraperSearchParams params)
{
std::string path = "/api/GetGame.php?";
@ -75,17 +67,3 @@ std::vector<MetaDataList> GamesDBScraper::parseReq(ScraperSearchParams params, s
return mdl;
}
void GamesDBScraper::getResultsAsync(ScraperSearchParams params, Window* window, std::function<void(std::vector<MetaDataList>)> returnFunc)
{
std::shared_ptr<HttpReq> httpreq = makeHttpReq(params);
AsyncReqComponent* req = new AsyncReqComponent(window, httpreq,
[this, params, returnFunc] (std::shared_ptr<HttpReq> r)
{
returnFunc(parseReq(params, r));
}, [] ()
{
});
window->pushGui(req);
}

View file

@ -3,14 +3,9 @@
#include "Scraper.h"
#include "../HttpReq.h"
class GamesDBScraper : public IScraper
class GamesDBScraper : public Scraper
{
public:
std::vector<MetaDataList> getResults(ScraperSearchParams params) override;
void getResultsAsync(ScraperSearchParams params, Window* window, std::function<void(std::vector<MetaDataList>)> returnFunc) override;
private:
std::shared_ptr<HttpReq> makeHttpReq(ScraperSearchParams params);
std::vector<MetaDataList> parseReq(ScraperSearchParams params, std::shared_ptr<HttpReq>);
std::shared_ptr<HttpReq> makeHttpReq(ScraperSearchParams params) override;
std::vector<MetaDataList> parseReq(ScraperSearchParams params, std::shared_ptr<HttpReq>) override;
};

25
src/scrapers/Scraper.cpp Normal file
View file

@ -0,0 +1,25 @@
#include "Scraper.h"
#include "../components/AsyncReqComponent.h"
std::vector<MetaDataList> Scraper::getResults(ScraperSearchParams params)
{
std::shared_ptr<HttpReq> req = makeHttpReq(params);
while(req->status() == HttpReq::REQ_IN_PROGRESS);
return parseReq(params, req);
}
void Scraper::getResultsAsync(ScraperSearchParams params, Window* window, std::function<void(std::vector<MetaDataList>)> returnFunc)
{
std::shared_ptr<HttpReq> httpreq = makeHttpReq(params);
AsyncReqComponent* req = new AsyncReqComponent(window, httpreq,
[this, params, returnFunc] (std::shared_ptr<HttpReq> r)
{
returnFunc(parseReq(params, r));
}, [returnFunc] ()
{
returnFunc(std::vector<MetaDataList>());
});
window->pushGui(req);
}

View file

@ -3,6 +3,7 @@
#include "../MetaData.h"
#include "../SystemData.h"
#include "../GameData.h"
#include "../HttpReq.h"
#include <vector>
#include <functional>
@ -16,11 +17,14 @@ struct ScraperSearchParams
std::string nameOverride;
};
class IScraper
class Scraper
{
public:
//Get a list of potential results.
virtual std::vector<MetaDataList> getResults(ScraperSearchParams params) = 0;
virtual void getResultsAsync(ScraperSearchParams params, Window* window, std::function<void(std::vector<MetaDataList>)> returnFunc) = 0;
};
virtual std::vector<MetaDataList> getResults(ScraperSearchParams params);
virtual void getResultsAsync(ScraperSearchParams params, Window* window, std::function<void(std::vector<MetaDataList>)> returnFunc);
private:
virtual std::shared_ptr<HttpReq> makeHttpReq(ScraperSearchParams params) = 0;
virtual std::vector<MetaDataList> parseReq(ScraperSearchParams params, std::shared_ptr<HttpReq>) = 0;
};

View file

@ -4,14 +4,6 @@
#include "../Log.h"
#include "../pugiXML/pugixml.hpp"
std::vector<MetaDataList> TheArchiveScraper::getResults(ScraperSearchParams params)
{
std::shared_ptr<HttpReq> req = makeHttpReq(params);
while(req->status() == HttpReq::REQ_IN_PROGRESS);
return parseReq(params, req);
}
std::shared_ptr<HttpReq> TheArchiveScraper::makeHttpReq(ScraperSearchParams params)
{
std::string path = "/2.0/Archive.search/xml/7TTRM4MNTIKR2NNAGASURHJOZJ3QXQC5/";
@ -68,17 +60,3 @@ std::vector<MetaDataList> TheArchiveScraper::parseReq(ScraperSearchParams params
return mdl;
}
void TheArchiveScraper::getResultsAsync(ScraperSearchParams params, Window* window, std::function<void(std::vector<MetaDataList>)> returnFunc)
{
std::shared_ptr<HttpReq> httpreq = makeHttpReq(params);
AsyncReqComponent* req = new AsyncReqComponent(window, httpreq,
[this, params, returnFunc] (std::shared_ptr<HttpReq> r)
{
returnFunc(parseReq(params, r));
}, [] ()
{
});
window->pushGui(req);
}

View file

@ -3,14 +3,10 @@
#include "Scraper.h"
#include "../HttpReq.h"
class TheArchiveScraper : public IScraper
class TheArchiveScraper : public Scraper
{
public:
std::vector<MetaDataList> getResults(ScraperSearchParams params) override;
void getResultsAsync(ScraperSearchParams params, Window* window, std::function<void(std::vector<MetaDataList>)> returnFunc) override;
private:
std::shared_ptr<HttpReq> makeHttpReq(ScraperSearchParams params);
std::vector<MetaDataList> parseReq(ScraperSearchParams params, std::shared_ptr<HttpReq>);
std::shared_ptr<HttpReq> makeHttpReq(ScraperSearchParams params) override;
std::vector<MetaDataList> parseReq(ScraperSearchParams params, std::shared_ptr<HttpReq>) override;
};