Skip to content

Commit

Permalink
Don't try to get details for unowned DLCs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sude- committed Mar 27, 2023
1 parent b520eb8 commit 9cd85f6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 53 deletions.
1 change: 1 addition & 0 deletions include/globals.h
Expand Up @@ -15,6 +15,7 @@ namespace Globals
{
extern GalaxyConfig galaxyConf;
extern Config globalConfig;
extern std::vector<std::string> vOwnedGamesIds;
}

#endif // GLOBALS_H_INCLUDED
Expand Down
2 changes: 2 additions & 0 deletions include/website.h
Expand Up @@ -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<gameItem> getGames();
std::vector<wishlistItem> getWishlistItems();
bool IsLoggedIn();
std::map<std::string, std::string> getTags();
std::vector<std::string> getOwnedGamesIds();
virtual ~Website();
protected:
private:
Expand Down
1 change: 1 addition & 0 deletions src/downloader.cpp
Expand Up @@ -47,6 +47,7 @@ struct cloudSaveFile {
std::string location;
};

std::vector<std::string> Globals::vOwnedGamesIds;
std::vector<DownloadInfo> vDownloadInfo;
ThreadSafeQueue<gameFile> dlQueue;
ThreadSafeQueue<cloudSaveFile> dlCloudSaveQueue;
Expand Down
8 changes: 8 additions & 0 deletions src/galaxyapi.cpp
Expand Up @@ -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
Expand Down
99 changes: 46 additions & 53 deletions src/website.cpp
Expand Up @@ -61,27 +61,35 @@ std::string Website::getResponse(const std::string& url)
return response;
}

Json::Value Website::getGameDetailsJSON(const std::string& gameid)
Json::Value Website::getResponseJson(const std::string& url)
{
std::string gameDataUrl = "https://www.gog.com/account/gameDetails/" + gameid + ".json";
std::string json = this->getResponse(gameDataUrl);

// Parse JSON
Json::Value root;
std::istringstream json_stream(json);
std::istringstream response(this->getResponse(url));
Json::Value 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;
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 << exc.what();
std::cout << "Failed to parse json: " << exc.what();
}
}
if (Globals::globalConfig.iMsgLevel >= MSGLEVEL_DEBUG)
std::cerr << "DEBUG INFO (Website::getGameDetailsJSON)" << std::endl << root << std::endl;

return root;
return json;
}

Json::Value Website::getGameDetailsJSON(const std::string& gameid)
{
std::string gameDataUrl = "https://www.gog.com/account/gameDetails/" + gameid + ".json";
Json::Value json = this->getResponseJson(gameDataUrl);

return json;
}

// Get list of games from account page
Expand All @@ -102,34 +110,16 @@ std::vector<gameItem> 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;
Expand Down Expand Up @@ -642,21 +632,10 @@ std::vector<wishlistItem> 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;
Expand Down Expand Up @@ -789,3 +768,17 @@ std::map<std::string, std::string> Website::getTagsFromJson(const Json::Value& j

return tags;
}

std::vector<std::string> Website::getOwnedGamesIds()
{
std::vector<std::string> 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;
}

0 comments on commit 9cd85f6

Please sign in to comment.