Added item definition to generate_game_infos

Thx psychonic.
This commit is contained in:
Nemirtingas 2019-08-27 00:17:57 +02:00
parent 519abbdd96
commit da75aa3ae7

View file

@ -164,11 +164,202 @@ public:
})
}
*/
// ---------------------------------
// -- Special thanks to psychonic --
// ---------------------------------
// Get game items definition digest (Phase1): https://api.steampowered.com/IInventoryService/GetItemDefMeta/v1?key=<webapi_key>&appid=218620
/*
{
"response": {
"modified": 1566848385,
"digest": "3CDFC1CC1AC2B0D55D12C1C130F4294BDD6DF8D0"
}
}
*/
// Get game items definition: https://api.steampowered.com/IGameInventory/GetItemDefArchive/v0001?appid=218620&digest=<digest>
/*
[
{
"appid":"218620",
"itemdefid":"0",
"Timestamp":"2016-04-08T18:00:21.3643085Z",
"modified":"20160408T180021Z",
"date_created":"20160408T180021Z",
"type":"",
"display_type":"",
"name":"",
"quantity":0,
"description":"",
"tradable":false,
"marketable":false,
"commodity":false,
"drop_interval":0,
"drop_max_per_window":0,
"workshopid":"0"
},
{
"appid":"218620",
"itemdefid":"50002",
"Timestamp":"2015-11-13T16:01:18.0338618Z",
"modified":"20151113T160117Z",
"date_created":"20151113T160117Z",
"type":"item",
"display_type":"",
"name":"Sputnik Safe",
"quantity":0,
"description":"[color=#2360D8]THE JUDGE SHOTGUN | Pixel [/color]\n[color=#2360D8]KOBUS 90 SUBMACHINE GUN | Red Stars[/color]\n[color=#2360D8]PLAINSRIDER BOW | Arctic Plains[/color]\n[color=#2360D8]GRUBER KURZ PISTOL | Little Leopard[/color]\n[color=#2360D8]HRL-7 ROCKET LAUNCHER | Headline[/color]\n[color=#2360D8]LOCOMOTIVE 12G SHOTGUN | Cosmonaut[/color]\n[color=#9900FF]FLAMETHROWER | St. Basil[/color]\n[color=#9900FF]JP36 RIFLE | Ice Leopard [/color]\n[color=#9900FF]CAR-4 RIFLE | Stripe On[/color]\n[color=#9900FF]BRONCO .44 REVOLVER | Black Bull[/color]\n[color=#FF00FF]BERNETTI 9 PISTOL | Angry Bear[/color]\n[color=#FF00FF]THANATOS .50 CAL SNIPER RIFLE | Matrjoschka[/color]\n[color=#FF00FF]M308 RIFLE | Helmet Space Program[/color]\n[color=#FF0000]CLARION RIFLE | Breaching Owl[/color]\n[color=#FF0000]MOSCONI 12G SHOTGUN | Bullet Bear Gun[/color]\n[color=#FFAA00]or an exceedingly rare special item![/color]",
"icon_url":"http://media.overkillsoftware.com/economy42gF2Y/safes_weapon_01.png",
"icon_url_large":"http://media.overkillsoftware.com/economy42gF2Y/safes_weapon_01.png",
"store_tags":"safe;sputnik safe;",
"tradable":true,
"marketable":true,
"commodity":false,
"drop_interval":0,
"drop_max_per_window":0,
"workshopid":"0",
"dsl_bonus":"false",
"item_name":"weapon_01",
"item_slot":"safes"
}
*/
#ifdef max
#undef max
#endif
std::string steam_apikey;
std::string app_id;
void generate_achievements(CurlEasy &easy)
{
std::string url = "http://api.steampowered.com/ISteamUserStats/GetSchemaForGame/v2/?key=";
url += steam_apikey;
url += "&appid=";
url += app_id;
easy.set_url(url);
easy.perform();
try
{
std::ofstream ach_file("achievements.json", std::ios::trunc | std::ios::out);
nlohmann::json json = nlohmann::json::parse(easy.get_answer());
nlohmann::json output_json = nlohmann::json::array();
bool first = true;
int i = 0;
for (auto& item : json["game"]["availableGameStats"]["achievements"].items())
{
output_json[i]["name"] = item.value()["name"];
output_json[i]["displayName"] = item.value()["displayName"];
output_json[i]["hidden"] = std::to_string(item.value()["hidden"].get<int>());
try
{
output_json[i]["description"] = item.value()["description"];
}
catch (...)
{
output_json[i]["description"] = "";
}
output_json[i]["icon"] = item.value()["icon"];
output_json[i]["icongray"] = item.value()["icongray"];
++i;
}
ach_file << std::setw(2) << output_json;
}
catch (std::exception& e)
{
std::cerr << "Failed to get infos: ";
long code;
if (easy.get_html_code(code) == CURLE_OK && code == 403)
{
std::cerr << "Error in webapi key";
}
else
{
std::cerr << "Error while parsing json. Try to go at " << url << " and see what you can do to build your achivements.json";
}
std::cerr << std::endl;
}
}
void generate_items(CurlEasy& easy)
{
std::string url = "https://api.steampowered.com/IInventoryService/GetItemDefMeta/v1?key=";
url += steam_apikey;
url += "&appid=";
url += app_id;
easy.set_url(url);
easy.perform();
try
{
nlohmann::json json = nlohmann::json::parse(easy.get_answer());
std::string digest = json["response"]["digest"];
url = "https://api.steampowered.com/IGameInventory/GetItemDefArchive/v0001?appid=";
url += app_id;
url += "&digest=";
url += digest;
easy.set_url(url);
easy.perform();
nlohmann::json item_json = nlohmann::json::object();
json = nlohmann::json::parse(easy.get_answer());
std::ofstream items_file("items.json", std::ios::trunc | std::ios::out);
for (auto &i : json)
{
for (auto j = i.begin(); j != i.end(); ++j)
{
//if (j.key() == "itemdefid")
//{
// j.value() = std::stoll(j.value().get<std::string>());
//}
//else
{
nlohmann::json& v = j.value();
switch (v.type())
{
case nlohmann::json::value_t::boolean:
v = (v.get<bool>() ? "true" : "false");
break;
case nlohmann::json::value_t::number_float:
v = std::to_string(v.get<double>());
break;
case nlohmann::json::value_t::number_integer:
v = std::to_string(v.get<int64_t>());
break;
case nlohmann::json::value_t::number_unsigned:
v = std::to_string(v.get<uint64_t>());
break;
}
}
}
item_json[i["itemdefid"].get<std::string>()] = i;
}
items_file << std::setw(2) << item_json;
}
catch (std::exception& e)
{
std::cerr << "Failed to get infos: ";
long code;
if (easy.get_html_code(code) == CURLE_OK && code == 403)
{
std::cerr << "Error in webapi key";
}
else
{
std::cerr << "Error while parsing json. Try to go at " << url << " and see what you can do to build your achivements.json";
}
std::cerr << std::endl;
}
}
int main()
{
CurlGlobal& cglobal = CurlGlobal::Inst();
@ -177,10 +368,6 @@ int main()
CurlEasy easy;
if (easy.init())
{
std::string url;
std::string steam_apikey;
std::string app_id;
std::cout << "Enter the game appid: ";
std::cin >> app_id;
std::cout << "Enter your webapi key: ";
@ -188,53 +375,8 @@ int main()
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin >> steam_apikey;
url = "http://api.steampowered.com/ISteamUserStats/GetSchemaForGame/v2/?key=";
url += steam_apikey;
url += "&appid=";
url += app_id;
easy.set_url(url);
easy.perform();
try
{
std::ofstream ach_file("achievements.json", std::ios::trunc | std::ios::out);
nlohmann::json json = nlohmann::json::parse(easy.get_answer());
nlohmann::json output_json = nlohmann::json::array();
bool first = true;
int i = 0;
for (auto& item : json["game"]["availableGameStats"]["achievements"].items())
{
output_json[i]["name"] = item.value()["name"];
output_json[i]["displayName"] = item.value()["displayName"];
output_json[i]["hidden"] = std::to_string(item.value()["hidden"].get<int>());
try
{
output_json[i]["description"] = item.value()["description"];
}
catch (...)
{
output_json[i]["description"] = "";
}
output_json[i]["icon"] = item.value()["icon"];
output_json[i]["icongray"] = item.value()["icongray"];
++i;
}
ach_file << std::setw(2) << output_json;
}
catch (std::exception& e)
{
std::cerr << "Failed to get infos: ";
long code;
if (easy.get_html_code(code) == CURLE_OK && code == 403)
{
std::cerr << "Error in webapi key";
}
else
{
std::cerr << "Error while parsing json. Try to go at " << url << " and see what you can do to build your achivements.json";
}
std::cerr << std::endl;
}
generate_achievements(easy);
generate_items(easy);
}
}