Add checks for the Scraper, when the settings no longer match the list of available scrapers.

If the Scraper configured in the settings is no longer available, don't crash when running the scraper.
For single game scrapes, we show an error for the user to change the configuration.
For batch scraping, silently choose the 1st scraper available in the list
This commit is contained in:
Cristi Mitrana 2019-01-24 20:00:19 +02:00
parent cdd43bf7e9
commit ce04f7f297
4 changed files with 38 additions and 9 deletions

View file

@ -231,14 +231,23 @@ void ScraperSearchComponent::onSearchDone(const std::vector<ScraperSearchResult>
unsigned int color = 0x777777FF;
if(results.empty())
{
ComponentListRow row;
row.addElement(std::make_shared<TextComponent>(mWindow, "NO GAMES FOUND - SKIP", font, color), true);
// Check if the scraper used is still valid
if (!isValidConfiguredScraper())
{
mWindow->pushGui(new GuiMsgBox(mWindow, Utils::String::toUpper("Configured scraper is no longer available.\nPlease change the scraping source in the settings."),
"FINISH", mSkipCallback));
}
else
{
ComponentListRow row;
row.addElement(std::make_shared<TextComponent>(mWindow, "NO GAMES FOUND - SKIP", font, color), true);
if(mSkipCallback)
row.makeAcceptInputHandler(mSkipCallback);
if(mSkipCallback)
row.makeAcceptInputHandler(mSkipCallback);
mResultList->addRow(row);
mGrid.resetCursor();
mResultList->addRow(row);
mGrid.resetCursor();
}
}else{
ComponentListRow row;
for(size_t i = 0; i < results.size(); i++)

View file

@ -55,8 +55,10 @@ void GuiMenu::openScraperSettings()
// scrape from
auto scraper_list = std::make_shared< OptionListComponent< std::string > >(mWindow, "SCRAPE FROM", false);
std::vector<std::string> scrapers = getScraperList();
// Select either the first entry of the one read from the settings, just in case the scraper from settings has vanished.
for(auto it = scrapers.cbegin(); it != scrapers.cend(); it++)
scraper_list->add(*it, *it, *it == Settings::getInstance()->getString("Scraper"));
scraper_list->add(*it, *it, *it == Settings::getInstance()->getString("Scraper") || it==scrapers.cbegin());
s->addWithLabel("SCRAPE FROM", scraper_list);
s->addSaveFunc([scraper_list] { Settings::getInstance()->setString("Scraper", scraper_list->getSelected()); });

View file

@ -17,9 +17,18 @@ const std::map<std::string, generate_scraper_requests_func> scraper_request_func
std::unique_ptr<ScraperSearchHandle> startScraperSearch(const ScraperSearchParams& params)
{
const std::string& name = Settings::getInstance()->getString("Scraper");
std::unique_ptr<ScraperSearchHandle> handle(new ScraperSearchHandle());
scraper_request_funcs.at(name)(params, handle->mRequestQueue, handle->mResults);
// Check if the Scraper in the settings still exists as a registered scraping source.
if (scraper_request_funcs.find(name) == scraper_request_funcs.end())
{
LOG(LogWarning) << "Configured scraper (" << name << ") unavailable, scraping aborted.";
}
else
{
scraper_request_funcs.at(name)(params, handle->mRequestQueue, handle->mResults);
}
return handle;
}
@ -34,6 +43,12 @@ std::vector<std::string> getScraperList()
return list;
}
bool isValidConfiguredScraper()
{
const std::string& name = Settings::getInstance()->getString("Scraper");
return scraper_request_funcs.find(name) != scraper_request_funcs.end();
}
// ScraperSearchHandle
ScraperSearchHandle::ScraperSearchHandle()
{

View file

@ -113,6 +113,9 @@ std::unique_ptr<ScraperSearchHandle> startScraperSearch(const ScraperSearchParam
// returns a list of valid scraper names
std::vector<std::string> getScraperList();
// returns true if the scraper configured in the settings is still valid
bool isValidConfiguredScraper();
typedef void (*generate_scraper_requests_func)(const ScraperSearchParams& params, std::queue< std::unique_ptr<ScraperRequest> >& requests, std::vector<ScraperSearchResult>& results);
// -------------------------------------------------------------------------