-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathREADME.md.erb
More file actions
319 lines (238 loc) · 10.8 KB
/
README.md.erb
File metadata and controls
319 lines (238 loc) · 10.8 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<%-
def snippet(format, path)
lines = File.new(path).readlines
stop = lines.find_index { |line| line =~ /assertTrue/}
slice = File.new(path).readlines[23..stop-1]
slice << "System.out.println(results.toString());"
buf = slice.map { |l| l.gsub(/(^\s\s\s\s)/, '')}.join
buf.gsub!("System.getenv(\"API_KEY\")", "\"your_api_key\"")
%Q(```#{format}\n#{buf}\n```\n\n * source code: [#{path}](https://github.com/serpapi/serpapi-#{format}/blob/master/#{path}))
end
-%>
# SerpApi Java Library
[](https://github.com/serpapi/serpapi-java/actions/workflows/gradle.yml)
[](https://jitpack.io/#serpapi/serpapi-java)
Integrate search data into your Java application. This library is the official wrapper for SerpApi (https://serpapi.com).
SerpApi supports Google, Google Maps, Google Shopping, Baidu, Yandex, Yahoo, eBay, App Stores, and more.
[The full documentation is available here.](https://serpapi.com/search-api)
## Installation
Using Maven / Gradle.
Edit your build.gradle file
```java
repositories {
maven { url "https://jitpack.io" }
}
dependencies {
implementation 'com.github.serpapi:serpapi-java:1.0.0'
}
```
To list all the version available.
https://jitpack.io/api/builds/com.github.serpapi/serpapi-java
or you can download the jar file from https://github.com/serpapi/serpapi-java.git
Note: jitpack.io enables to download maven package directly from github release.
## Usage
To get started with this project in Java.
We provided a fully working example.
```bash
git clone https://github.com/serpapi/serpapi-java.git
cd serpapi-java/demo
make run api_key=<your private key>
```
Note: You need an account with SerpApi to obtain this key from: https://serpapi.com/dashboard
file: demo/src/main/java/demo/App.java
```java
public class App {
public static void main(String[] args) throws SerpApiException {
if(args.length != 1) {
System.out.println("Usage: app <secret api key>");
System.exit(1);
}
String location = "Austin,Texas";
System.out.println("find the first Coffee in " + location);
// set api_key provided by the command line
Map<String, String> auth = new HashMap<>();
auth.put("api_key", args[0]);
// Create search
SerpApi serpapi= new SerpApi(auth);
Map<String, String> parameter = new HashMap<>();
parameter.put("q", "Coffee");
parameter.put("location", location);
try {
// Perform search
JsonObject data = serpapi.search(parameter);
// Decode response
JsonArray results = data.get("local_results").getAsJsonObject().get("places").getAsJsonArray();
JsonObject first_result = results.get(0).getAsJsonObject();
System.out.println("first coffee shop: " + first_result.get("title").getAsString() + " found on Google in " + location);
} catch (SerpApiException e) {
System.out.println("oops exception detected!");
e.printStackTrace();
}
}
}
```
The [SerpApi.com API Documentation](https://serpapi.com/search-api) contains a list of all the possible parameters that can be passed to the API.
## Documentation
Documentation is [available on Read the Docs](https://serpapi-python.readthedocs.io/en/latest/).
## Requirements
This project is an implementation of SerpApi in Java 7.
This code depends on GSON for efficient JSON processing.
The HTTP response are converted to JSON Object.
Runtime:
- Java / JDK 8+ (https://www.java.com/en/download/)
Older version of java do not support HTTPS protocol.
The SSLv3 is buggy which leads to Java raising this exception: javax.net.ssl.SSLHandshakeException
For development:
- Gradle 7.7+ (https://gradle.org/install/)
### Location API
```java
SerpApi serpapi = new SerpApi();
Map<String, String> parameter = new HashMap<String, String>();
parameter.put("q", "Austin");
parameter.put("limit", "3");
JsonArray location = serpapi.location(parameter);
assertEquals("Austin, TX", location.get(0).getAsJsonObject().get("name").getAsString());
```
it prints the first 3 location matching Austin (Texas, Texas, Rochester)
[/src/test/java/serpapi/LocationApiTest.java](https://github.com/serpapi/serpapi-java/blob/master/src/test/java/serpapi/LocationApiTest.java)
### Search Archive API
Let's run a search to get a search_id.
```java
Map<String, String> auth = new HashMap<>();
auth.put("api_key", System.getenv("SERPAPI_KEY"));
SerpApi serpapi = new SerpApi(auth);
Map<String, String> parameter = new HashMap<>();
parameter.put("q", "Coffee");
parameter.put("location", "Austin, Texas, United States");
parameter.put("hl", "en");
parameter.put("gl", "us");
parameter.put("google_domain", "google.com");
parameter.put("safe", "active");
parameter.put("start", "10");
parameter.put("device", "desktop");
JsonObject results = serpapi.search(parameter);
```
Now let retrieve the previous search from the archive.
```java
// now search in the archive
String id = results.getAsJsonObject("search_metadata").getAsJsonPrimitive("id").getAsString();
// retrieve search from the archive with speed for free
JsonObject archive = serpapi.searchArchive(id);
System.out.println(archive.toString());
```
it prints the search from the archive which matches 1:1 to previous search results.
[/src/test/java/serpapi/SerpApiTest.java](https://github.com/serpapi/serpapi-java/blob/master/src/test/java/serpapi/SerpApiTest.java)
### Account API
Get account API
```java
Map<String, String> parameter = new HashMap<>();
parameter.put("api_key", "your_api_key");
SerpApi serpapi = new SerpApi(parameter);
JsonObject account = serpapi.account();
System.out.println(account.toString());
```
it prints your account information.
[/src/test/java/serpapi/AccountApiTest.java](https://github.com/serpapi/serpapi-java/blob/master/src/test/java/serpapi/AccountApiTest.java)
## Examples in java
### Search bing
<%= snippet('java', 'src/test/java/serpapi/BingTest.java') %>
see: [https://serpapi.com/bing-search-api](https://serpapi.com/bing-search-api)
### Search baidu
<%= snippet('java', 'src/test/java/serpapi/BaiduTest.java') %>
see: [https://serpapi.com/baidu-search-api](https://serpapi.com/baidu-search-api)
### Search yahoo
<%= snippet('java', 'src/test/java/serpapi/YahooTest.java') %>
see: [https://serpapi.com/yahoo-search-api](https://serpapi.com/yahoo-search-api)
### Search youtube
<%= snippet('java', 'src/test/java/serpapi/YoutubeTest.java') %>
see: [https://serpapi.com/youtube-search-api](https://serpapi.com/youtube-search-api)
### Search walmart
<%= snippet('java', 'src/test/java/serpapi/WalmartTest.java') %>
see: [https://serpapi.com/walmart-search-api](https://serpapi.com/walmart-search-api)
### Search ebay
<%= snippet('java', 'src/test/java/serpapi/EbayTest.java') %>
see: [https://serpapi.com/ebay-search-api](https://serpapi.com/ebay-search-api)
### Search naver
<%= snippet('java', 'src/test/java/serpapi/NaverTest.java') %>
see: [https://serpapi.com/naver-search-api](https://serpapi.com/naver-search-api)
### Search home depot
<%= snippet('java', 'src/test/java/serpapi/HomeDepotTest.java') %>
see: [https://serpapi.com/home-depot-search-api](https://serpapi.com/home-depot-search-api)
### Search apple app store
<%= snippet('java', 'src/test/java/serpapi/AppleAppStoreTest.java') %>
see: [https://serpapi.com/apple-app-store](https://serpapi.com/apple-app-store)
### Search duckduckgo
<%= snippet('java', 'src/test/java/serpapi/DuckduckgoTest.java') %>
see: [https://serpapi.com/duckduckgo-search-api](https://serpapi.com/duckduckgo-search-api)
### Search google
<%= snippet('java', 'src/test/java/serpapi/GoogleTest.java') %>
see: [https://serpapi.com/search-api](https://serpapi.com/search-api)
### Search google scholar
<%= snippet('java', 'src/test/java/serpapi/GoogleScholarTest.java') %>
see: [https://serpapi.com/google-scholar-api](https://serpapi.com/google-scholar-api)
### Search google autocomplete
<%= snippet('java', 'src/test/java/serpapi/GoogleAutocompleteTest.java') %>
see: [https://serpapi.com/google-autocomplete-api](https://serpapi.com/google-autocomplete-api)
### Search google product
<%= snippet('java', 'src/test/java/serpapi/GoogleProductTest.java') %>
see: [https://serpapi.com/google-product-api](https://serpapi.com/google-product-api)
### Search google reverse image
<%= snippet('java', 'src/test/java/serpapi/GoogleReverseImageTest.java') %>
see: [https://serpapi.com/google-reverse-image](https://serpapi.com/google-reverse-image)
### Search google events
<%= snippet('java', 'src/test/java/serpapi/GoogleEventsTest.java') %>
see: [https://serpapi.com/google-events-api](https://serpapi.com/google-events-api)
### Search google local services
<%= snippet('java', 'src/test/java/serpapi/GoogleLocalServicesTest.java') %>
see: [https://serpapi.com/google-local-services-api](https://serpapi.com/google-local-services-api)
### Search google maps
<%= snippet('java', 'src/test/java/serpapi/GoogleMapsTest.java') %>
see: [https://serpapi.com/google-maps-api](https://serpapi.com/google-maps-api)
### Search google jobs
<%= snippet('java', 'src/test/java/serpapi/GoogleJobsTest.java') %>
see: [https://serpapi.com/google-jobs-api](https://serpapi.com/google-jobs-api)
### Search google play
<%= snippet('java', 'src/test/java/serpapi/GooglePlayTest.java') %>
see: [https://serpapi.com/google-play-api](https://serpapi.com/google-play-api)
### Search google images
<%= snippet('java', 'src/test/java/serpapi/GoogleImagesTest.java') %>
see: [https://serpapi.com/images-results](https://serpapi.com/images-results)
### Contributing
We love true open source, continuous integration and Test Drive Development (TDD).
We are using JUnit test, Git action, gradle [our infrastructure around the clock](https://travis-ci.org/serpapi/serpapi-java) to achieve the best QoS (Quality Of Service).
To run the test:
```gradle test```
#### How to build from the source ?
You must clone this repository.
```bash
git clone https://github.com/serpapi/serpapi-java.git
```
Build the jar file.
```bash
gradle build
```
Copy the jar to your project lib/ directory.
```bash
cp build/libs/serpapi-java.jar path/to/yourproject/lib
```
## Java limitation
### SSL handshake error.
#### symptom
javax.net.ssl.SSLHandshakeException
#### cause
SerpApi is using HTTPS / SSLv3. Older JVM version do not support this protocol because it's more recent.
#### solution
Upgrade java to 1.8_201+ (which is recommended by Oracle).
* On OSX you can switch versino of Java.
```sh
export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_201`
java -version
```
* On Windows manually upgrade your JDK / JVM to the latest.
### Inspiration
* http://www.baeldung.com/java-http-request
* https://github.com/google/gson
## License
MIT license
## Changelog
- 1.0.0 - Fully revisit API naming convention, and generalize client usage to match serpapi.com latest development