-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavailability.go
206 lines (175 loc) · 5.56 KB
/
availability.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
package dvcscraper
import (
"encoding/json"
"fmt"
"time"
"github.com/go-rod/rod"
)
const (
bookingPage = "https://disneyvacationclub.disney.go.com/booking/"
calendarURL = "https://disneyvacationclub.disney.go.com/booking-api/api/v1/calendar-availability"
calendarPickerMonthSelector = ".mobCoreDatepickerRange ul.carousel-wrapper li.carousel-slide"
calendarPickerDaySelector = "td[data-date='%s']"
deluxeStudioButtonSelector = "#mobBookingRoomType button[data-capacity='deluxe-studio']"
closeTermsSelector = "button#closeTermsOfUse"
checkAvailabilityButtonSelector = "button#checkAvailabilityBtn"
uiDateFormat = "01/02/2006"
dateFormat = "2006-01-02"
)
// AvailabilityOptions configure an availability request
type AvailabilityOptions struct {
Resort string `json:"resort"`
RoomType string `json:"roomType"`
Date time.Time `json:"startDate"`
}
type AvailabilityResults struct {
ResortCode string `json:"resortCode"`
RoomCode string `json:"roomCode"`
Availability []struct {
Date string `json:"date"`
Rooms int `json:"rooms"`
Points int `json:"points"`
} `json:"availability"`
}
type CalendarRequestBody struct {
Resort string `json:"resort"`
RoomType string `json:"roomType"`
StartDate string `json:"startDate"`
EndDate string `json:"endDate"`
ParentID *struct{} `json:"parentId"`
Accessible bool `json:"accessible"`
IsModify bool `json:"isModify"`
}
type AvailabilityHandle struct {
page *rod.Page
}
func (s *Scraper) NewAvailabilityHandle() (*AvailabilityHandle, error) {
handle := AvailabilityHandle{}
page, err := s.getPage()
if err != nil {
err = fmt.Errorf("failed to get page: %w", err)
return &handle, err
}
s.logger.Println("got page for avail")
handle.page = page
err = s.AuthenticatedNavigate(bookingPage, closeTermsSelector)
if err != nil {
err = fmt.Errorf("failed to navigate to booking page: %w", err)
return &handle, err
}
s.logger.Println("navigated to booking page for avail")
err = s.click(page, closeTermsSelector)
if err != nil {
err = fmt.Errorf("failed to click close terms button: %w, moving on", err)
s.logger.Println(err.Error())
} else {
s.logger.Println("Clicked close terms button")
}
startDate, endDate := bookingDates()
startSelector := calendarPickerMonthSelector + " " + fmt.Sprintf(calendarPickerDaySelector, startDate)
err = s.click(page, startSelector)
if err != nil {
err = fmt.Errorf("failed to click start date (%s): %w", startDate, err)
return &handle, err
}
s.logger.Println("Clicked start date")
endDateSelector := calendarPickerMonthSelector + " " + fmt.Sprintf(calendarPickerDaySelector, endDate)
err = s.click(page, endDateSelector)
if err != nil {
err = fmt.Errorf("failed to click end date (%s): %w", endDate, err)
return &handle, err
}
s.logger.Println("Clicked end date")
err = s.click(page, deluxeStudioButtonSelector)
if err != nil {
err = fmt.Errorf("failed to click deluxe studio button: %w", err)
return &handle, err
}
s.logger.Println("Clicked studio button")
err = s.click(page, checkAvailabilityButtonSelector)
if err != nil {
err = fmt.Errorf("failed to click check availability button: %w", err)
return &handle, err
}
s.logger.Println("Clicked check availability button")
err = page.WaitLoad()
if err != nil {
err = fmt.Errorf("failed to wait for search page to load: %w", err)
return &handle, err
}
s.logger.Println("Waited loading")
return &handle, nil
}
func (h *AvailabilityHandle) GetAvailability(opts AvailabilityOptions) (AvailabilityResults, error) {
results := AvailabilityResults{}
page := h.page
start, end := startEnd(opts.Date)
body := CalendarRequestBody{
Resort: opts.Resort,
RoomType: opts.RoomType,
StartDate: start.Format(dateFormat),
EndDate: end.Format(dateFormat),
}
obj, err := page.Evaluate(&rod.EvalOptions{
AwaitPromise: true,
ByValue: true,
UserGesture: true,
ThisObj: nil,
JS: getAvailJS,
JSArgs: []interface{}{
calendarURL,
body,
},
})
if err != nil {
err = fmt.Errorf("failed to Evaluate: %w", err)
return results, err
}
err = json.Unmarshal([]byte(obj.Value.JSON("", "")), &results)
if err != nil {
err = fmt.Errorf("failed to unmarshal results: %w -- %s", err, obj.Value.JSON("", ""))
return results, err
}
return results, nil
}
func bookingDates() (string, string) {
initial := time.Now().AddDate(0, 7, 0)
loc := initial.Location()
y, m, _ := initial.Date()
startOfMonth := time.Date(y, m, 1, 0, 0, 0, 0, loc)
startDate := startOfMonth.Format(uiDateFormat)
endDate := startOfMonth.AddDate(0, 0, 5).Format(uiDateFormat)
return startDate, endDate
}
func startEnd(in time.Time) (time.Time, time.Time) {
loc := in.Location()
y, m, _ := in.Date()
startOfMonth := time.Date(y, m, 1, 0, 0, 0, 0, loc)
startDate := startOfMonth
endDate := startOfMonth.AddDate(0, 1, -1)
if startDate.Before(time.Now()) {
startDate = time.Now()
}
if endDate.Month() == time.Now().AddDate(0, 11, 0).Month() {
endDate = time.Now().AddDate(0, 11, 6)
}
return startDate, endDate
}
const getAvailJS = `(url, body) => {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000)
return fetch(url, {
signal: controller.signal,
method: "POST",
headers: {
Accept: "application/json, text/plain, */*",
"Accept-Language": "en-US,en;q=0.5",
"Content-Type": "application/json;charset=utf-8",
ADRUM: "isAjax:true",
Pragma: "no-cache",
"Cache-Control": "no-cache",
},
body: JSON.stringify(body),
}).then(r => r.json()).catch(error => error.message)
}
`