FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
serpapi-cpp/src/serpapi.cpp at main · serpapi/serpapi-cpp · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
serpapi
/
serpapi-cpp
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
3
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
serpapi-cpp
/
src
/
serpapi.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
209 lines (181 loc) · 7.2 KB
Breadcrumbs
serpapi-cpp
/
src
/
serpapi.cpp
Copy path
File metadata and controls
209 lines (181 loc) · 7.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
**
* Scrape search powered by SerpApi
*/
#
include
"
callback.hpp
"
#
include
<
serpapi.hpp
>
#
include
<
mutex
>
#
include
<
sstream
>
namespace
serpapi
{
const
static
std::string
HOST
=
"
https://serpapi.com
"
;
const
static
std::string
NAME
=
"
serpapi-cpp
"
;
const
static
std::string
VERSION
=
"
0.5.0
"
;
static
std::once_flag curl_init_flag;
/*
*
* @brief Constructs a client with default parameters merged into every request.
* @param parameter Default parameters (e.g. api_key, engine) applied to all calls.
*/
Client::Client
(
const
std::map<std::string, std::string> ¶meter) {
this
->
parameter
= parameter;
}
Client::~Client
() {}
/*
*
* @brief Runs a search and returns the raw search engine result page as HTML.
* @param parameter Search parameters (e.g. q, location) merged with the client defaults.
* @return Raw HTML response body.
*/
std::string
Client::html
(
const
std::map<std::string, std::string> ¶meter) {
GetResponse gr =
Client::get
(
"
/search
"
,
"
html
"
, parameter);
return
gr.
payload
;
}
/*
*
* @brief Runs a search and returns the raw search engine result page as Markdown.
* @param parameter Search parameters (e.g. q, location) merged with the client defaults.
* @return Raw Markdown response body.
*/
std::string
Client::markdown
(
const
std::map<std::string, std::string> ¶meter) {
GetResponse gr =
Client::get
(
"
/search
"
,
"
md
"
, parameter);
return
gr.
payload
;
}
/*
*
* @brief Runs a search and returns the parsed/structured results as JSON.
* @param parameter Search parameters (e.g. q, location) merged with the client defaults.
* @return Parsed JSON response as a rapidjson::Document.
*/
rapidjson::Document
Client::search
(
const
std::map<std::string, std::string> ¶meter) {
return
Client::json
(
"
/search
"
, parameter);
}
/*
*
* @brief Retrieves a previously run search from the search archive.
* @param id Search id, as returned in search_metadata.id from a prior search().
* @return Parsed JSON response as a rapidjson::Document.
*/
rapidjson::Document
Client::search_archive
(
const
std::string &id) {
return
Client::json
(
"
/searches/
"
+ id +
"
.json
"
, std::map<std::string, std::string>());
}
/*
*
* @brief Retrieves account information (e.g. plan, usage) for the given api_key.
* @param parameter Request parameters (e.g. api_key) merged with the client defaults.
* @return Parsed JSON response as a rapidjson::Document.
*/
rapidjson::Document
Client::account
(
const
std::map<std::string, std::string> ¶meter) {
return
Client::json
(
"
/account.json
"
, parameter);
}
/*
*
* @brief Looks up supported locations matching a query.
* @param parameter Request parameters (e.g. q, limit) merged with the client defaults.
* @return Parsed JSON response as a rapidjson::Document.
*/
rapidjson::Document
Client::location
(
const
std::map<std::string, std::string> ¶meter) {
return
Client::json
(
"
/locations.json
"
, parameter);
}
/*
*
* @brief Fetches a JSON endpoint and parses it into a rapidjson::Document.
* @param uri Endpoint path, relative to HOST, to request with output=json.
* @param parameter Request parameters merged with the client defaults.
* @return Parsed JSON response, or a Document with an "error" member if the
* payload could not be parsed.
*/
rapidjson::Document
Client::json
(
const
std::string &uri,
const
std::map<std::string, std::string> ¶meter) {
GetResponse gr =
get
(uri,
"
json
"
, parameter);
rapidjson::Document d;
d.
Parse
(gr.
payload
.
c_str
());
if
(d.
HasParseError
()) {
d.
SetObject
();
d.
AddMember
(
"
error
"
,
"
JSON parse error
"
, d.
GetAllocator
());
}
return
d;
}
/*
*
* @brief URL-encodes and joins a parameter map into a query string.
* @param curl Initialized CURL handle used to escape keys and values.
* @param parameter Parameters to encode.
* @return "key=value&key=value" encoded string, or "" if escaping any entry failed.
*/
std::string
encodeUrl
(
CURL
*curl,
const
std::map<std::string, std::string> ¶meter) {
std::ostringstream oss;
bool
first =
true
;
for
(
const
auto
& entry : parameter) {
char
*escaped_key =
curl_easy_escape
(curl, entry.
first
.
c_str
(), entry.
first
.
length
());
char
*escaped_value =
curl_easy_escape
(curl, entry.
second
.
c_str
(), entry.
second
.
length
());
if
(!escaped_key || !escaped_value) {
if
(escaped_key)
curl_free
(escaped_key);
if
(escaped_value)
curl_free
(escaped_value);
return
"
"
;
}
if
(!first) oss <<
"
&
"
;
oss << escaped_key <<
"
=
"
<< escaped_value;
first =
false
;
curl_free
(escaped_key);
curl_free
(escaped_value);
}
return
oss.
str
();
}
/*
*
* @brief Builds the full query string for a request.
*
* Concatenates call-specific parameters, then the client's default
* parameters, then the output format and source tag.
*
* @param curl Initialized CURL handle used to escape parameters.
* @param output Desired response format (e.g. "json", "html", "md").
* @param parameter Call-specific parameters, merged with the client defaults.
* @return Fully encoded query string, without the leading '?'.
*/
std::string
Client::url
(
CURL
*curl,
const
std::string &output,
const
std::map<std::string, std::string> ¶meter) {
std::string url_str =
encodeUrl
(curl, parameter);
if
(
this
->
parameter
.
size
() >
0
) {
if
(!url_str.
empty
()) url_str +=
"
&
"
;
url_str +=
encodeUrl
(curl,
this
->
parameter
);
}
url_str +=
"
&output=
"
+ output;
url_str +=
"
&source=
"
+
NAME
+
"
:
"
+
VERSION
;
return
url_str;
}
/*
*
* @brief Performs the HTTP GET request against SerpApi.
* @param uri Endpoint path, relative to HOST (e.g. "/search").
* @param output Desired response format (e.g. "json", "html", "md").
* @param parameter Call-specific parameters, merged with the client defaults.
* @return HTTP status code and raw response body. httpCode is 0 on a
* client-side failure (CURL init or transport error), with the
* error message in payload.
*/
GetResponse
Client::get
(
const
std::string &uri,
const
std::string &output,
const
std::map<std::string, std::string> ¶meter) {
std::call_once
(curl_init_flag, []() {
curl_global_init
(
CURL_GLOBAL_DEFAULT
); });
CURL
*curl =
curl_easy_init
();
if
(!curl) {
GetResponse gr;
gr.
httpCode
=
0
;
gr.
payload
=
"
CURL initialization failed
"
;
return
gr;
}
const
std::string url_params =
this
->
url
(curl, output, parameter);
const
std::string full_url =
HOST
+ uri +
"
?
"
+ url_params;
curl_easy_setopt
(curl,
CURLOPT_URL
, full_url.
c_str
());
curl_easy_setopt
(curl,
CURLOPT_IPRESOLVE
,
CURL_IPRESOLVE_V4
);
curl_easy_setopt
(curl,
CURLOPT_TIMEOUT
, (
long
)timeout);
curl_easy_setopt
(curl,
CURLOPT_FOLLOWLOCATION
,
1L
);
curl_easy_setopt
(curl,
CURLOPT_SSL_VERIFYHOST
,
2L
);
curl_easy_setopt
(curl,
CURLOPT_SSL_VERIFYPEER
,
1L
);
long
httpCode
(
0
);
std::string httpData;
curl_easy_setopt
(curl,
CURLOPT_WRITEFUNCTION
, callback);
curl_easy_setopt
(curl,
CURLOPT_WRITEDATA
, &httpData);
CURLcode res =
curl_easy_perform
(curl);
if
(res !=
CURLE_OK
) {
curl_easy_cleanup
(curl);
GetResponse gr;
gr.
httpCode
=
0
;
gr.
payload
=
std::string
(
"
CURL error:
"
) +
curl_easy_strerror
(res);
return
gr;
}
curl_easy_getinfo
(curl,
CURLINFO_RESPONSE_CODE
, &httpCode);
curl_easy_cleanup
(curl);
GetResponse gr;
gr.
httpCode
= httpCode;
gr.
payload
= httpData;
return
gr;
}
}
//
namespace serpapi
Back
|
FazBrowse Home
|
New Git URL