Skip to content

Commit f92763d

Browse files
committed
bug fixes+ API improvement
documentation update
1 parent 8503411 commit f92763d

File tree

10 files changed

+193
-221
lines changed

10 files changed

+193
-221
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018-2023 SerpApi
3+
Copyright (c) 2018-2024 SerpApi
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ oobt: build
1919
$(MAKE) -C demo all
2020

2121
doc:
22-
gradle javadoc:javadoc
22+
gradle javadoc --info --warning-mode all
2323

2424
# Create a release using GitHub
2525
release: doc build
2626
@echo "drag drop file"
2727
open build/libs/
2828
open build/distributions/
29-
open -a "Google Chrome" https://github.com/serpapi/google-search-results-java/releases
29+
open -a "Google Chrome" https://github.com/serpapi/serpapi-java/releases

README.erb.md

Lines changed: 79 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
1-
# Google Search Results JAVA API
1+
# SerpApi Java Library
22

33
![test](https://github.com/serpapi/google-search-results-java/workflows/test/badge.svg)
44

5-
This Java package enables to scrape and parse Google, Bing and Baidu search results using [SerpApi](https://serpapi.com). Feel free to fork this repository to add more backends.
5+
Integrate search data into your Java application. This library is the official wrapper for SerpApi (https://serpapi.com).
66

7-
This project is an implementation of SerpApi in Java 7.
8-
This code depends on GSON for efficient JSON processing.
9-
The HTTP response are converted to JSON Object.
10-
11-
An example is provided in the test.
12-
@see src/test/java/GoogleSearchImplementationTest.java
7+
SerpApi supports Google, Google Maps, Google Shopping, Baidu, Yandex, Yahoo, eBay, App Stores, and more.
138

149
[The full documentation is available here.](https://serpapi.com/search-api)
1510

16-
## Requirements
11+
## Installation
1712

18-
Runtime:
19-
- Java / JDK 8+ (https://www.java.com/en/download/)
20-
Older version of java do not support HTTPS protocol.
21-
The SSLv3 is buggy which leads to Java raising this exception: javax.net.ssl.SSLHandshakeException
22-
23-
For development:
24-
- Gradle 6.7+ (https://gradle.org/install/)
25-
26-
## Maven / Gradle support
13+
Using Maven / Gradle.
2714

2815
Edit your build.gradle file
2916
```java
@@ -39,9 +26,11 @@ dependencies {
3926
To list all the version available.
4027
https://jitpack.io/api/builds/com.github.serpapi/serpapi
4128

29+
or you can download the jar file from https://github.com/serpapi/serpapi-java.git
30+
4231
Note: jitpack.io enables to download maven package directly from github release.
4332

44-
## Quick start
33+
## Usage
4534

4635
To get started with this project in Java.
4736
We provided a fully working example.
@@ -68,17 +57,17 @@ public class App {
6857
Map<String, String> parameter = new HashMap<>();
6958
parameter.put("q", "Coffee");
7059
parameter.put("location", location);
71-
parameter.put("apikey", args[0]);
60+
parameter.put("api_key", args[0]);
7261

7362
// Create search
7463
SerpApi serpapi = new SerpApi(parameter);
7564

7665
try {
7766
// Execute search
78-
JsonObject data = search.getJson();
67+
JsonObject data = serpapi.search(parameter);
7968

8069
// Decode response
81-
JsonArray results = (JsonArray) data.get("local_results");
70+
JsonArray results = data.get("local_results").getAsJsonObject().get("places").getAsJsonArray();
8271
JsonObject first_result = results.get(0).getAsJsonObject();
8372
System.out.println("first coffee: " + first_result.get("title").getAsString() + " in " + location);
8473
} catch (SerpApiException e) {
@@ -89,89 +78,42 @@ public class App {
8978
}
9079
```
9180

92-
This example runs a search about "coffee" using your secret api key.
93-
94-
The Serp API service (backend)
95-
- searches on Google using the query: q = "coffee"
96-
- parses the messy HTML responses
97-
- return a standardized JSON response
98-
The class GoogleSearch
99-
- Format the request to Serp API server
100-
- Execute GET http request
101-
- Parse JSON into Ruby Hash using JSON standard library provided by Ruby
102-
Et voila..
103-
104-
Alternatively, you can search:
105-
- Bing using BingSearch class
106-
- Baidu using BaiduSearch class
107-
108-
See the playground to generate your code.
109-
https://serpapi.com/playground
110-
111-
## Example
112-
* [How to set SERP API key](#how-to-set-serp-api-key)
113-
* [Search API capability](#search-api-capability)
114-
* [Example by specification](#example-by-specification)
115-
* [Location API](#location-api)
116-
* [Search Archive API](#search-archive-api)
117-
* [Account API](#account-api)
118-
119-
## How to set SERP API key
120-
The Serp API key can be set globally using a singleton pattern.
121-
```java
122-
GoogleSearch.serp_api_key_default = "Your Private Key"
123-
search = GoogleSearch(parameter)
124-
```
125-
Or the Serp API key can be provided for each query.
81+
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.
12682

127-
```java
128-
search = GoogleSearch(parameter, "Your Private Key")
129-
```
13083

131-
## Example with all params and all outputs
84+
## Documentation
13285

133-
```java
134-
query_parameter = {
135-
"q": "query",
136-
"google_domain": "Google Domain",
137-
"location": "Location Requested",
138-
"device": device,
139-
"hl": "Google UI Language",
140-
"gl": "Google Country",
141-
"safe": "Safe Search Flag",
142-
"num": "Number of Results",
143-
"start": "Pagination Offset",
144-
"serp_api_key": "Your SERP API Key",
145-
"tbm": "nws|isch|shop",
146-
"tbs": "custom to be search criteria",
147-
"async": true|false, // allow async request - non-blocker
148-
"output": "json|html", // output format
149-
}
86+
Documentation is [available on Read the Docs](https://serpapi-python.readthedocs.io/en/latest/).
15087

151-
query = GoogleSearch.new(query_parameter)
152-
query.parameter.put("location", "Austin,Texas")
88+
## Requirements
15389

154-
String html_results = query.getHtml()
155-
JsonObject json_results = query.getJson()
156-
```
90+
This project is an implementation of SerpApi in Java 7.
91+
This code depends on GSON for efficient JSON processing.
92+
The HTTP response are converted to JSON Object.
15793

158-
### Example by specification
94+
An example is provided in the test.
95+
@see src/test/java/SerpApiImplementationTest.java
15996

160-
We love true open source, continuous integration and Test Drive Development (TDD).
161-
We are using RSpec to test [our infrastructure around the clock](https://travis-ci.org/serpapi/google-search-results-ruby) to achieve the best QoS (Quality Of Service).
162-
163-
The directory test/ includes specification/examples.
97+
Runtime:
98+
- Java / JDK 8+ (https://www.java.com/en/download/)
99+
Older version of java do not support HTTPS protocol.
100+
The SSLv3 is buggy which leads to Java raising this exception: javax.net.ssl.SSLHandshakeException
164101

165-
To run the test:
166-
```gradle test```
102+
For development:
103+
- Gradle 6.7+ (https://gradle.org/install/)
167104

168105

169106
### Location API
170107

171108
```java
172-
GoogleSearch search = new GoogleSearch(new HashMap<String, String());
173-
JsonArray locationList = search.getLocation("Austin", 3);
174-
System.out.println(locationList.toString());
109+
Map<String, String> parameter = new HashMap<String, String>();
110+
parameter.put("api_key", System.getenv("API_KEY"));
111+
SerpApi serpapi = new SerpApi(parameter);
112+
113+
parameter.put("q", "Austin");
114+
parameter.put("limit", "3");
115+
JsonArray location = serpapi.location(parameter);
116+
System.out.println(location.toString());
175117
```
176118
it prints the first 3 location matching Austin (Texas, Texas, Rochester)
177119

@@ -180,34 +122,58 @@ it prints the first 3 location matching Austin (Texas, Texas, Rochester)
180122
Let's run a search to get a search_id.
181123
```java
182124
Map<String, String> parameter = new HashMap<>();
125+
parameter.put("api_key", System.getenv("API_KEY"));
183126
parameter.put("q", "Coffee");
184-
parameter.put("location", "Austin,Texas");
127+
parameter.put("location", "Austin, Texas, United States");
128+
parameter.put("hl", "en");
129+
parameter.put("gl", "us");
130+
parameter.put("google_domain", "google.com");
131+
parameter.put("safe", "active");
132+
parameter.put("start", "10");
133+
parameter.put("num", "10");
134+
parameter.put("device", "desktop");
135+
136+
SerpApi serpapi = new SerpApi(parameter);
137+
JsonObject results = serpapi.search(parameter);
138+
139+
185140

186-
GoogleSearch search = new GoogleSearch(parameter);
187-
JsonObject result = search.getJson();
188-
int search_id = result.get("search_metadata").getAsJsonObject().get("id").getAsInt();
189141
```
190142

191143
Now let retrieve the previous search from the archive.
192144
```java
193-
JsonObject archived_result = search.getSearchArchive(search_id);
194-
System.out.println(archived_result.toString());
145+
// now search in the archive
146+
String id = results.getAsJsonObject("search_metadata").getAsJsonPrimitive("id").getAsString();
147+
148+
// retrieve search from the archive with speed for free
149+
JsonObject archive = serpapi.searchArchive(id);
150+
System.out.println(archive.toString());
195151
```
196-
it prints the search from the archive.
152+
it prints the search from the archive which matches 1:1 to previous search results.
197153

198154
### Account API
199155
Get account API
200156
```java
201-
GoogleSearch.serp_api_key_default = "Your Private Key"
202-
GoogleSearch search = new GoogleSearch();
203-
JsonObject info = search.getAccount();
204-
System.out.println(info.toString());
157+
Map<String, String> parameter = new HashMap<>();
158+
parameter.put("api_key", "your_api_key");
159+
160+
SerpApi serpapi = new SerpApi(parameter);
161+
JsonObject account = serpapi.account();
162+
System.out.println(account.toString());
205163
```
206164
it prints your account information.
207165

208-
## Build project
166+
### Contributing
167+
168+
We love true open source, continuous integration and Test Drive Development (TDD).
169+
We are using RSpec to test [our infrastructure around the clock](https://travis-ci.org/serpapi/google-search-results-ruby) to achieve the best QoS (Quality Of Service).
170+
171+
The directory test/ includes specification/examples.
172+
173+
To run the test:
174+
```gradle test```
209175

210-
### How to build from the source ?
176+
#### How to build from the source ?
211177

212178
You must clone this repository.
213179
```bash
@@ -222,29 +188,8 @@ Copy the jar to your project lib/ directory.
222188
cp build/libs/serpapi-java.jar path/to/yourproject/lib
223189
```
224190

225-
### How to test ?
226-
227-
```bash
228-
make test
229-
```
230-
231-
### Conclusion
232-
233-
This service supports Google Images, News, Shopping.
234-
To enable a type of search, the field tbm (to be matched) must be set to:
235-
236-
* isch: Google Images API.
237-
* nws: Google News API.
238-
* shop: Google Shopping API.
239-
* any other Google service should work out of the box.
240-
* (no tbm parameter): regular Google Search.
241-
242-
[The full documentation is available here.](https://serpapi.com/search-api)
243-
244-
Issue
245-
---
191+
## Java limitation
246192
### SSL handshake error.
247-
248193
#### symptom
249194

250195
javax.net.ssl.SSLHandshakeException
@@ -263,15 +208,13 @@ java -version
263208

264209
* On Windows manually upgrade your JDK / JVM to the latest.
265210

266-
Changelog
267-
---
268-
- 1.0.0 - Fully revisit API naming convention, and generalize client usage to match serpapi.com latest development
269-
270-
Source
271-
---
211+
### Inspiration
272212
* http://www.baeldung.com/java-http-request
273213
* https://github.com/google/gson
214+
215+
## License
216+
MIT license
217+
218+
## Changelog
219+
- 1.0.0 - Fully revisit API naming convention, and generalize client usage to match serpapi.com latest development
274220

275-
Author
276-
---
277-
Victor Benarbia - [email protected]

build.gradle

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ targetCompatibility = 1.8
1818

1919
// Load repositories
2020
repositories {
21-
// jcenter()
2221
mavenCentral()
22+
23+
maven { url "https://jitpack.io" }
2324
}
2425

25-
// compileTestJava {
26-
// options.encoding = 'UTF-8'
27-
// options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
28-
// }
26+
java {
27+
withJavadocJar()
28+
}
2929

3030
dependencies {
3131
implementation 'com.google.code.gson:gson:2.9.0'
@@ -47,21 +47,6 @@ tasks.named('jar') {
4747
}
4848
}
4949

50-
task sourcesJar(type: Jar, dependsOn: classes) {
51-
classifier = 'sources'
52-
from sourceSets.main.allSource
53-
}
54-
55-
task javadocJar(type: Jar, dependsOn: javadoc) {
56-
classifier = 'javadoc'
57-
from javadoc.destinationDir
58-
}
59-
60-
artifacts {
61-
archives sourcesJar
62-
archives javadocJar
63-
}
64-
6550
publishing {
6651
publications {
6752
mavenJava(MavenPublication) {

demo/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ clean:
1717

1818
dep:
1919
$(MAKE) -C ../ build
20-
rm -rf libs/*.jar
20+
rm -rf libs
21+
mkdir -p libs
2122
cp ../build/libs/* libs
2223

2324
build: clean

0 commit comments

Comments
 (0)