Nimbin2git.christianimmanuel.de / Games / dvd / upcitemdb.c

dvd files

last change 2026-09-04 (2 hours ago)

wget https://git.christianimmanuel.de/games/dvd/archive/dvd.tar.gz
upcitemdb.c 5.2 KB · 149 lines raw
#include <json-c/json.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

#include "upcitemdb.h"

// ANSI color codes
#define RESET   "\033[0m"
#define RED     "\033[31m"
#define GREEN   "\033[32m"
#define YELLOW  "\033[33m"
#define BLUE    "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN    "\033[36m"
#define WHITE   "\033[37m"



// Define a struct to hold the parsed JSON response

// Function to parse the JSON response and populate the struct
Websearch_st parse_json_response(const char *response) {
    Websearch_st result = { "", 0.0, 0.0, "" };

    struct json_object *parsed_json = json_tokener_parse(response);
    struct json_object *items, *item, *title, *lowest_price, *highest_price, *category;

    if (json_object_object_get_ex(parsed_json, "items", &items)) {
        item = json_object_array_get_idx(items, 0);

        if (json_object_object_get_ex(item, "title", &title)) {
            strncpy(result.title, json_object_get_string(title), sizeof(result.title) - 1);
            if (strlen(result.title) == 0)
                return result;
        }

        if (json_object_object_get_ex(item, "lowest_recorded_price", &lowest_price)) {
            result.lowest_recorded_price = json_object_get_double(lowest_price);
        }

        if (json_object_object_get_ex(item, "highest_recorded_price", &highest_price)) {
            result.highest_recorded_price = json_object_get_double(highest_price);
        }

        if (json_object_object_get_ex(item, "category", &category)) {
            strncpy(result.category, json_object_get_string(category), sizeof(result.category) - 1);
        }
    }

    json_object_put(parsed_json);  // Free the JSON object
    return result;
}

// Function to print the contents of the Websearch_st struct
void print_json_response(const Websearch_st *response) {
    printf(MAGENTA "\n┌────────────────────────────────────────\n" RESET);
    printf(MAGENTA "│ " CYAN "Web search for barcode\n" RESET);
    printf(MAGENTA "│ " WHITE "%24s\n" RESET, response->title);
    printf(MAGENTA "├────────────────────────────────────────\n" RESET);
    printf(MAGENTA "│ " RESET "Lowest Recorded Price: %.2f\n" RESET, response->lowest_recorded_price);
    printf(MAGENTA "│ " RESET "Highest Recorded Price: %.2f\n" RESET, response->highest_recorded_price);
    printf(MAGENTA "│ " RESET "Category: %s\n" RESET, response->category);
    printf(MAGENTA "└────────────────────────────────────────\n" RESET);
}

// Function to handle the data received from the curl request
size_t write_callback(void *ptr, size_t size, size_t nmemb, char *data) {
    strcat(data, ptr);
    return size * nmemb;
}

// Function to execute the curl request and return the response as a dynamically allocated string
char* fetch_json_response(const char *url) {
    CURL *curl;
    CURLcode res;
    
    char *response = malloc(2048);  // Allocate memory for the response
    if (!response) {
        fprintf(stderr, "Memory allocation failed\n");
        return NULL;
    }

    response[0] = '\0';  // Initialize the response buffer to an empty string

    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();

    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);

        res = curl_easy_perform(curl);

        if (res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
            free(response);  // Free memory if the request fails
            curl_easy_cleanup(curl);
            return NULL;
        }

        curl_easy_cleanup(curl);
    }

    curl_global_cleanup();
    return response;  // Return the dynamically allocated response
}

// Function to read barcode, fetch data, and parse into a Websearch_st struct
Websearch_st readUpcitemdb(char* barcode) {
    if (!barcode) {
        static char barcode_input[256];
        barcode = barcode_input;
        printf("Enter barcode:\n");
        if (fgets(barcode, sizeof(barcode_input), stdin)) {
            barcode[strcspn(barcode, "\n")] = '\0';  // Remove trailing newline
        }
    }

    // Construct the URL
    char url[512];
    snprintf(url, sizeof(url), "https://api.upcitemdb.com/prod/trial/lookup?upc=%s", barcode);
    printf("Fetching data from URL: %s\n", url);

    // Fetch JSON response
    char *response = fetch_json_response(url);
    if (response) {
        // printf("Raw JSON response: %s\n", response);
        Websearch_st r = parse_json_response(response);
        printf("Search done: %s\n", r.title);
        return r;
    } else {
        printf("Failed to fetch the response.\n");
        return (Websearch_st) { "", 0.0, 0.0, "" };  // Return empty struct
    }
}

int test_upcitemdb() {
    // Read data into a Websearch_st struct
    Websearch_st data_web = readUpcitemdb("");

    // Print the data in the struct
    print_json_response(&data_web);

    return 0;
}