-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutscraper.go
94 lines (72 loc) · 2.45 KB
/
outscraper.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
package outscraper
import (
"fmt"
"net/url"
"net/http"
"encoding/json"
"errors"
)
const (
ApiUrl string = "https://api.app.outscraper.com"
)
type Client struct {
ApiKey string
}
type Request map[string]string
type Result map[string]interface{}
type ResultArray []interface{}
func (c Client) getAPIRequest(path string, parameters map[string]string) (Result, error) {
httpClient := &http.Client{}
url, _ := url.Parse(ApiUrl + path)
query := url.Query()
if parameters != nil {
for k, v := range parameters {
query.Set(k, v)
}
}
url.RawQuery = query.Encode()
fmt.Println(url.String())
req, _ := http.NewRequest("GET", url.String(), nil)
req.Header.Add("X-API-KEY", c.ApiKey)
req.Header.Add("Client", "Go SDK")
response, doError := httpClient.Do(req)
if doError != nil {
return nil, errors.New("Cannot create request")
}
defer response.Body.Close()
decoder := json.NewDecoder(response.Body)
var result Result
decodeError := decoder.Decode(&result)
if decodeError != nil {
return nil, errors.New("cannot decode")
}
return result, nil
}
func (c Client) GetRequestArchive(requestId string) (Result, error) {
return c.getAPIRequest("/requests/"+ requestId, map[string]string {})
}
func (c Client) GoogleSearch(parameters map[string]string) ([]interface{}, error) {
parameters["async"] = "false"
response, err := c.getAPIRequest("/google-search-v3", parameters)
return response["data"].([]interface{}), err
}
func (c Client) GoogleMapsSearch(parameters map[string]string) ([]interface{}, error) {
parameters["async"] = "false"
response, err := c.getAPIRequest("/maps/search-v2", parameters)
return response["data"].([]interface{}), err
}
func (c Client) GoogleMapsReviews(parameters map[string]string) ([]interface{}, error) {
parameters["async"] = "false"
response, err := c.getAPIRequest("/maps/reviews-v3", parameters)
return response["data"].([]interface{}), err
}
func (c Client) EmailsAndContacts(parameters map[string]string) ([]interface{}, error) {
parameters["async"] = "false"
response, err := c.getAPIRequest("/emails-and-contacts", parameters)
return response["data"].([]interface{}), err
}
func (c Client) PhonesEnricher(parameters map[string]string) ([]interface{}, error) {
parameters["async"] = "false"
response, err := c.getAPIRequest("/phones-enricher", parameters)
return response["data"].([]interface{}), err
}