diff --git a/include/globals.h b/include/globals.h index 9824d8a..a250717 100644 --- a/include/globals.h +++ b/include/globals.h @@ -15,6 +15,7 @@ namespace Globals { extern GalaxyConfig galaxyConf; extern Config globalConfig; + extern std::vector vOwnedGamesIds; } #endif // GLOBALS_H_INCLUDED diff --git a/include/website.h b/include/website.h index a7d95de..6cd7288 100644 --- a/include/website.h +++ b/include/website.h @@ -20,11 +20,13 @@ class Website Website(); int Login(const std::string& email, const std::string& password); std::string getResponse(const std::string& url); + Json::Value getResponseJson(const std::string& url); Json::Value getGameDetailsJSON(const std::string& gameid); std::vector getGames(); std::vector getWishlistItems(); bool IsLoggedIn(); std::map getTags(); + std::vector getOwnedGamesIds(); virtual ~Website(); protected: private: diff --git a/src/downloader.cpp b/src/downloader.cpp index 634c389..0897883 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -47,6 +47,7 @@ struct cloudSaveFile { std::string location; }; +std::vector Globals::vOwnedGamesIds; std::vector vDownloadInfo; ThreadSafeQueue dlQueue; ThreadSafeQueue dlCloudSaveQueue; diff --git a/src/galaxyapi.cpp b/src/galaxyapi.cpp index 81bb69f..d48bdc7 100644 --- a/src/galaxyapi.cpp +++ b/src/galaxyapi.cpp @@ -340,6 +340,14 @@ gameDetails galaxyAPI::productInfoJsonToGameDetails(const Json::Value& json, con { for (unsigned int i = 0; i < json["expanded_dlcs"].size(); ++i) { + std::string dlc_id = json["expanded_dlcs"][i]["id"].asString(); + + if (!Globals::vOwnedGamesIds.empty()) + { + if (std::find(Globals::vOwnedGamesIds.begin(), Globals::vOwnedGamesIds.end(), dlc_id) == Globals::vOwnedGamesIds.end()) + continue; + } + gameDetails dlc_gamedetails = this->productInfoJsonToGameDetails(json["expanded_dlcs"][i], dlConf); // Add DLC type to all DLC files diff --git a/src/website.cpp b/src/website.cpp index 2ef126f..b437e11 100644 --- a/src/website.cpp +++ b/src/website.cpp @@ -61,27 +61,35 @@ std::string Website::getResponse(const std::string& url) return response; } +Json::Value Website::getResponseJson(const std::string& url) +{ + std::istringstream response(this->getResponse(url)); + Json::Value json; + + if (!response.str().empty()) + { + try + { + response >> json; + } + catch(const Json::Exception& exc) + { + if (Globals::globalConfig.iMsgLevel >= MSGLEVEL_DEBUG) + std::cerr << "DEBUG INFO (Website::getResponseJson)" << std::endl << json << std::endl; + + std::cout << "Failed to parse json: " << exc.what(); + } + } + + return json; +} + Json::Value Website::getGameDetailsJSON(const std::string& gameid) { std::string gameDataUrl = "https://www.gog.com/account/gameDetails/" + gameid + ".json"; - std::string json = this->getResponse(gameDataUrl); + Json::Value json = this->getResponseJson(gameDataUrl); - // Parse JSON - Json::Value root; - std::istringstream json_stream(json); - - try { - json_stream >> root; - } catch(const Json::Exception& exc) { - if (Globals::globalConfig.iMsgLevel >= MSGLEVEL_DEBUG) - std::cerr << "DEBUG INFO (Website::getGameDetailsJSON)" << std::endl << json << std::endl; - - std::cout << exc.what(); - } - if (Globals::globalConfig.iMsgLevel >= MSGLEVEL_DEBUG) - std::cerr << "DEBUG INFO (Website::getGameDetailsJSON)" << std::endl << root << std::endl; - - return root; + return json; } // Get list of games from account page @@ -102,34 +110,16 @@ std::vector Website::getGames() tags += "," + tag; } + Globals::vOwnedGamesIds = this->getOwnedGamesIds(); do { std::string url = "https://www.gog.com/account/getFilteredProducts?hiddenFlag=" + std::to_string(iHidden) + "&isUpdated=" + std::to_string(iUpdated) + "&mediaType=1&sortBy=title&system=&page=" + std::to_string(i); if (!tags.empty()) url += "&tags=" + tags; - std::string response = this->getResponse(url); - std::istringstream json_stream(response); - try { - // Parse JSON - json_stream >> root; - } catch (const Json::Exception& exc) { - if (Globals::globalConfig.iMsgLevel >= MSGLEVEL_DEBUG) - std::cerr << "DEBUG INFO (Website::getGames)" << std::endl << response << std::endl; - - std::cout << exc.what(); - if (!response.empty()) - { - if(response[0] != '{') - { - // Response was not JSON. Assume that cookies have expired. - std::cerr << "Response was not JSON. Cookies have most likely expired. Try --login first." << std::endl; - } - } - exit(1); - } - if (Globals::globalConfig.iMsgLevel >= MSGLEVEL_DEBUG) - std::cerr << "DEBUG INFO (Website::getGames)" << std::endl << root << std::endl; + Json::Value root = this->getResponseJson(url); + if (root.empty()) + continue; if (root["page"].asInt() == root["totalPages"].asInt() || root["totalPages"].asInt() == 0) bAllPagesParsed = true; @@ -642,21 +632,10 @@ std::vector Website::getWishlistItems() do { - std::string response(this->getResponse("https://www.gog.com/account/wishlist/search?hasHiddenProducts=false&hiddenFlag=0&isUpdated=0&mediaType=0&sortBy=title&system=&page=" + std::to_string(i))); - std::istringstream response_stream(response); - - try { - // Parse JSON - response_stream >> root; - } catch(const Json::Exception& exc) { - if (Globals::globalConfig.iMsgLevel >= MSGLEVEL_DEBUG) - std::cerr << "DEBUG INFO (Website::getWishlistItems)" << std::endl << response << std::endl; - - std::cout << exc.what(); - exit(1); - } - if (Globals::globalConfig.iMsgLevel >= MSGLEVEL_DEBUG) - std::cerr << "DEBUG INFO (Website::getWishlistItems)" << std::endl << root << std::endl; + std::string url = "https://www.gog.com/account/wishlist/search?hasHiddenProducts=false&hiddenFlag=0&isUpdated=0&mediaType=0&sortBy=title&system=&page=" + std::to_string(i); + Json::Value root = this->getResponseJson(url); + if (root.empty()) + continue; if (root["page"].asInt() >= root["totalPages"].asInt()) bAllPagesParsed = true; @@ -789,3 +768,17 @@ std::map Website::getTagsFromJson(const Json::Value& j return tags; } + +std::vector Website::getOwnedGamesIds() +{ + std::vector vOwnedGamesIds; + Json::Value owned_json = this->getResponseJson("https://www.gog.com/user/data/games"); + + if (owned_json.isMember("owned")) + { + for (auto id : owned_json["owned"]) + vOwnedGamesIds.push_back(id.asString()); + } + + return vOwnedGamesIds; +}