|
| 1 | +#include <cstdint> |
| 2 | +#include <iostream> |
| 3 | +#include <memory> |
| 4 | +#include <string> |
| 5 | +#include <curl/curl.h> |
| 6 | +#include <serpapi.hpp> |
| 7 | + |
| 8 | +#include "rapidjson/document.h" |
| 9 | +#include "rapidjson/writer.h" |
| 10 | +#include "rapidjson/stringbuffer.h" |
| 11 | +#include "rapidjson/prettywriter.h" |
| 12 | +#include "rapidjson/pointer.h" |
| 13 | + |
| 14 | +using namespace rapidjson; |
| 15 | +using namespace std; |
| 16 | + |
| 17 | +void info(string msg) { |
| 18 | + cout << "\nINFO: " << msg << endl; |
| 19 | +} |
| 20 | +void info(double msg) { |
| 21 | + cout << "\nINFO: " << msg << endl; |
| 22 | +} |
| 23 | + |
| 24 | +void info(const Document& document) { |
| 25 | + StringBuffer buffer; |
| 26 | + PrettyWriter<StringBuffer> writer(buffer); |
| 27 | + document.Accept(writer); |
| 28 | + cout << "\nINFO: " << buffer.GetString(); |
| 29 | +} |
| 30 | + |
| 31 | +// RapidJSON parser documentation is available: https://rapidjson.org |
| 32 | +int main() |
| 33 | +{ |
| 34 | + // initialize a client |
| 35 | + const char* env_p = std::getenv("API_KEY"); |
| 36 | + std::string apiKey(env_p); |
| 37 | + std::map<string, string> default_parameter; |
| 38 | + default_parameter["api_key"] = apiKey; |
| 39 | + default_parameter["engine"] = "google"; |
| 40 | + |
| 41 | + // using namespace serpapi; |
| 42 | + serpapi::Client client(default_parameter); |
| 43 | + |
| 44 | + // execute search |
| 45 | + std::map<string, string> parameter; |
| 46 | + parameter["q"] = "coffee"; |
| 47 | + parameter["location"] = "Austin,TX"; |
| 48 | + // using namespace rapidjson; |
| 49 | + rapidjson::Document d = client.search(parameter); |
| 50 | + info("document loaded"); |
| 51 | + assert(d.HasMember("search_metadata")); |
| 52 | + assert(d["search_metadata"]["status"] == "Success"); |
| 53 | + assert(d["search_metadata"]["id"].IsString()); |
| 54 | + |
| 55 | + string id = d["search_metadata"]["id"].GetString(); |
| 56 | + client.searchArchive(id); |
| 57 | + assert(d["search_metadata"]["status"] == "Success"); |
| 58 | + info(d); |
| 59 | +} |
0 commit comments