-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
266 lines (218 loc) · 5.8 KB
/
client.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
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
package funda
import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strconv"
)
const baseURL = "https://mobile.funda.io/api/v1"
type searchResultItem struct {
ItemType int `json:"ItemType"`
GlobalID int `json:"GlobalId"`
Link string `json:"Link"`
Fotos []foto `json:"Fotos"`
Info []info `json:"Info"`
}
type info struct {
Line []houseResponseItemList `json:"Line"`
}
type foto struct {
Link string `json:"Link"`
}
type searchResult []searchResultItem
type houseResponseItem struct {
URL string `json:"URL"`
List []json.RawMessage `json:"List"`
Section int `json:"Section"`
}
type houseResponseItemList struct {
Label string `json:"Label"`
Value string `json:"Value"`
Text string `json:"Text"`
List []json.RawMessage `json:"List"`
}
type houseResponse []houseResponseItem
// Client defines an HTTP client to the Funda API.
type Client struct {
HTTPClient *http.Client
BaseURL string
APIKey string
}
// NewClient initialises and returns a new Client.
func NewClient(apiKey string) *Client {
return &Client{
HTTPClient: http.DefaultClient,
BaseURL: baseURL,
APIKey: apiKey,
}
}
func (c *Client) newRequest(method, url string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
req.Header.Set("accepted_cookie_policy", "10")
req.Header.Set("api_key", c.APIKey)
req.Header.Set("User-Agent", "Funda/2.17.0 (com.funda.two; build:80; Android 25) okhttp/3.5.0")
req.Header.Set("Cookie", "X-Stored-Data=null; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=/; samesite=lax; httponly")
req.Header.Set("Accept", "application/json")
req.Header.Set("Accept-Language", "nl-NL")
return req, nil
}
func (c *Client) fundaSearchURL(searchOpts string, page, pageSize int) (*url.URL, error) {
u, err := url.Parse(c.BaseURL + "/Aanbod/koop" + searchOpts)
if err != nil {
return nil, err
}
q := url.Values{}
q.Set("page", strconv.Itoa(page))
q.Set("pageSize", strconv.Itoa(pageSize))
u.RawQuery = q.Encode()
return u, nil
}
// Search does a house search request at the Funda API.
func (c *Client) Search(searchOpts string, page, pageSize int) ([]*House, error) {
req, err := c.newRequest("GET", "", nil)
if err != nil {
return nil, fmt.Errorf("funda: could not create http request: %e", err)
}
u, err := c.fundaSearchURL(searchOpts, page, pageSize)
if err != nil {
return nil, fmt.Errorf("funda: could not parse search URL: %v", err)
}
req.URL = u
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, fmt.Errorf("funda: could not execute http request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf(
"funda: unexpected HTTP response code (%d) received",
resp.StatusCode,
)
}
houses, err := c.housesFromSearchResult(resp.Body)
if err != nil {
return nil, fmt.Errorf(
"funda: could not parse houses from search result: %v",
err,
)
}
return houses, nil
}
func (c *Client) housesFromSearchResult(r io.Reader) ([]*House, error) {
var result searchResult
if err := json.NewDecoder(r).Decode(&result); err != nil {
return nil, err
}
var houses []*House
for _, item := range result {
// Skip highlighted houses (ads).
if item.ItemType != 1 {
continue
}
if len(item.Fotos) < 1 {
return nil, errors.New("result does not have photos")
}
if len(item.Info) < 4 {
return nil, errors.New("result does not have enough info values")
}
for _, info := range item.Info {
if len(info.Line) < 1 {
return nil, errors.New("result does not have enough info lines")
}
}
house := &House{
ID: item.GlobalID,
Address: item.Info[0].Line[0].Text,
}
imageURL, err := url.Parse(item.Fotos[0].Link)
if err != nil {
return nil, err
}
house.ImageURL = *imageURL
if err := c.populateHouseDetails(house, item.GlobalID); err != nil {
log.Printf("Error: Could not get house (%v): %v", item.GlobalID, err)
continue
}
houses = append(houses, house)
}
return houses, nil
}
func (c *Client) populateHouseDetails(house *House, globalID int) error {
url := fmt.Sprintf("%v/Aanbod/Detail/Koop/%v", c.BaseURL, globalID)
req, err := c.newRequest("GET", url, nil)
if err != nil {
return fmt.Errorf("could not create http request: %e", err)
}
resp, err := c.HTTPClient.Do(req)
if err != nil {
return fmt.Errorf("funda: could not execute http request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf(
"funda: unexpected HTTP response code (%d) received",
resp.StatusCode,
)
}
if err := house.parseDetailsFromAPIResponse(resp.Body); err != nil {
return fmt.Errorf(
"funda: could not parse house from api response: %v",
err,
)
}
return nil
}
func (h *House) parseDetailsFromAPIResponse(r io.Reader) error {
var houseResp houseResponse
if err := json.NewDecoder(r).Decode(&houseResp); err != nil {
return err
}
for _, item := range houseResp {
if item.URL != "" {
houseURL, err := url.Parse(item.URL)
if err != nil {
return err
}
h.URL = *houseURL
}
// Skip photos.
if item.Section == 3 {
continue
}
for _, l := range item.List {
var list houseResponseItemList
if err := json.Unmarshal(l, &list); err != nil {
return err
}
if err := h.parseList(list); err != nil {
return err
}
}
}
return nil
}
func (h *House) parseList(list houseResponseItemList) error {
for _, l := range list.List {
var list houseResponseItemList
if err := json.Unmarshal(l, &list); err != nil {
return err
}
h.parseList(list)
}
switch list.Label {
case "Vraagprijs":
h.Price = list.Value
case "Wonen (= woonoppervlakte)":
h.SurfaceArea = list.Value
case "Aantal kamers":
h.Rooms = list.Value
}
return nil
}