added edamam api

This commit is contained in:
frank 2021-05-11 23:51:33 -04:00
parent edc6d8946d
commit a9c8d053d6
2 changed files with 54 additions and 15 deletions

View File

@ -1,14 +1,17 @@
/*
______________
//````````````\\ 😀 Thank you for choosing Pudding Customs for your business 😀
//~~~~~~~~~~~~~~\\
//================\\ Custom pudding code provided by 0eggxactly ........... [http://0eggxactly.itch.io]
// \\ Available for copy, modification and redistribution .. [http://git.shampoo.ooo/pudding]
// ''CUSTOM PUDDING'' \\ Updates .............................................. [http://twitter.com/diskmem]
//______________________\\
``````````````````````````
*/
* ______________
* //````````````\\ 😀 Thank you for choosing Pudding Customs for your business 😀
* //~~~~~~~~~~~~~~\\
* //================\\ Custom pudding code provided by 0eggxactly ........... [http://0eggxactly.itch.io]
* // \\ Available for copy, modification and redistribution .. [http://git.shampoo.ooo/pudding]
* // ''CUSTOM PUDDING'' \\ Updates .............................................. [http://twitter.com/diskmem]
* //______________________\\
* ``````````````````````````
*
* Use this application to generate a custom pudding from a series of UPC scans, helping a pair of rats leverage their
* extraterrestrial trash symbiosis, using it to take over the video game industry with performance enhancing drugged
* puddings that enable business professionals to predict the stock market with supernatural accuracy.
*/
#include "Pudding.hpp"
@ -30,12 +33,17 @@ void Pudding::load_sdl_context()
super::load_sdl_context();
}
void Pudding::add_item(const std::string& barcode)
/* Build an Item object by submitting upc parameter to various APIs and incorporating
* relevant results from each. Result JSON will be saved if saving is enabled in the global
* configuration.
*/
void Pudding::add_item(const std::string& upc)
{
Item item;
item.set_upc(barcode);
item.set_upc(upc);
incorporate_open_food_api(item);
incorporate_nutronix_api(item);
incorporate_edamam_api(item);
items.push_back(item);
}
@ -102,6 +110,35 @@ void Pudding::incorporate_nutronix_api(Item& item)
}
}
/* Submit a query to Edamam API and insert relevant results into supplied Item object
*/
void Pudding::incorporate_edamam_api(Item& item)
{
SDL_Log("checking Edamam API");
// build API url by concatenating relevant values into query string
std::stringstream url;
url << "https://api.edamam.com/api/food-database/v2/parser?upc=" << item.get_upc() << "&app_id=" <<
get_configuration()["api"]["edamam-app-id"].get<std::string>() << "&app_key=" <<
get_configuration()["api"]["edamam-app-key"].get<std::string>();
nlohmann::json json = json_from_url(url.str());
// test that should determine if a Edamam response has food data
if (json.contains("hints") && json["hints"][0].contains("food"))
{
nlohmann::json food = json["hints"][0]["food"];
if (food.value("image", "") != "")
{
std::string url = food["image"];
SDL_Texture* texture = texture_from_image_url(url);
if (texture != nullptr)
{
item.add_image_texture(texture);
}
item.set_product_name(food.value("label", ""));
}
save_item_json(json, item, "Edamam_API");
}
}
/* Write submitted JSON to file, creating parent directories if necessary, and using item and
* api_name to determine file name prefix
*/
@ -205,10 +242,12 @@ SDL_Texture* Pudding::texture_from_image_url(const std::string& url)
if (!storage.empty())
{
SDL_RWops* rw = SDL_RWFromConstMem(storage.data(), storage.size());
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "received image data");
return IMG_LoadTexture_RW(get_renderer(), rw, 0);
}
else
{
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "image url returned no data");
return nullptr;
}
}
@ -226,7 +265,7 @@ void Pudding::update()
{
if (items.front().get_image_textures().size() > 0)
{
SDL_RenderCopyF(get_renderer(), items.front().get_image_textures().front().get(), nullptr, nullptr);
SDL_RenderCopyF(get_renderer(), items.front().get_image_textures().back().get(), nullptr, nullptr);
}
}
}

View File

@ -20,13 +20,12 @@ struct Pudding : Game
const std::string OPEN_FOOD_API_URL = "https://world.openfoodfacts.org/api/v0/product/";
const std::string NUTRONIX_API_URL = "https://trackapi.nutritionix.com/v2/search/item?upc=";
const std::string EDAMAM_API_URL = "https://api.edamam.com/api/food-database/v2/parser?upc=&app_id=&app_key=";
const std::string BARCODE_MONSTER_API_URL = "https://barcode.monster/api/";
const std::string BEST_BUY_API_URL = "https://api.bestbuy.com/v1/products(upc=)?format=json&apiKey=";
const std::string NUTRONIX_NOT_FOUND = "resource not found";
/*
* images, ingredients, protein weight, nutrition grade, popularity, "serving unit", item name, brand name, keywords,
* ingredients, protein weight, nutrition grade, popularity, "serving unit", keywords,
* allergens, calories, fat, saturated fat, cholesterol, sodium, carbohydrates, fiber, sugar, potassium
*/
@ -39,6 +38,7 @@ struct Pudding : Game
void add_item(const std::string&);
void incorporate_open_food_api(Item&);
void incorporate_nutronix_api(Item&);
void incorporate_edamam_api(Item&);
void save_item_json(const nlohmann::json&, const Item&, const std::string&);
nlohmann::json json_from_url(const std::string&, const std::vector<std::string>& = {});
void curl_get_bytes(const std::string& url, std::vector<std::uint8_t>&, const std::vector<std::string>& = {});