From 287821fdef8618cf68acee0c7f264e48009c21fa Mon Sep 17 00:00:00 2001 From: Sebastien MacDougall-Landry <60635017+EmperorPenguin18@users.noreply.github.com> Date: Mon, 4 Oct 2021 14:27:40 -0400 Subject: [PATCH] Update libscry.cc --- libscry.cc | 47 +++++++++++++++++++---------------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/libscry.cc b/libscry.cc index 40febae..94e621c 100644 --- a/libscry.cc +++ b/libscry.cc @@ -25,43 +25,34 @@ Scry::Scry() rc = sqlite3_open("/home/sebastien/test.db", &db); } -static string *DownloadedResponse; - -static size_t writer(char *data, size_t size, size_t nmemb, string *buffer_in) +static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) { - cout << "In writer callback" << endl; - - // Is there anything in the buffer? - if (buffer_in != NULL) - { - cout << "Buffer not null" << endl; - // Append the data to the buffer - buffer_in->append(data, size * nmemb); - cout <<" Buffer appended, seting response" << endl; - - DownloadedResponse = buffer_in; - cout << "Set downloadedResponse" << endl; - return size * nmemb; - } - - return 0; + ((string*)userp)->append((char*)contents, size * nmemb); + return size * nmemb; } Card Scry::cards_named(string search) { - curl_easy_setopt(easyhandle, CURLOPT_URL, "https://api.scryfall.com/cards/named?fuzzy=" + search); - curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, writer); - string *data; - curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, data); + string url = "https://api.scryfall.com/cards/named?fuzzy="; + url.append(search); + curl_easy_setopt(easyhandle, CURLOPT_URL, url.c_str()); + curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, WriteCallback); + string data; + curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, &data); + curl_easy_setopt(easyhandle, CURLOPT_FOLLOWLOCATION, 1); + curl_easy_setopt(easyhandle, CURLOPT_HTTPGET, 1); CURLcode success = curl_easy_perform(easyhandle); if (success != 0) { - printf("Error\n"); - exit(1); + printf("Errored with CURLcode %i\n", success); + exit(success); } - stringstream temp(&data); + Json::CharReaderBuilder reader; + reader["collectComments"] = false; + stringstream temp(data); Json::Value root; - temp >> root; - Card card(root["name"].asString()); + Json::String errs; + Json::parseFromStream(reader, temp, &root, &errs); + Card card(root.get("name", "UTF-32").asString()); return card; }