From c9668c66d281b19584fba99dd8c15578d3312112 Mon Sep 17 00:00:00 2001 From: Sude Date: Wed, 4 Sep 2013 19:58:03 +0300 Subject: [PATCH] Fix listing/downloading free games using "--game free" --- src/downloader.cpp | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/downloader.cpp b/src/downloader.cpp index 822ec54..0144491 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -1277,8 +1277,26 @@ std::vector Downloader::getGames() // Get list of free games std::vector Downloader::getFreeGames() { + Json::Value root; + Json::Reader *jsonparser = new Json::Reader; std::vector games; - std::string html = this->getResponse("https://secure.gog.com/catalogue/ajax?a=getGames&tab=all_genres&genre=all_genres&price=0&order=alph&publisher=&releaseDate=&availability=&gameMode=&rating=&search=&sort=vote&system=&language=&mixPage=1"); + std::string json = this->getResponse("https://secure.gog.com/games/ajax?a=search&f={\"price\":[\"free\"]}&p=1&t=all"); + + // Parse JSON + if (!jsonparser->parse(json, root)) + { + #ifdef DEBUG + std::cerr << "DEBUG INFO (Downloader::getFreeGames)" << std::endl << json << std::endl; + #endif + std::cout << jsonparser->getFormatedErrorMessages(); + delete jsonparser; + exit(1); + } + #ifdef DEBUG + std::cerr << "DEBUG INFO (Downloader::getFreeGames)" << std::endl << root << std::endl; + #endif + std::string html = root["result"]["html"].asString(); + delete jsonparser; // Parse HTML to get game names htmlcxx::HTML::ParserDom parser; @@ -1287,14 +1305,14 @@ std::vector Downloader::getFreeGames() tree::iterator end = dom.end(); for (; it != end; ++it) { - if (it->tagName()=="a") + if (it->tagName()=="span") { it->parseAttributes(); std::string classname = it->attribute("class").second; - if (classname=="game-title-link") + if (classname=="gog-price game-owned") { - std::string game = it->attribute("href").second; - game.assign(game.begin()+game.find_last_of("/")+1,game.end()); + // Game name is contained in data-gameindex attribute + std::string game = it->attribute("data-gameindex").second; if (!game.empty()) games.push_back(game); }