-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.md.erb
392 lines (315 loc) · 12.3 KB
/
README.md.erb
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<%-
def snippet(format, path, demo = false)
lines = File.new(path).readlines
if demo
buf = lines[1..-1].join
else
start = lines.find_index { |line| line =~ /shoulSkip/ } + 4
stop = lines.size - 1
buf = File.new(path).readlines[start..stop].join
buf.gsub!("*getApiKey()", "\"secret_api_key\"")
buf.gsub!("t.Error", "fmt.Println")
end
%Q(```golang
import (
"github.com/serpapi/serpapi-golang"
"fmt"
)
func main() {
#{buf}\n```\n\n * source code: [#{path}](https://github.com/serpapi/serpapi-golang/blob/master/#{path}))
end
-%>
<div align="center">
<h1 align="center">SerpApi Go Library</h1>
[![serpapi-go](https://github.com/serpapi/serpapi-golang/actions/workflows/go.yml/badge.svg?branch=master)](https://github.com/serpapi/serpapi-golang/actions/workflows/go.yml)
</div>
Integrate search data into your Go application. This library is the official wrapper for [SerpApi](https://serpapi.com).
[SerpApi](https://serpapi.com) supports Google, Google Maps, Google Shopping, Baidu, Yandex, Yahoo, eBay, App Stores, and more.
## Installation
Go 1.10 is required.
```bash
go get -u github.com/serpapi/serpapi-golang
```
## Simple Usage
```golang
import "github.com/serpapi/serpapi-golang"
auth := map[string]string{
"api_key": "secret_api_key"
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"engine": "google",
"q": "coffee"
}
data, err := client.Search(parameter)
fmt.Println(data)
```
This example runs a search for "coffee" on Google. It then returns the results a Go Hash. See the [playground](https://serpapi.com/playground) to generate your own code.
## Advanced Usage
### Search API
```golang
func main() {
// serpapi client created with default parameters
auth := map[string]string{
"api_key": "secret_key",
"timeout": "30",
"engine": "google",
}
client := serpapi.NewClient(auth)
// We recommend that you keep your keys safe.
// At least, don't commit them in plain text.
// More about configuration via environment variables:
// https://hackernoon.com/all-the-secrets-of-encrypting-api-keys-in-golang-revealed-5qf3t5l
// search query overview (more fields available depending on search engine)
parameter := map[string]string{
"q": "Coffee",
"location": "Portland, Oregon, United States",
"hl": "en",
"gl": "us",
"google_domain": "google.com",
"safe": "active",
"start": "10",
"num": "10",
"device": "desktop",
}
// formated search results as a Hash
// serpapi.com converts HTML -> JSON
rsp, err := client.Search(parameter)
if err != nil {
panic(err)
}
fmt.Println(rsp)
// raw search engine html as a String
// serpapi.com acts a proxy to provive high throughputs, no search limit and more.
raw_html, err := client.Html(parameter)
if err != nil {
panic(err)
}
fmt.Println(raw_html)
}
```
[Google search documentation](https://serpapi.com/search-api).
More hands on examples are available below.
#### Documentations
* [Full documentation on SerpApi.com](https://serpapi.com)
* [Library Github page](https://github.com/serpapi/serpapi-golang)
* [API health status](https://serpapi.com/status)
### Location API
```golang
client := serpapi.NewClient(map[string]string{})
rsp, err := client.Location("Austin", 3)
if err != nil {
panic(err)
}
fmt.Println(rsp)
```
it prints the first 3 locations matching Austin (Texas, Texas, Rochester)
```
[map[canonical_name:Austin,TX,Texas,United States country_code:US google_id:200635 google_parent_id:21176 gps:[-97.7430608 30.267153]...
```
NOTE: api_key is not required for this endpoint.
### Search Archive API
This API allows retrieving previous search results.
To fetch earlier results from the search_id.
First, you need to run a search and save the search id.
```golang
// First, you need to run a search and save the search id.
auth := map[string]string{
"engine": "google",
"api_key": "secret_api_key",
}
client := serpapi.NewClient(auth)
parameter := map[string]string{
"q": "Coffee",
"location": "Portland"}
rsp, err := client.Search(parameter)
if err != nil {
t.Error("unexpected error", err)
return
}
// Now let's retrieve the previous search results from the archive.
searchID := rsp["search_metadata"].(map[string]interface{})["id"].(string)
if len(searchID) == 0 {
t.Error("search_metadata.id must be defined")
return
}
searchArchive, err := client.SearchArchive(searchID)
if err != nil {
t.Error(err)
return
}
searchIDArchive := searchArchive["search_metadata"].(map[string]interface{})["id"].(string)
if searchIDArchive != searchID {
t.Error("search_metadata.id do not match", searchIDArchive, searchID)
}
```
This code prints the search results from the archive. :)
### Account API
```golang
auth := map[string]string{
"api_key": "<secret_api_key>"
}
client := serpapi.NewClient(auth)
rsp, err = client.Account()
fmt.Println(rsp)
```
It prints your account information.
## Basic examples in Go
### Search bing
<%= snippet('golang', 'test/example_search_bing_test.go') %>
see: [https://serpapi.com/bing-search-api](https://serpapi.com/bing-search-api)
### Search baidu
<%= snippet('golang', 'test/example_search_baidu_test.go') %>
see: [https://serpapi.com/baidu-search-api](https://serpapi.com/baidu-search-api)
### Search yahoo
<%= snippet('golang', 'test/example_search_yahoo_test.go') %>
see: [https://serpapi.com/yahoo-search-api](https://serpapi.com/yahoo-search-api)
### Search youtube
<%= snippet('golang', 'test/example_search_youtube_test.go') %>
see: [https://serpapi.com/youtube-search-api](https://serpapi.com/youtube-search-api)
### Search walmart
<%= snippet('golang', 'test/example_search_walmart_test.go') %>
see: [https://serpapi.com/walmart-search-api](https://serpapi.com/walmart-search-api)
### Search ebay
<%= snippet('golang', 'test/example_search_ebay_test.go') %>
see: [https://serpapi.com/ebay-search-api](https://serpapi.com/ebay-search-api)
### Search naver
<%= snippet('golang', 'test/example_search_naver_test.go') %>
see: [https://serpapi.com/naver-search-api](https://serpapi.com/naver-search-api)
### Search home depot
<%= snippet('golang', 'test/example_search_home_depot_test.go') %>
see: [https://serpapi.com/home-depot-search-api](https://serpapi.com/home-depot-search-api)
### Search apple app store
<%= snippet('golang', 'test/example_search_apple_app_store_test.go') %>
see: [https://serpapi.com/apple-app-store](https://serpapi.com/apple-app-store)
### Search duckduckgo
<%= snippet('golang', 'test/example_search_duckduckgo_test.go') %>
see: [https://serpapi.com/duckduckgo-search-api](https://serpapi.com/duckduckgo-search-api)
### Search google
<%= snippet('golang', 'test/example_search_google_test.go') %>
see: [https://serpapi.com/search-api](https://serpapi.com/search-api)
### Search google scholar
<%= snippet('golang', 'test/example_search_google_scholar_test.go') %>
see: [https://serpapi.com/google-scholar-api](https://serpapi.com/google-scholar-api)
### Search google autocomplete
<%= snippet('golang', 'test/example_search_google_autocomplete_test.go') %>
see: [https://serpapi.com/google-autocomplete-api](https://serpapi.com/google-autocomplete-api)
### Search google product
<%= snippet('golang', 'test/example_search_google_product_test.go') %>
see: [https://serpapi.com/google-product-api](https://serpapi.com/google-product-api)
### Search google reverse image
<%= snippet('golang', 'test/example_search_google_reverse_image_test.go') %>
see: [https://serpapi.com/google-reverse-image](https://serpapi.com/google-reverse-image)
### Search google events
<%= snippet('golang', 'test/example_search_google_events_test.go') %>
see: [https://serpapi.com/google-events-api](https://serpapi.com/google-events-api)
### Search google local services
<%= snippet('golang', 'test/example_search_google_local_services_test.go') %>
see: [https://serpapi.com/google-local-services-api](https://serpapi.com/google-local-services-api)
### Search google maps
<%= snippet('golang', 'test/example_search_google_maps_test.go') %>
see: [https://serpapi.com/google-maps-api](https://serpapi.com/google-maps-api)
### Search google jobs
<%= snippet('golang', 'test/example_search_google_jobs_test.go') %>
see: [https://serpapi.com/google-jobs-api](https://serpapi.com/google-jobs-api)
### Search google play
<%= snippet('golang', 'test/example_search_google_play_test.go') %>
see: [https://serpapi.com/google-play-api](https://serpapi.com/google-play-api)
## Advanced search API usage
### Highly scalable batching
Search API features non-blocking search using the option: `async=true`.
- Non-blocking - async=true - a single parent process can handle unlimited concurrent searches.
- Blocking - async=false - many processes must be forked and synchronized to handle concurrent searches. This strategy is I/O usage because each client would hold a network connection.
Search API enables `async` search.
- Non-blocking (`async=true`) : the development is more complex, but this allows handling many simultaneous connections.
- Blocking (`async=false`) : it's easy to write the code but more compute-intensive when the parent process needs to hold many connections.
Here is an example of asynchronous searches using Go
<%= snippet('golang', 'oobt/demo.go', true) %>
This code shows a simple solution to batch searches asynchronously into a [queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)).
Each search takes a few seconds before completion by SerpApi service and the search engine. By the time the first element pops out of the queue. The search result might be already available in the archive. If not, the `search_archive` method blocks until the search results are available.
## Supported Go version.
Go versions validated by Github Actions:
- 3.1
- 2.6
see: [Github Actions.](https://github.com/serpapi/serpapi-golang/actions/workflows/ci.yml)
## Change logs
* [2023-09-01] 1.0.0 Full API support
## Developer Guide
### Key goals
- Brand centric instead of search engine based
- No hard-coded logic per search engine
- Simple HTTP client (lightweight, reduced dependency)
- No magic default values
- Thread safe
- Easy extension
- Defensive code style (raise a custom exception)
- TDD
- Best API coding practice per platform
- KiSS principles
### Inspirations
This project source code and coding style was inspired by Go itself.
This programming language native provides all the recommendation to build awesome software.
### Code quality expectations
- 0 lint offense: `make lint`
- 100% tests passing: `make test`
- 100% code coverage: `make test`
# Developer Guide
## Design : UML diagram
### Class diagram
```mermaid
classDiagram
Application *-- serpapi
serpapi *-- Client
class Client {
engine String
api_key String
params Hash
search() Hash
html() String
location() String
search_archive() Hash
account() Hash
}
openuri <.. Client
json <.. Client
Go <.. openuri
Go <.. json
```
### search() : Sequence diagram
```mermaid
sequenceDiagram
Client->>SerpApi.com: search() : http request
SerpApi.com-->>SerpApi.com: query search engine
SerpApi.com-->>SerpApi.com: parse HTML into JSON
SerpApi.com-->>Client: JSON string payload
Client-->>Client: decode JSON into Hash
```
where:
- The end user implements the application.
- Client refers to SerpApi:Client.
- SerpApi.com is the backend HTTP / REST service.
- Engine refers to Google, Baidu, Bing, and more.
The SerpApi.com service (backend)
- executes a scalable search on `engine: "google"` using the search query: `q: "coffee"`.
- parses the messy HTML responses from Google on the backend.
- returns a standardized JSON response.
The class SerpApi::Client (client side / golang):
- Format the request to SerpApi.com server.
- Execute HTTP Get request.
- Parse JSON into Go Hash using a standard JSON library.
Et voila!
## Continuous integration
We love "true open source" and "continuous integration", and Test Drive Development (TDD).
We are using RSpec to test [our infrastructure around the clock]) using Github Action to achieve the best QoS (Quality Of Service).
The directory spec/ includes specification which serves the dual purposes of examples and functional tests.
Set your secret API key in your shell before running a test.
```bash
export API_KEY="your_secret_key"
```
Install testing dependency
```bash
$ make
```
Contributions are welcome. Feel to submit a pull request!
## License
MIT License.