-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_example.cpp
More file actions
48 lines (38 loc) · 1.4 KB
/
google_example.cpp
File metadata and controls
48 lines (38 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <map>
#include <rapidjson/document.h>
#include <rapidjson/prettywriter.h>
#include <rapidjson/stringbuffer.h>
#include <serpapi.hpp>
#include <string>
int main() {
const char *env_p = std::getenv("SERPAPI_KEY");
if (env_p == nullptr) {
std::cout << "SERPAPI_KEY not set, skipping example" << std::endl;
return 0;
}
std::string apiKey(env_p);
std::map<std::string, std::string> default_parameter;
default_parameter["api_key"] = apiKey;
default_parameter["engine"] = "google";
serpapi::Client client(default_parameter);
std::map<std::string, std::string> parameter;
parameter["q"] = "coffee";
parameter["location"] = "Austin,TX";
rapidjson::Document d = client.search(parameter);
rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
d.Accept(writer);
std::cout << "Search Results:" << std::endl;
std::cout << buffer.GetString() << std::endl;
if (d.HasMember("search_metadata") && d["search_metadata"].HasMember("id")) {
std::string id = d["search_metadata"]["id"].GetString();
std::cout << "\nArchiving search ID: " << id << std::endl;
rapidjson::Document archived = client.search_archive(id);
if (archived.HasMember("search_metadata") &&
archived["search_metadata"]["status"] == "Success") {
std::cout << "Successfully retrieved from archive." << std::endl;
}
}
return 0;
}