-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
166 lines (154 loc) · 4.14 KB
/
api.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"sync"
"time"
)
type ResStruct struct {
Data struct {
HomeSearch struct {
Results []struct {
Location struct {
Address struct {
City string `json:"city"`
Line string `json:"line"`
PostalCode string `json:"postal_code"`
StateCode string `json:"state_code"`
} `json:"address"`
} `json:"location"`
Description struct {
Sqft int `json:"sqft"`
Beds int `json:"beds"`
Baths int `json:"baths"`
} `json:"description"`
Href string `json:"href"`
ListPrice int `json:"list_price"`
PriceReducedAmount int `json:"price_reduced_amount"`
LastSoldPrice int `json:"last_sold_price"`
ListDate string `json:"list_date"`
Status string `json:"status"`
} `json:"results"`
} `json:"home_search"`
} `json:"data"`
}
type ReqBody struct {
Limit int `json:"limit"`
Offset int `json:"offset"`
PostalCode string `json:"postal_code"`
Status []string `json:"status"`
SortFields struct {
Direction string `json:"direction"`
Field string `json:"field"`
} `json:"sort_fields"`
ListPrice struct {
Min int `json:"min"`
Max int `json:"max"`
} `json:"list_price"`
Beds struct {
Min int `json:"min"`
} `json:"beds"`
Baths struct {
Min int `json:"min"`
} `json:"baths"`
Sqft struct {
Min int `json:"min"`
} `json:"sqft"`
}
func callApi(
searchArea string,
priceMin int,
priceMax int,
bedMin int,
bathMin int,
sqftMin int,
status []string,
numResults int,
ch chan<- string,
wg *sync.WaitGroup,
) {
defer wg.Done()
url := "https://realtor.p.rapidapi.com/properties/v3/list"
reqBody := &ReqBody{
Limit: numResults,
Offset: 0,
PostalCode: searchArea,
Status: status,
SortFields: struct {
Direction string "json:\"direction\""
Field string "json:\"field\""
}{Direction: "desc", Field: "list_date"},
ListPrice: struct {
Min int "json:\"min\""
Max int "json:\"max\""
}{Min: priceMin, Max: priceMax},
Beds: struct {
Min int "json:\"min\""
}{Min: bedMin},
Baths: struct {
Min int "json:\"min\""
}{Min: bathMin},
Sqft: struct {
Min int "json:\"min\""
}{Min: sqftMin},
}
body, err := json.Marshal(reqBody)
if err != nil {
panic(err)
}
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(body))
req.Header.Add("content-type", "application/json")
req.Header.Add("X-RapidAPI-Key", os.Getenv("realtorApiKey"))
req.Header.Add("X-RapidAPI-Host", "realtor.p.rapidapi.com")
res, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
resp, _ := io.ReadAll(res.Body)
err = readJson(resp, ch)
if err != nil {
panic(err)
}
}
func readJson(body []byte, ch chan<- string) error {
res := &ResStruct{}
json.Unmarshal(body, res)
for i := 0; i < len(res.Data.HomeSearch.Results); i++ {
fullStreet := res.Data.HomeSearch.Results[i].Location.Address.Line
city := res.Data.HomeSearch.Results[i].Location.Address.City
state := res.Data.HomeSearch.Results[i].Location.Address.StateCode
zip := res.Data.HomeSearch.Results[i].Location.Address.PostalCode
listDate := res.Data.HomeSearch.Results[i].ListDate
formattedDate, err := formatDate(listDate)
if err != nil {
panic(err)
}
ch <- fmt.Sprintf("%s, %s, %s, %s\nLink: %s\nList Price: %d\nList Date: %s\nStatus: %s\nPrice Reduced Amount: %d\nLast Sold Price: %d\nSqft: %d\nBeds: %d\nBaths: %d\n",
fullStreet,
city,
state,
zip,
res.Data.HomeSearch.Results[i].Href,
res.Data.HomeSearch.Results[i].ListPrice,
formattedDate, res.Data.HomeSearch.Results[i].Status,
res.Data.HomeSearch.Results[i].PriceReducedAmount,
res.Data.HomeSearch.Results[i].LastSoldPrice,
res.Data.HomeSearch.Results[i].Description.Sqft,
res.Data.HomeSearch.Results[i].Description.Beds,
res.Data.HomeSearch.Results[i].Description.Baths)
}
return nil
}
func formatDate(dateString string) (string, error) {
dateObj, err := time.Parse(time.RFC3339, dateString)
if err != nil {
return "", err
}
formattedDate := dateObj.Format("06-01-02")
return formattedDate, nil
}