FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
serpapi-java/src/main/java/serpapi/SerpApi.java at master · serpapi/serpapi-java · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
serpapi
/
serpapi-java
Public
Notifications
You must be signed in to change notification settings
Fork
3
Star
9
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-java
/
src
/
main
/
java
/
serpapi
/
SerpApi.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
209 lines (183 loc) · 6.23 KB
Breadcrumbs
serpapi-java
/
src
/
main
/
java
/
serpapi
/
SerpApi.java
Copy path
File metadata and controls
209 lines (183 loc) · 6.23 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
package
serpapi
;
import
com
.
google
.
gson
.
Gson
;
import
com
.
google
.
gson
.
JsonArray
;
import
com
.
google
.
gson
.
JsonElement
;
import
com
.
google
.
gson
.
JsonObject
;
import
java
.
util
.
Map
;
import
java
.
util
.
HashMap
;
/**
* SerpApi wraps HTTP interaction with the service serpapi.com
*/
public
class
SerpApi
{
/**
* client parameters
*/
public
Map
<
String
,
String
>
parameter
;
// initialize gson
private
static
Gson
gson
=
new
Gson
();
/**
* https client implementation for Java 7+
*/
public
SerpApiHttp
client
;
/**
* default HTTP client timeout
*
* Applied to both connecting and reading. Some engines, home_depot in
* particular, regularly take longer than a minute to respond.
*/
public
Integer
timeout
=
120000
;
/***
* Constructor
*
* @param parameter default search parameter should include {"api_key": "secret_api_key", "engine": "google" }
*/
public
SerpApi
(
Map
<
String
,
String
>
parameter
) {
this
.
parameter
=
parameter
;
this
.
client
=
new
SerpApiHttp
(
"/search"
);
this
.
client
.
setHttpConnectionTimeout
(
this
.
timeout
);
this
.
client
.
setHttpReadTimeout
(
this
.
timeout
);
}
/***
* Constructor without arguments
*/
public
SerpApi
() {
this
.
parameter
=
new
HashMap
<>();
this
.
client
=
new
SerpApiHttp
(
"/search"
);
this
.
client
.
setHttpConnectionTimeout
(
this
.
timeout
);
this
.
client
.
setHttpReadTimeout
(
this
.
timeout
);
}
/***
* Returns HTML search results as a raw HTML String
*
* @param parameter html search parameter
* @return raw HTML response from the client engine for custom parsing
* @throws SerpApiException wraps backend error message
*/
public
String
html
(
Map
<
String
,
String
>
parameter
)
throws
SerpApiException
{
return
get
(
"/client"
,
"html"
,
parameter
);
}
/***
* Returns search results as a raw markdown String
*
* The markdown format is produced by the backend and intended for LLM and
* agent consumption. It is returned unparsed, like html().
*
* @param parameter markdown search parameter
* @return raw markdown response from the client engine
* @throws SerpApiException wraps backend error message
*/
public
String
markdown
(
Map
<
String
,
String
>
parameter
)
throws
SerpApiException
{
return
get
(
"/search"
,
"md"
,
parameter
);
}
/***
* Returns search results as JSON object
*
* @param parameter custom search parameter which override the default parameter provided in the constructor
* @return JsonObject search results parent node
* @throws SerpApiException wraps backend error message
*/
public
JsonObject
search
(
Map
<
String
,
String
>
parameter
)
throws
SerpApiException
{
return
json
(
"/search"
,
parameter
);
}
/***
* Return location using Location API
*
* @param parameter must include {q: "city", limit: 3}
* @return JsonObject location using Location API
* @throws SerpApiException wraps backend error message
*/
public
JsonArray
location
(
Map
<
String
,
String
>
parameter
)
throws
SerpApiException
{
String
content
=
get
(
"/locations.json"
,
"json"
,
parameter
);
JsonElement
element
=
gson
.
fromJson
(
content
,
JsonElement
.
class
);
// An error is reported as an object, where a successful call returns an array.
if
(
element
.
isJsonObject
() &&
element
.
getAsJsonObject
().
has
(
"error"
)) {
this
.
client
.
triggerSerpApiException
(
content
);
}
return
element
.
getAsJsonArray
();
}
/***
* Retrieve search result from the Search Archive API
*
* @param id search unique identifier
* @return JsonObject client result
* @throws SerpApiException wraps backend error message
*/
public
JsonObject
searchArchive
(
String
id
)
throws
SerpApiException
{
return
json
(
"/searches/"
+
id
+
".json"
,
null
);
}
/***
* Get account information using Account API
*
* @param parameter Map including the api_key if not set in the default client parameter
* @return JsonObject account information
* @throws SerpApiException wraps backend error message
*/
public
JsonObject
account
(
Map
<
String
,
String
>
parameter
)
throws
SerpApiException
{
return
json
(
"/account.json"
,
parameter
);
}
/***
* Get account information using Account API
*
* @return JsonObject account information
* @throws SerpApiException wraps backend error message
*/
public
JsonObject
account
()
throws
SerpApiException
{
return
json
(
"/account.json"
,
null
);
}
/***
* Convert HTTP content to JsonValue
*
* @param content raw JSON HTTP response
* @return JsonObject created by gson parser
*/
private
JsonObject
json
(
String
endpoint
,
Map
<
String
,
String
>
parameter
)
throws
SerpApiException
{
String
content
=
get
(
endpoint
,
"json"
,
parameter
);
JsonElement
element
=
gson
.
fromJson
(
content
,
JsonElement
.
class
);
JsonObject
result
=
element
.
getAsJsonObject
();
// SerpApi reports some failures in the body of an HTTP 200 response, so the
// status code alone is not enough to tell success from failure.
if
(
result
.
has
(
"error"
)) {
this
.
client
.
triggerSerpApiException
(
content
);
}
return
result
;
}
/***
* Get the HTTP client used for requests
*
* @return http client
*/
public
SerpApiHttp
getClient
() {
return
this
.
client
;
}
/***
* Build a serp API query by expanding existing parameters
*
* @param path backend HTTP path
* @param output type of output format (json, html, md)
* @param parameter custom search parameter which override the default parameter provided in the constructor
* @return format parameter hash map
* @throws SerpApiException wraps backend error message
*/
public
String
get
(
String
path
,
String
output
,
Map
<
String
,
String
>
parameter
)
throws
SerpApiException
{
// Update path for the client
this
.
client
.
path
=
path
;
// create HTTP query
Map
<
String
,
String
>
query
=
new
HashMap
<>();
if
(
path
.
startsWith
(
"/searches"
)) {
// Only preserve API_KEY
query
.
put
(
"api_key"
,
this
.
parameter
.
get
(
"api_key"
));
}
else
{
// Merge default parameter
query
.
putAll
(
this
.
parameter
);
}
// override default parameter with custom parameter
if
(
parameter
!=
null
) {
query
.
putAll
(
parameter
);
}
// Set current programming language
query
.
put
(
"source"
,
"java"
);
// Set output format
query
.
put
(
"output"
,
output
);
return
this
.
client
.
get
(
query
);
}
}
Back
|
FazBrowse Home
|
New Git URL