-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
134 lines (116 loc) · 3.32 KB
/
main.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
func main() {
log.Print("Looking for an OFFERZEN_TOKEN.")
token := os.Getenv("OFFERZEN_TOKEN")
if token == "" {
panic("Please set the OFFERZEN_TOKEN.")
}
oc := &offerzenClient{
http: &http.Client{},
baseURL: "https://www.offerzen.com/api",
token: token,
}
log.Print("Getting offerzen company metadata.")
meta, err := oc.getPublicProfiles(1)
if err != nil {
panic("Error with retrieving company list from offerzen.")
}
log.Print("Scraping offerzen company data.")
var cl []company
for i := 1; i <= meta.PaginationInfo.TotalPages; i++ {
log.Println("Retrieving page ", i)
ocl, err := oc.getPublicProfiles(i)
if err != nil {
panic("Error with retrieving company list from offerzen.")
}
cl = append(cl, ocl.Result...)
time.Sleep(1 * time.Second) // Don't request the offerzen api.
}
o := struct {
Companies []company
}{cl}
b, err := json.MarshalIndent(&o, "", " ")
if err != nil {
panic("Error with marshalling offerzen data as json.")
}
err = ioutil.WriteFile("offerzendata.json", b, 0644)
if err != nil {
panic("Error with writing offerzendata.json.")
}
}
// offerzenClient is the http client for accessing the offerzen public api.
type offerzenClient struct {
http *http.Client
baseURL string
token string
}
func (oc offerzenClient) getPublicProfiles(page int) (*publicProfileResp, error) {
url := fmt.Sprintf("%s/company/public_profiles?page=%d", oc.baseURL, page)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("authorization", oc.token)
res, err := oc.http.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
return nil, errors.New("invalid request to offerzen api")
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var ppr publicProfileResp
err = json.Unmarshal(b, &ppr)
if err != nil {
return nil, err
}
return &ppr, nil
}
// publicProfileResp is a response when using the api/company/public_profiles endpoint.
// Generated with https://mholt.github.io/json-to-go/
type publicProfileResp struct {
Success bool `json:"success"`
Result []company `json:"result"`
PaginationInfo struct {
CurrentPage int `json:"current_page"`
TotalPages int `json:"total_pages"`
ItemsPerPage int `json:"items_per_page"`
TotalItems int `json:"total_items"`
} `json:"pagination_info"`
}
// company is an offerzen representation of a company.
type company struct {
ID string `json:"id"`
Name string `json:"name"`
LogoURL string `json:"logo_url"`
BrandColor string `json:"brand_color"`
ElevatorPitch string `json:"elevator_pitch"`
Address string `json:"address"`
NumberOfEmployees string `json:"number_of_employees"`
URL string `json:"url"`
WebsiteURL string `json:"website_url"`
FullWebsiteURL string `json:"full_website_url"`
Cities []interface{} `json:"cities"`
TechStack []struct {
ID string `json:"id"`
Title string `json:"title"`
} `json:"tech_stack"`
Perks []struct {
ID string `json:"id"`
Title string `json:"title"`
Icon string `json:"icon"`
} `json:"perks"`
}