Play-/Source/ui_shared/TheGamesDbClient.cpp

164 lines
4.5 KiB
C++
Raw Normal View History

2017-10-23 07:35:47 -04:00
#include <memory>
#include <cstdlib>
2017-10-23 07:51:20 -04:00
#include <cstring>
2019-01-06 13:42:20 +00:00
#include <sstream>
#include <nlohmann/json.hpp>
2017-10-23 07:35:47 -04:00
#include "TheGamesDbClient.h"
#include "string_format.h"
#include "http/HttpClientFactory.h"
using namespace TheGamesDb;
2019-01-06 13:42:20 +00:00
static const char *g_getGameUrl = "https://api.thegamesdb.net/Games/GamesByGameID?apikey=API_KEY&fields=overview,serials&include=boxart&id=%d";
static const char *g_getGamesBySerialUrl = "https://api.thegamesdb.net/Games/ByGameSerialID?apikey=API_KEY&filter%5Bplatform%5D=11&fields=overview,serials&include=boxart";
static const char* g_getGamesListUrl = "https://api.thegamesdb.net/v1.1/Games/ByGameName?apikey=API_KEY&fields=overview,serials&filter%5Bplatform%5D=%s&include=boxart&name=%s";
2017-10-23 07:35:47 -04:00
2019-01-06 13:42:20 +00:00
GamesList CClient::GetGames(std::vector<std::string> serials)
2017-10-23 07:35:47 -04:00
{
2019-01-06 13:42:20 +00:00
std::ostringstream stream;
std::copy(serials.begin(), serials.end(), std::ostream_iterator<std::string>(stream, ","));
std::string str_games_id = stream.str();
std::vector<Game> gamesList;
auto url = std::string(g_getGamesBySerialUrl);
url += "&id=";
url += str_games_id;
while(!url.empty())
{
auto requestResult =
[&]() {
auto client = Framework::Http::CreateHttpClient();
client->SetUrl(url);
return client->SendRequest();
}();
url.clear();
if(requestResult.statusCode == Framework::Http::HTTP_STATUS_CODE::OK)
{
auto json_ret = requestResult.data.ReadString();
auto parsed_json = nlohmann::json::parse(json_ret);
PopulateGameList(json_ret, gamesList, url);
}
}
return gamesList;
2017-10-23 07:35:47 -04:00
}
Game CClient::GetGame(uint32 id)
{
auto url = string_format(g_getGameUrl, id);
2018-08-29 13:53:51 -04:00
auto requestResult =
[&]() {
auto client = Framework::Http::CreateHttpClient();
client->SetUrl(url);
return client->SendRequest();
}();
2017-10-23 07:35:47 -04:00
if(requestResult.statusCode != Framework::Http::HTTP_STATUS_CODE::OK)
{
throw std::runtime_error("Failed to get game.");
}
2019-01-06 13:42:20 +00:00
auto json_ret = requestResult.data.ReadString();
std::vector<Game> gamesList;
int count = PopulateGameList(json_ret, gamesList);
2017-10-23 07:35:47 -04:00
2019-01-06 13:42:20 +00:00
if(count < 1)
2017-10-23 07:35:47 -04:00
{
2019-01-06 13:42:20 +00:00
throw std::runtime_error("Failed to get game.");
2017-10-23 07:35:47 -04:00
}
Game game;
2019-01-06 13:42:20 +00:00
return gamesList.at(0);;
2017-10-23 07:35:47 -04:00
}
2019-01-06 13:42:20 +00:00
GamesList CClient::GetGamesList(const std::string& platformID, const std::string& name)
2017-10-23 07:35:47 -04:00
{
auto encodedName = Framework::Http::CHttpClient::UrlEncode(name);
2019-01-06 13:42:20 +00:00
auto url = string_format(g_getGamesListUrl, platformID.c_str(), encodedName.c_str());
2018-08-29 13:53:51 -04:00
auto requestResult =
[&]() {
auto client = Framework::Http::CreateHttpClient();
client->SetUrl(url);
return client->SendRequest();
}();
2017-10-23 07:35:47 -04:00
if(requestResult.statusCode != Framework::Http::HTTP_STATUS_CODE::OK)
{
throw std::runtime_error("Failed to get games list.");
}
2019-01-06 13:42:20 +00:00
auto json_ret = requestResult.data.ReadString();
std::vector<Game> gamesList;
int count = PopulateGameList(json_ret, gamesList);
2017-10-23 07:35:47 -04:00
2019-01-06 13:42:20 +00:00
if (count < 1)
2017-10-23 07:35:47 -04:00
{
2019-01-06 13:42:20 +00:00
throw std::runtime_error("Failed to get game.");
2017-10-23 07:35:47 -04:00
}
2019-01-06 13:42:20 +00:00
return gamesList;
}
int CClient::PopulateGameList(std::string &json_ret, std::vector<TheGamesDb::Game> &list)
{
std::string tmp;
return PopulateGameList(json_ret, list, tmp);
}
int CClient::PopulateGameList(std::string &json_ret, std::vector<TheGamesDb::Game> &list, std::string &next_page_url)
{
nlohmann::json parsed_json = nlohmann::json::parse(json_ret);
if(parsed_json["data"]["count"].get<int>() == 0)
return 0;
2017-10-23 07:35:47 -04:00
2019-01-06 13:42:20 +00:00
if(!parsed_json["pages"]["next"].empty())
next_page_url = parsed_json["pages"]["next"].get<std::string>();
auto games = parsed_json["data"]["games"].get<std::vector<nlohmann::json>>();
std::string image_base = "";
auto includes = parsed_json["include"];
if(!includes.empty())
2017-10-23 07:35:47 -04:00
{
2019-01-06 13:42:20 +00:00
image_base = includes["boxart"]["base_url"]["medium"].get<std::string>();
2017-10-23 07:35:47 -04:00
}
2019-01-06 13:42:20 +00:00
int count = 0;
for(auto game : games)
{
++count;
int game_id = game["id"].get<int>();
TheGamesDb::Game meta;
meta.id = game_id;
meta.overview = game["overview"].get<std::string>();
meta.discIds = game["serials"].get<std::vector<std::string>>();
meta.title = game["game_title"].get<std::string>();
meta.baseImgUrl = image_base;
if(!includes.empty())
{
auto boxarts = includes["boxart"]["data"];
if(!boxarts.empty())
{
auto str_id = std::to_string(game_id);
auto games_cover_meta = boxarts[str_id.c_str()];
if(!games_cover_meta.empty())
{
for(auto &game_cover : games_cover_meta)
{
meta.boxArtUrl = game_cover["filename"].get<std::string>().c_str();
if(game_cover["side"].get<std::string>() == "front")
{
break;
}
}
}
}
}
list.push_back(meta);
}
return count;
2017-10-23 07:35:47 -04:00
}