-
Notifications
You must be signed in to change notification settings - Fork 16
/
search.go
145 lines (122 loc) · 4.34 KB
/
search.go
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
package search
/*
* This package enables to interact with SerpApi search API
*/
import (
"fmt"
"net/http"
"time"
)
// NewSearch create generic search which support any search engine
func NewSearch(engine string, parameter map[string]string, apiKey string) Search {
// Create the http search
httpSearch := &http.Client{
Timeout: time.Second * 60,
}
return Search{Engine: engine, Parameter: parameter, ApiKey: apiKey, HttpSearch: httpSearch}
}
// NewGoogleSearch creates search for google
func NewGoogleSearch(parameter map[string]string, apiKey string) Search {
return NewSearch("google", parameter, apiKey)
}
// NewBingSearch creates search for bing
func NewBingSearch(parameter map[string]string, apiKey string) Search {
return NewSearch("bing", parameter, apiKey)
}
// NewBaiduSearch creates search for baidu
func NewBaiduSearch(parameter map[string]string, apiKey string) Search {
return NewSearch("baidu", parameter, apiKey)
}
// NewYahooSearch creates search for yahoo
func NewYahooSearch(parameter map[string]string, apiKey string) Search {
return NewSearch("yahoo", parameter, apiKey)
}
// NewGoogleMapsSearch creates search for google_maps
func NewGoogleMapsSearch(parameter map[string]string, apiKey string) Search {
return NewSearch("google_maps", parameter, apiKey)
}
// NewGoogleProductSearch creates search for google_product
func NewGoogleProductSearch(parameter map[string]string, apiKey string) Search {
return NewSearch("google_product", parameter, apiKey)
}
// NewGoogleScholarSearch creates search for google_product
func NewGoogleScholarSearch(parameter map[string]string, apiKey string) Search {
return NewSearch("google_scholar", parameter, apiKey)
}
// NewYandexSearch creates search for yandex
func NewYandexSearch(parameter map[string]string, apiKey string) Search {
return NewSearch("yandex", parameter, apiKey)
}
// NewEbaySearch creates search for ebay
func NewEbaySearch(parameter map[string]string, apiKey string) Search {
return NewSearch("ebay", parameter, apiKey)
}
// NewYoutubeSearch creates search for ebay
func NewYoutubeSearch(parameter map[string]string, apiKey string) Search {
return NewSearch("youtube", parameter, apiKey)
}
// NewWalmartSearch creates search for ebay
func NewWalmartSearch(parameter map[string]string, apiKey string) Search {
return NewSearch("walmart", parameter, apiKey)
}
// NewHomeDepotSearch creates search for ebay
func NewHomeDepotSearch(parameter map[string]string, apiKey string) Search {
return NewSearch("home_depot", parameter, apiKey)
}
// NewNaverSearch creates search for Naver search engine
func NewNaverSearch(parameter map[string]string, apiKey string) Search {
return NewSearch("naver", parameter, apiKey)
}
// NewAppleStoreSearch creates search for Apple store (itunes.apple.com)
func NewAppleStoreSearch(parameter map[string]string, apiKey string) Search {
return NewSearch("apple_app_store", parameter, apiKey)
}
// SetApiKey globaly set api_key
func (search *Search) SetApiKey(key string) {
search.ApiKey = key
}
// GetJSON returns SearchResult containing
func (search *Search) GetJSON() (SearchResult, error) {
rsp, err := search.execute("/search", "json")
if err != nil {
return nil, err
}
return search.decodeJSON(rsp.Body)
}
// GetHTML returns html as a string
func (search *Search) GetHTML() (*string, error) {
rsp, err := search.execute("/search", "html")
if err != nil {
return nil, err
}
return search.decodeHTML(rsp.Body)
}
// GetLocation returns the standardize location takes location and limit as input.
func (search *Search) GetLocation(location string, limit int) (SearchResultArray, error) {
search.Parameter = map[string]string{
"q": location,
"limit": fmt.Sprint(limit),
}
rsp, err := search.execute("/locations.json", "json")
if err != nil {
return nil, err
}
return search.decodeJSONArray(rsp.Body)
}
// GetAccount return account information
func (search *Search) GetAccount() (SearchResult, error) {
search.Parameter = map[string]string{}
rsp, err := search.execute("/account", "json")
if err != nil {
return nil, err
}
return search.decodeJSON(rsp.Body)
}
// GetSearchArchive retrieve search from the archive using the Search Archive API
func (search *Search) GetSearchArchive(searchID string) (SearchResult, error) {
rsp, err := search.execute("/searches/"+searchID+".json", "json")
if err != nil {
return nil, err
}
return search.decodeJSON(rsp.Body)
}