Skip to content

Commit da85ebc

Browse files
committed
copy over source code with a lot of modification from previous implementation.
this current code is not functional
0 parents  commit da85ebc

28 files changed

+1760
-0
lines changed

.github/workflows/gradle.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This workflow will build a Java project with Gradle
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle
3+
4+
name: test
5+
6+
on:
7+
push:
8+
branches: [ master ]
9+
pull_request:
10+
branches: [ master ]
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/checkout@v2
19+
- name: Set up JDK 1.8
20+
uses: actions/setup-java@v1
21+
with:
22+
java-version: 1.8
23+
- name: Grant execute permission for gradlew
24+
run: chmod +x gradlew
25+
- name: test
26+
run: make test
27+
env:
28+
API_KEY: ${{secrets.API_KEY}}

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
build/
2+
.idea/
3+
.gradle/
4+
.settings/
5+
.classpath
6+
.project
7+
bin/
8+
data.json
9+
demo/gradle/
10+
demo/libs/*.jar
11+
derive.rb
12+
gradlew.bat
13+
switch_java.sh

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018-2023 SerpApi
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.PHONY: build
2+
3+
all: clean test
4+
5+
# clean
6+
clean:
7+
./gradlew clean
8+
9+
# Run gradle test with information
10+
test:
11+
./gradlew test --info
12+
13+
build: clean
14+
# ./gradlew build -x test
15+
./gradlew build publishToMavenLocal
16+
@echo "see build/lib"
17+
18+
oobt: build
19+
$(MAKE) -C demo all
20+
21+
doc:
22+
gradle javadoc:javadoc
23+
24+
# Create a release using GitHub
25+
release: doc build
26+
@echo "drag drop file"
27+
open build/libs/
28+
open build/distributions/
29+
open -a "Google Chrome" https://github.com/serpapi/google-search-results-java/releases

README.erb.md

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
# Google Search Results JAVA API
2+
3+
![test](https://github.com/serpapi/google-search-results-java/workflows/test/badge.svg)
4+
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.
6+
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
13+
14+
[The full documentation is available here.](https://serpapi.com/search-api)
15+
16+
## Requirements
17+
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
27+
28+
Edit your build.gradle file
29+
```java
30+
repositories {
31+
maven { url "https://jitpack.io" }
32+
}
33+
34+
dependencies {
35+
implementation 'com.github.serpapi:serpapi:1.0.0'
36+
}
37+
```
38+
39+
To list all the version available.
40+
https://jitpack.io/api/builds/com.github.serpapi/serpapi
41+
42+
Note: jitpack.io enables to download maven package directly from github release.
43+
44+
## Quick start
45+
46+
To get started with this project in Java.
47+
We provided a fully working example.
48+
```bash
49+
git clone https://github.com/serpapi/serpapi-java.git
50+
cd serpapi-java/demo
51+
make run api_key=<your private key>
52+
```
53+
Note: You need an account with SerpApi to obtain this key from: https://serpapi.com/dashboard
54+
55+
file: demo/src/main/java/demo/App.java
56+
```java
57+
public class App {
58+
public static void main(String[] args) throws SerpApiException {
59+
if(args.length != 1) {
60+
System.out.println("Usage: app <secret api key>");
61+
System.exit(1);
62+
}
63+
64+
String location = "Austin,Texas";
65+
System.out.println("find the first Coffee in " + location);
66+
67+
// parameters
68+
Map<String, String> parameter = new HashMap<>();
69+
parameter.put("q", "Coffee");
70+
parameter.put("location", location);
71+
parameter.put("apikey", args[0]);
72+
73+
// Create search
74+
SerpApi serpapi = new SerpApi(parameter);
75+
76+
try {
77+
// Execute search
78+
JsonObject data = search.getJson();
79+
80+
// Decode response
81+
JsonArray results = (JsonArray) data.get("local_results");
82+
JsonObject first_result = results.get(0).getAsJsonObject();
83+
System.out.println("first coffee: " + first_result.get("title").getAsString() + " in " + location);
84+
} catch (SerpApiException e) {
85+
System.out.println("oops exception detected!");
86+
e.printStackTrace();
87+
}
88+
}
89+
}
90+
```
91+
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.
126+
127+
```java
128+
search = GoogleSearch(parameter, "Your Private Key")
129+
```
130+
131+
## Example with all params and all outputs
132+
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+
}
150+
151+
query = GoogleSearch.new(query_parameter)
152+
query.parameter.put("location", "Austin,Texas")
153+
154+
String html_results = query.getHtml()
155+
JsonObject json_results = query.getJson()
156+
```
157+
158+
### Example by specification
159+
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.
164+
165+
To run the test:
166+
```gradle test```
167+
168+
169+
### Location API
170+
171+
```java
172+
GoogleSearch search = new GoogleSearch(new HashMap<String, String());
173+
JsonArray locationList = search.getLocation("Austin", 3);
174+
System.out.println(locationList.toString());
175+
```
176+
it prints the first 3 location matching Austin (Texas, Texas, Rochester)
177+
178+
### Search Archive API
179+
180+
Let's run a search to get a search_id.
181+
```java
182+
Map<String, String> parameter = new HashMap<>();
183+
parameter.put("q", "Coffee");
184+
parameter.put("location", "Austin,Texas");
185+
186+
GoogleSearch search = new GoogleSearch(parameter);
187+
JsonObject result = search.getJson();
188+
int search_id = result.get("search_metadata").getAsJsonObject().get("id").getAsInt();
189+
```
190+
191+
Now let retrieve the previous search from the archive.
192+
```java
193+
JsonObject archived_result = search.getSearchArchive(search_id);
194+
System.out.println(archived_result.toString());
195+
```
196+
it prints the search from the archive.
197+
198+
### Account API
199+
Get account API
200+
```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());
205+
```
206+
it prints your account information.
207+
208+
## Build project
209+
210+
### How to build from the source ?
211+
212+
You must clone this repository.
213+
```bash
214+
git clone https://github.com/serpapi/serpapi-java.git
215+
```
216+
Build the jar file.
217+
```bash
218+
gradle build
219+
```
220+
Copy the jar to your project lib/ directory.
221+
```bash
222+
cp build/libs/serpapi-java.jar path/to/yourproject/lib
223+
```
224+
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+
---
246+
### SSL handshake error.
247+
248+
#### symptom
249+
250+
javax.net.ssl.SSLHandshakeException
251+
252+
#### cause
253+
SerpApi is using HTTPS / SSLv3. Older JVM version do not support this protocol because it's more recent.
254+
255+
#### solution
256+
Upgrade java to 1.8_201+ (which is recommended by Oracle).
257+
258+
* On OSX you can switch versino of Java.
259+
```sh
260+
export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_201`
261+
java -version
262+
```
263+
264+
* On Windows manually upgrade your JDK / JVM to the latest.
265+
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+
---
272+
* http://www.baeldung.com/java-http-request
273+
* https://github.com/google/gson
274+
275+
Author
276+
---
277+
Victor Benarbia - [email protected]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Early development in progress

0 commit comments

Comments
 (0)