From d7c10ae6fe96705197e57fd59fffc7387a3caf52 Mon Sep 17 00:00:00 2001 From: nerdspice <49222223+nerdspice@users.noreply.github.com> Date: Thu, 28 Dec 2023 09:27:59 -0500 Subject: [PATCH] add --save-api-json option --- include/config.h | 1 + include/downloader.h | 1 + include/gamedetails.h | 3 +++ main.cpp | 1 + src/downloader.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++ src/gamedetails.cpp | 6 +++++ src/util.cpp | 2 +- 7 files changed, 65 insertions(+), 1 deletion(-) diff --git a/include/config.h b/include/config.h index 9850392..1663098 100644 --- a/include/config.h +++ b/include/config.h @@ -46,6 +46,7 @@ struct DownloadConfig bool bRemoteXML; bool bSaveChangelogs; bool bSaveSerials; + bool bSaveApiJson; bool bSaveLogo; bool bSaveIcon; bool bAutomaticXMLCreation; diff --git a/include/downloader.h b/include/downloader.h index c7c1afe..5a072c5 100644 --- a/include/downloader.h +++ b/include/downloader.h @@ -148,6 +148,7 @@ class Downloader static std::string getSerialsFromJSON(const Json::Value& json); void saveSerials(const std::string& serials, const std::string& filepath); static std::string getChangelogFromJSON(const Json::Value& json); + void saveApiJson(const std::string& json, const std::string& filepath); void saveChangelog(const std::string& changelog, const std::string& filepath); static void processDownloadQueue(Config conf, const unsigned int& tid); static void processCloudSaveDownloadQueue(Config conf, const unsigned int& tid); diff --git a/include/gamedetails.h b/include/gamedetails.h index f45f94a..954951c 100644 --- a/include/gamedetails.h +++ b/include/gamedetails.h @@ -33,12 +33,14 @@ class gameDetails std::string serials; std::string changelog; std::string logo; + std::string apiJson; void filterWithPriorities(const gameSpecificConfig& config); void makeFilepaths(const DirectoryConfig& config); std::string getSerialsFilepath(); std::string getLogoFilepath(); std::string getIconFilepath(); std::string getChangelogFilepath(); + std::string getApiJsonFilepath(); Json::Value getDetailsAsJson(); std::vector getGameFileVector(); std::vector getGameFileVectorFiltered(const unsigned int& iType); @@ -52,6 +54,7 @@ class gameDetails std::string logoFilepath; std::string iconFilepath; std::string changelogFilepath; + std::string apiJsonFilepath; }; #endif // GAMEDETAILS_H diff --git a/main.cpp b/main.cpp index 468e772..84775dd 100644 --- a/main.cpp +++ b/main.cpp @@ -261,6 +261,7 @@ int main(int argc, char *argv[]) ("use-cache", bpo::value(&Globals::globalConfig.bUseCache)->zero_tokens()->default_value(false), ("Use game details cache")) ("cache-valid", bpo::value(&Globals::globalConfig.iCacheValid)->default_value(2880), ("Set how long cached game details are valid (in minutes)\nDefault: 2880 minutes (48 hours)")) ("save-serials", bpo::value(&Globals::globalConfig.dlConf.bSaveSerials)->zero_tokens()->default_value(false), "Save serial numbers when downloading") + ("save-api-json", bpo::value(&Globals::globalConfig.dlConf.bSaveApiJson)->zero_tokens()->default_value(false), "Save game details JSON data AS-IS from the API to \"api.json\"") ("save-logo", bpo::value(&Globals::globalConfig.dlConf.bSaveLogo)->zero_tokens()->default_value(false), "Save logo when downloading") ("save-icon", bpo::value(&Globals::globalConfig.dlConf.bSaveIcon)->zero_tokens()->default_value(false), "Save icon when downloading") ("ignore-dlc-count", bpo::value(&Globals::globalConfig.sIgnoreDLCCountRegex)->implicit_value(".*"), "Set regular expression filter for games to ignore DLC count information\nIgnoring DLC count information helps in situations where the account page doesn't provide accurate information about DLCs") diff --git a/src/downloader.cpp b/src/downloader.cpp index 0615922..80fcaeb 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -707,6 +707,12 @@ void Downloader::download() this->saveChangelog(games[i].changelog, filepath); } + if (conf.dlConf.bSaveApiJson && !games[i].apiJson.empty()) + { + std::string filepath = games[i].getApiJsonFilepath(); + this->saveApiJson(games[i].apiJson, filepath); + } + if ((conf.dlConf.iInclude & GlobalConstants::GFTYPE_DLC) && !games[i].dlcs.empty()) { for (unsigned int j = 0; j < games[i].dlcs.size(); ++j) @@ -2210,6 +2216,47 @@ void Downloader::updateCache() return; } +// Save API JSON to file +void Downloader::saveApiJson(const std::string& json, const std::string& filepath) +{ + // Get directory from filepath + boost::filesystem::path pathname = filepath; + std::string directory = pathname.parent_path().string(); + + // Check that directory exists and create subdirectories + boost::filesystem::path path = directory; + if (boost::filesystem::exists(path)) + { + if (!boost::filesystem::is_directory(path)) + { + std::cout << path << " is not directory" << std::endl; + return; + } + } + else + { + if (!boost::filesystem::create_directories(path)) + { + std::cout << "Failed to create directory: " << path << std::endl; + return; + } + } + + std::ofstream ofs(filepath); + if (ofs) + { + std::cout << "Saving API JSON: " << filepath << std::endl; + ofs << json; + ofs.close(); + } + else + { + std::cout << "Failed to create file: " << filepath << std::endl; + } + + return; +} + // Save serials to file void Downloader::saveSerials(const std::string& serials, const std::string& filepath) { @@ -3633,6 +3680,7 @@ void Downloader::getGameDetailsThread(Config config, const unsigned int& tid) if ((conf.dlConf.bSaveSerials && game.serials.empty()) || (conf.dlConf.bSaveChangelogs && game.changelog.empty()) + || (conf.dlConf.bSaveApiJson && game.apiJson.empty()) ) { Json::Value gameDetailsJSON; @@ -3648,6 +3696,10 @@ void Downloader::getGameDetailsThread(Config config, const unsigned int& tid) if (conf.dlConf.bSaveChangelogs && game.changelog.empty()) game.changelog = Downloader::getChangelogFromJSON(gameDetailsJSON); + + if (conf.dlConf.bSaveApiJson && game.apiJson.empty()) { + game.apiJson = gameDetailsJSON.toStyledString(); + } } game.makeFilepaths(conf.dirConf); diff --git a/src/gamedetails.cpp b/src/gamedetails.cpp index fc8b0f8..fb90422 100644 --- a/src/gamedetails.cpp +++ b/src/gamedetails.cpp @@ -95,6 +95,7 @@ void gameDetails::makeFilepaths(const DirectoryConfig& config) this->logoFilepath = Util::makeFilepath(directory, "logo" + logo_ext, this->gamename, subdir, 0); this->iconFilepath = Util::makeFilepath(directory, "icon" + icon_ext, this->gamename, subdir, 0); this->changelogFilepath = Util::makeFilepath(directory, "changelog_" + gamename + ".html", this->gamename, subdir, 0); + this->apiJsonFilepath = Util::makeFilepath(directory, "api.json", this->gamename, subdir, 0); for (unsigned int i = 0; i < this->installers.size(); ++i) { @@ -212,6 +213,11 @@ std::string gameDetails::getChangelogFilepath() return this->changelogFilepath; } +std::string gameDetails::getApiJsonFilepath() +{ + return this->apiJsonFilepath; +} + // Return vector containing all game files std::vector gameDetails::getGameFileVector() { diff --git a/src/util.cpp b/src/util.cpp index e577e7f..7fab780 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -439,7 +439,7 @@ void Util::filepathReplaceReservedStrings(std::string& str, const std::string& g platform = "no_platform"; } - // Don't save artwork in "no_platform" folder + // Don't save artwork in "no_platform" folder if (str.rfind("/icon.png") != std::string::npos || str.rfind("/logo.jpg") != std::string::npos) { platform = "";