2020-05-26 16:34:33 +00:00
|
|
|
//
|
2020-06-21 12:25:28 +00:00
|
|
|
// GamesDBJSONScraperResources.cpp
|
2020-05-26 16:34:33 +00:00
|
|
|
//
|
2020-06-21 12:25:28 +00:00
|
|
|
// Functions specifically for scraping from thegamesdb.net
|
|
|
|
// Called from GamesDBJSONScraper.
|
2020-05-26 16:34:33 +00:00
|
|
|
//
|
2020-06-21 12:25:28 +00:00
|
|
|
// Downloads these resource files to ~/.emulationstation/scrapers:
|
|
|
|
// gamesdb_developers.json
|
|
|
|
// gamesdb_genres.json
|
|
|
|
// gamesdb_publishers.json
|
2020-06-06 11:10:33 +00:00
|
|
|
//
|
2020-05-26 16:34:33 +00:00
|
|
|
|
2019-02-08 02:08:11 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <fstream>
|
|
|
|
#include <memory>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include "Log.h"
|
|
|
|
|
|
|
|
#include "scrapers/GamesDBJSONScraperResources.h"
|
|
|
|
#include "utils/FileSystemUtil.h"
|
|
|
|
|
|
|
|
#include <rapidjson/document.h>
|
|
|
|
#include <rapidjson/error/en.h>
|
|
|
|
|
|
|
|
using namespace rapidjson;
|
|
|
|
|
2020-05-26 16:34:33 +00:00
|
|
|
namespace {
|
|
|
|
constexpr char GamesDBAPIKey[] =
|
2020-06-21 12:25:28 +00:00
|
|
|
"445fcbc3f32bb2474bc27016b99eb963d318ee3a608212c543b9a79de1041600";
|
2019-02-08 02:08:11 +00:00
|
|
|
|
|
|
|
constexpr int MAX_WAIT_MS = 90000;
|
|
|
|
constexpr int POLL_TIME_MS = 500;
|
|
|
|
constexpr int MAX_WAIT_ITER = MAX_WAIT_MS / POLL_TIME_MS;
|
|
|
|
|
|
|
|
constexpr char SCRAPER_RESOURCES_DIR[] = "scrapers";
|
|
|
|
constexpr char DEVELOPERS_JSON_FILE[] = "gamesdb_developers.json";
|
|
|
|
constexpr char PUBLISHERS_JSON_FILE[] = "gamesdb_publishers.json";
|
|
|
|
constexpr char GENRES_JSON_FILE[] = "gamesdb_genres.json";
|
|
|
|
constexpr char DEVELOPERS_ENDPOINT[] = "/Developers";
|
|
|
|
constexpr char PUBLISHERS_ENDPOINT[] = "/Publishers";
|
|
|
|
constexpr char GENRES_ENDPOINT[] = "/Genres";
|
|
|
|
|
|
|
|
std::string genFilePath(const std::string& file_name)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
return Utils::FileSystem::getGenericPath(getScrapersResouceDir() + "/" + file_name);
|
2019-02-08 02:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ensureScrapersResourcesDir()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
std::string path = getScrapersResouceDir();
|
|
|
|
if (!Utils::FileSystem::exists(path))
|
|
|
|
Utils::FileSystem::createDirectory(path);
|
2019-02-08 02:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::string getScrapersResouceDir()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
return Utils::FileSystem::getGenericPath(
|
|
|
|
Utils::FileSystem::getHomePath() + "/.emulationstation/" + SCRAPER_RESOURCES_DIR);
|
2019-02-08 02:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string TheGamesDBJSONRequestResources::getApiKey() const { return GamesDBAPIKey; }
|
|
|
|
|
|
|
|
void TheGamesDBJSONRequestResources::prepare()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if (checkLoaded())
|
|
|
|
return;
|
2019-02-08 02:08:11 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (loadResource(gamesdb_new_developers_map, "developers",
|
|
|
|
genFilePath(DEVELOPERS_JSON_FILE)) && !gamesdb_developers_resource_request)
|
|
|
|
gamesdb_developers_resource_request = fetchResource(DEVELOPERS_ENDPOINT);
|
2020-05-26 16:34:33 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (loadResource(gamesdb_new_publishers_map, "publishers",
|
|
|
|
genFilePath(PUBLISHERS_JSON_FILE)) && !gamesdb_publishers_resource_request)
|
|
|
|
gamesdb_publishers_resource_request = fetchResource(PUBLISHERS_ENDPOINT);
|
2020-05-26 16:34:33 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (loadResource(gamesdb_new_genres_map, "genres",
|
|
|
|
genFilePath(GENRES_JSON_FILE)) && !gamesdb_genres_resource_request)
|
|
|
|
gamesdb_genres_resource_request = fetchResource(GENRES_ENDPOINT);
|
2019-02-08 02:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TheGamesDBJSONRequestResources::ensureResources()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if (checkLoaded())
|
|
|
|
return;
|
2019-02-08 02:08:11 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
for (int i = 0; i < MAX_WAIT_ITER; ++i) {
|
2019-02-08 02:08:11 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (gamesdb_developers_resource_request &&
|
|
|
|
saveResource(gamesdb_developers_resource_request.get(),
|
|
|
|
gamesdb_new_developers_map, "developers", genFilePath(DEVELOPERS_JSON_FILE)))
|
|
|
|
gamesdb_developers_resource_request.reset(nullptr);
|
2020-05-26 16:34:33 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (gamesdb_publishers_resource_request &&
|
|
|
|
saveResource(gamesdb_publishers_resource_request.get(),
|
|
|
|
gamesdb_new_publishers_map, "publishers", genFilePath(PUBLISHERS_JSON_FILE)))
|
|
|
|
gamesdb_publishers_resource_request.reset(nullptr);
|
2020-05-26 16:34:33 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (gamesdb_genres_resource_request && saveResource(gamesdb_genres_resource_request.get(),
|
|
|
|
gamesdb_new_genres_map, "genres", genFilePath(GENRES_JSON_FILE)))
|
|
|
|
gamesdb_genres_resource_request.reset(nullptr);
|
2019-02-08 02:08:11 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (!gamesdb_developers_resource_request && !gamesdb_publishers_resource_request &&
|
|
|
|
!gamesdb_genres_resource_request)
|
|
|
|
return;
|
2020-05-26 16:34:33 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(POLL_TIME_MS));
|
|
|
|
}
|
|
|
|
LOG(LogError) << "Timed out while waiting for resources\n";
|
2019-02-08 02:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TheGamesDBJSONRequestResources::checkLoaded()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
return !gamesdb_new_genres_map.empty() && !gamesdb_new_developers_map.empty() &&
|
|
|
|
!gamesdb_new_publishers_map.empty();
|
2019-02-08 02:08:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 16:34:33 +00:00
|
|
|
bool TheGamesDBJSONRequestResources::saveResource(
|
2020-06-21 12:25:28 +00:00
|
|
|
HttpReq* req,
|
|
|
|
std::unordered_map<int, std::string>& resource,
|
|
|
|
const std::string& resource_name,
|
|
|
|
const std::string& file_name)
|
2019-02-08 02:08:11 +00:00
|
|
|
{
|
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (req == nullptr) {
|
2020-07-26 21:30:45 +00:00
|
|
|
LOG(LogError) << "HTTP request pointer was null.\n";
|
2020-06-21 12:25:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (req->status() == HttpReq::REQ_IN_PROGRESS) {
|
|
|
|
return false; // Not ready: wait some more.
|
|
|
|
}
|
|
|
|
if (req->status() != HttpReq::REQ_SUCCESS) {
|
2020-07-26 21:30:45 +00:00
|
|
|
LOG(LogError) << "Resource request for " << file_name <<
|
2020-06-21 12:25:28 +00:00
|
|
|
" failed:\n\t" << req->getErrorMsg();
|
|
|
|
return true; // Request failed, resetting request..
|
|
|
|
}
|
|
|
|
|
|
|
|
ensureScrapersResourcesDir();
|
|
|
|
|
|
|
|
std::ofstream fout(file_name);
|
|
|
|
fout << req->getContent();
|
|
|
|
fout.close();
|
|
|
|
loadResource(resource, resource_name, file_name);
|
|
|
|
return true;
|
2019-02-08 02:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<HttpReq> TheGamesDBJSONRequestResources::fetchResource(const std::string& endpoint)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
std::string path = "https://api.thegamesdb.net/v1";
|
|
|
|
path += endpoint;
|
|
|
|
path += "?apikey=" + getApiKey();
|
2019-02-08 02:08:11 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
return std::unique_ptr<HttpReq>(new HttpReq(path));
|
2019-02-08 02:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int TheGamesDBJSONRequestResources::loadResource(
|
2020-06-21 12:25:28 +00:00
|
|
|
std::unordered_map<int, std::string>& resource,
|
|
|
|
const std::string& resource_name,
|
|
|
|
const std::string& file_name)
|
2019-02-08 02:08:11 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
std::ifstream fin(file_name);
|
|
|
|
if (!fin.good())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
std::stringstream buffer;
|
|
|
|
buffer << fin.rdbuf();
|
|
|
|
Document doc;
|
|
|
|
doc.Parse(buffer.str().c_str());
|
|
|
|
|
|
|
|
if (doc.HasParseError()) {
|
|
|
|
std::string err = std::string("TheGamesDBJSONRequest - "
|
|
|
|
"Error parsing JSON for resource file ") + file_name +
|
|
|
|
":\n\t" + GetParseError_En(doc.GetParseError());
|
|
|
|
LOG(LogError) << err;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!doc.HasMember("data") || !doc["data"].HasMember(resource_name.c_str()) ||
|
|
|
|
!doc["data"][resource_name.c_str()].IsObject()) {
|
|
|
|
std::string err = "TheGamesDBJSONRequest - Response had no resource data.\n";
|
|
|
|
LOG(LogError) << err;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
auto& data = doc["data"][resource_name.c_str()];
|
|
|
|
|
|
|
|
for (Value::ConstMemberIterator itr = data.MemberBegin(); itr != data.MemberEnd(); ++itr) {
|
|
|
|
auto& entry = itr->value;
|
|
|
|
if (!entry.IsObject() || !entry.HasMember("id") || !entry["id"].IsInt() ||
|
|
|
|
!entry.HasMember("name") || !entry["name"].IsString())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
resource[entry["id"].GetInt()] = entry["name"].GetString();
|
|
|
|
}
|
|
|
|
return resource.empty();
|
2019-02-08 02:08:11 +00:00
|
|
|
}
|