-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
123 lines (112 loc) · 3.33 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
package zillow
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
type Client struct {
httpClient *http.Client
}
type SearchPropertiesResponse struct {
Results []struct {
Display string
ResultType string
MetaData struct {
AddressType string
StreetNumber string
StreetName string
City string
State string
Country string
ZipCode string
Zpid int
Lat float64
Lng float64
MaloneId int
}
}
}
type PropertyLookupResponse struct {
LookupResults []struct {
Zpid int
Estimates struct {
Zestimate int64
RentZestimate int64
}
}
}
func NewClient(httpClient *http.Client) *Client {
return &Client{httpClient}
}
func (c *Client) SearchProperties(address string) (*SearchPropertiesResponse, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("https://www.zillowstatic.com/autocomplete/v3/suggestions?q=%s", url.QueryEscape(address)), nil)
if err != nil {
return nil, fmt.Errorf("error building zillow api request: %w", err)
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("error querying zillow api: %w", err)
}
defer resp.Body.Close()
var result *SearchPropertiesResponse
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading zillow api response: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("error querying zillow api: %s", string(b))
}
err = json.Unmarshal(b, &result)
if err != nil {
return nil, fmt.Errorf("error json decoding zillow api response: %w", err)
}
return result, nil
}
func (c *Client) LookupProperty(propertyId int) (*PropertyLookupResponse, error) {
payload := map[string]interface{}{
"homeDetailsUriParameters": map[string]interface{}{
"googleMaps": false,
"platform": "iphone",
"showFactsAndFeatures": true,
"streetView": false,
},
"listingCategoryFilter": "all",
"propertyIds": []int{propertyId},
"sortAscending": true,
"sortOrder": "recentlyChanged",
}
payloadBuf := new(bytes.Buffer)
err := json.NewEncoder(payloadBuf).Encode(payload)
if err != nil {
return nil, fmt.Errorf("error encoding json for zillow api request: %w", err)
}
req, err := http.NewRequest("POST", "https://zm.zillow.com/api/public/v2/mobile-search/homes/lookup", payloadBuf)
if err != nil {
return nil, fmt.Errorf("error building zillow api request: %w", err)
}
req.Header.Set("User-Agent", "ZillowMap/15.12.0.0 CFNetwork/1240.0.4 Darwin/20.5.0")
req.Header.Set("X-Client", "com.zillow.ZillowMap")
req.Header.Set("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("error querying zillow api: %w", err)
}
defer resp.Body.Close()
var result *PropertyLookupResponse
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading zillow api response: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("error querying zillow api: %s", string(b))
}
err = json.Unmarshal(b, &result)
if err != nil {
return nil, fmt.Errorf("error json decoding zillow api response: %w", err)
}
return result, nil
}