Play-/Source/ui_shared/TheGamesDbClient.cpp

169 lines
4.4 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>
2017-10-23 07:35:47 -04:00
#include "TheGamesDbClient.h"
#include "string_format.h"
#include "http/HttpClientFactory.h"
using namespace TheGamesDb;
2019-01-28 20:10:42 -05:00
#define API_KEY "API_KEY"
static const char* g_getGameUrl = "https://api.thegamesdb.net/Games/GamesByGameID?apikey=" API_KEY "&fields=overview,uids&include=boxart&id=%d";
static const char* g_getGamesByUIDUrl = "https://api.thegamesdb.net/Games/ByGameUniqueID?apikey=" API_KEY "&filter%5Bplatform%5D=11&fields=overview,uids&include=boxart";
static const char* g_getGamesListUrl = "https://api.thegamesdb.net/v1.1/Games/ByGameName?apikey=" API_KEY "&fields=overview,uids&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_getGamesByUIDUrl);
url += "&uid=";
2019-01-06 13:42:20 +00:00
url += str_games_id;
while(!url.empty())
2019-01-06 13:42:20 +00:00
{
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);
auto games = PopulateGameList(parsed_json);
gamesList.insert(gamesList.end(), games.begin(), games.end());
2019-01-28 20:14:53 -05:00
if(!parsed_json["pages"]["next"].empty())
{
url = parsed_json["pages"]["next"].get<std::string>();
}
}
2019-01-06 13:42:20 +00:00
}
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();
auto parsed_json = nlohmann::json::parse(json_ret);
2017-10-23 07:35:47 -04:00
auto gamesList = PopulateGameList(parsed_json);
if(gamesList.empty())
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 12:27:31 +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();
auto parsed_json = nlohmann::json::parse(json_ret);
2017-10-23 07:35:47 -04:00
auto gamesList = PopulateGameList(parsed_json);
if(gamesList.empty())
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;
}
GamesList CClient::PopulateGameList(const nlohmann::json& parsed_json)
2019-01-06 13:42:20 +00:00
{
GamesList list;
2019-01-06 13:42:20 +00:00
if(parsed_json["data"]["count"].get<int>() == 0)
{
return list;
}
2019-01-06 13:42:20 +00:00
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
for(auto game : games)
{
int game_id = game["id"].get<int>();
TheGamesDb::Game meta;
meta.id = game_id;
meta.overview = game["overview"].get<std::string>();
if(!game["uids"].empty())
{
auto uids = game["uids"].get<std::vector<nlohmann::json>>();
for(auto item : uids)
{
meta.discIds.push_back(item["uid"].get<std::string>());
}
}
2019-01-06 13:42:20 +00:00
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())
{
2019-01-06 12:27:31 +00:00
for(auto& game_cover : games_cover_meta)
2019-01-06 13:42:20 +00:00
{
meta.boxArtUrl = game_cover["filename"].get<std::string>().c_str();
if(game_cover["side"].get<std::string>() == "front")
{
break;
}
}
}
}
}
list.push_back(meta);
}
return list;
2017-10-23 07:35:47 -04:00
}