google books api

This commit is contained in:
frank 2021-11-02 13:59:07 -04:00
parent 4336f69d98
commit 392371d0c5
3 changed files with 76 additions and 20 deletions

View File

@ -47,7 +47,7 @@
"enabled": true,
"json-save": true,
"json-save-directory": "local/scans",
"barcode": "400063314395",
"barcode": "1250245567",
"capture-device": "/dev/video0"
},
"api":
@ -62,7 +62,9 @@
"nutronix-enabled": false,
"edamam-enabled": true,
"open-food-enabled": true,
"best-buy-enabled": true
"open-products-enabled": true,
"best-buy-enabled": true,
"google-books-enabled": true
},
"pudding":
{

View File

@ -313,7 +313,11 @@ void Pudding::add_item(const std::string& upc)
item.upc(upc);
if (get_configuration()["api"]["open-food-enabled"])
{
incorporate_open_food_api(item);
incorporate_open_api(item, OPEN_FOOD_API_URL);
}
if (get_configuration()["api"]["open-products-enabled"])
{
incorporate_open_api(item, OPEN_PRODUCTS_API_URL);
}
if (get_configuration()["api"]["nutronix-enabled"])
{
@ -327,6 +331,10 @@ void Pudding::add_item(const std::string& upc)
{
incorporate_best_buy_api(item);
}
if (get_configuration()["api"]["google-books-enabled"])
{
incorporate_google_books_api(item);
}
if (item.texture_count() > 0)
{
items.push_back(item);
@ -341,31 +349,41 @@ void Pudding::add_item(const std::string& upc)
}
}
/* Look for item upc in the Open Food API, and use the result to fill out item properties if found
*/
void Pudding::incorporate_open_food_api(Item& item)
/* Look for item upc in the Open Food/Products API and use the result to fill out item properties if found. */
void Pudding::incorporate_open_api(Item& item, const std::string& api_url)
{
sb::Log::log("checking Open Food API");
nlohmann::json json = json_from_url(OPEN_FOOD_API_URL + item.upc());
std::ostringstream checking_message;
checking_message << "checking " << api_url;
sb::Log::log(checking_message);
nlohmann::json json = json_from_url(api_url + item.upc());
/* test that should determine if an Open Food API response is not empty */
if (json.value("status", 0) && json.contains("product"))
{
if (json["product"].value("image_url", "") != "")
{
std::string url = json["product"]["image_url"];
sb::Texture texture = texture_from_image_url(url);
std::string image_url = json["product"]["image_url"];
sb::Texture texture = texture_from_image_url(image_url);
if (texture.generated())
{
item.texture(texture, url);
item.texture(texture, image_url);
}
}
item.brand_name(json["product"].value("brands", ""));
item.product_name(json["product"].value("product_name", ""));
save_item_json(json, item, "Open_Food_API");
if (api_url == OPEN_FOOD_API_URL)
{
save_item_json(json, item, "Open_Food_API");
}
else if (api_url == OPEN_PRODUCTS_API_URL)
{
save_item_json(json, item, "Open_Products_API");
}
}
else
{
sb::Log::log("no results from Open Food");
std::ostringstream results_message;
results_message << "no results from " << api_url;
sb::Log::log(results_message);
}
}
@ -473,6 +491,39 @@ void Pudding::incorporate_best_buy_api(Item& item)
}
}
/* Look for item upc in the Google Books API and use the result to fill out item properties if found. */
void Pudding::incorporate_google_books_api(Item& item)
{
sb::Log::log("checking Google Books API");
nlohmann::json json = json_from_url(GOOGLE_BOOKS_API_URL + item.upc());
/* test that should determine if a Google Books API response is not empty */
if (json.value<int>("totalItems", 0) > 0 && json.contains("items") && json["items"][0].contains("volumeInfo"))
{
/* book specific section of the JSON */
json = json["items"][0]["volumeInfo"];
/* get the image data */
if (json.contains("imageLinks") && json["imageLinks"].value("thumbnail", "") != "")
{
std::string image_url = json["imageLinks"]["thumbnail"];
sb::Texture texture = texture_from_image_url(image_url);
if (texture.generated())
{
item.texture(texture, image_url);
}
}
if (json.contains("authors"))
{
item.brand_name(json["authors"][0]);
}
item.product_name(json.value("title", ""));
save_item_json(json, item, "Google_Books_API");
}
else
{
sb::Log::log("no results from Google Books API");
}
}
/* Write submitted JSON to file, creating parent directories if necessary, and using item and
* api_name to determine file name prefix
*/
@ -730,17 +781,17 @@ void Pudding::update()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* switch to flat shader for background */
glUseProgram(flat_program);
/* flat shader uniforms for BG: time, texture ID, disabled HSV blend, scroll on */
glActiveTexture(GL_TEXTURE0);
glUniform1f(uniform["flat"]["time"], time_seconds);
glUniform1i(uniform["flat"]["texture"], 0);
glUniform3f(uniform["flat"]["blend"], 0.0f, 0.0f, 1.0f);
glUniform1i(uniform["flat"]["scroll"], true);
/* disable pudding attributes and enable background attributes */
pudding_model.disable();
background.enable();
glUniform1i(uniform["flat"]["texture"], 0);
glActiveTexture(GL_TEXTURE0);
background.current().bind();
/* set blend to modify white part of background, color passed is in HSV format */
glUniform3f(uniform["flat"]["blend"], 0.0f, 0.0f, 1.0f);
glUniform1i(uniform["flat"]["scroll"], true);
/* draws rectangle vertices and rectangle texture using UV coords */
/* draws bg vertices and texture */
glDrawArrays(GL_TRIANGLES, 0, background.attributes("position")->count());
glUniform1i(uniform["flat"]["scroll"], false);
/* draw pudding model using MVP shader */

View File

@ -66,11 +66,13 @@ private:
typedef Game super;
const std::string OPEN_FOOD_API_URL = "https://world.openfoodfacts.org/api/v0/product/";
const std::string OPEN_PRODUCTS_API_URL = "https://world.openproductsfacts.org/api/v0/product/";
const std::string NUTRONIX_API_URL = "https://trackapi.nutritionix.com/v2/search/item?upc=";
const std::string BARCODE_MONSTER_API_URL = "https://barcode.monster/api/";
const std::string BEST_BUY_API_URL_1 = "https://api.bestbuy.com/v1/products(upc=";
const std::string BEST_BUY_API_URL_2 = ")?format=json&apiKey=";
const std::string NUTRONIX_NOT_FOUND = "resource not found";
const std::string GOOGLE_BOOKS_API_URL = "https://www.googleapis.com/books/v1/volumes?q=isbn:";
const std::string GIANTBOMB_API_URL = "https://www.giantbomb.com/api/release/?api_key=";
const glm::vec3 ZERO_VECTOR_3D = glm::vec3(0, 0, 0);
const glm::vec3 Y_UNIT_NORMAL_3D = glm::vec3(0, 1, 0);
@ -99,10 +101,11 @@ private:
void load_gl_context();
void load_tiles();
void initialize_camera();
void incorporate_open_food_api(Item&);
void incorporate_open_api(Item&, const std::string&);
void incorporate_nutronix_api(Item&);
void incorporate_edamam_api(Item&);
void incorporate_best_buy_api(Item&);
void incorporate_google_books_api(Item&);
void save_item_json(const nlohmann::json&, const Item&, const std::string&) const;
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>& = {}) const;