-
Notifications
You must be signed in to change notification settings - Fork 2
/
playwright-script.js
162 lines (129 loc) · 6.07 KB
/
playwright-script.js
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
const { chromium } = require('playwright');
const dotenv = require('dotenv');
dotenv.config();
const date = new Date()
date.setDate(date.getDate() + 7)
const dateString = date.toLocaleDateString("sv", {timeZone: "Europe/London"})
console.log(dateString)
const courtIds = [
'f51042cf-95c8-4aca-acdf-0206636b5db0',
'd5c0b45a-45ab-47d6-acf7-3aec131f07a0',
'69957309-b814-4c24-9bda-a06a79a7e144',
'6d4b1751-6a4f-4263-b46e-600fd3e95f01',
'246e82df-4e7c-4c03-9377-06051aeaa766',
'94c48894-e669-4cd8-80cc-d092f468dd08',
'5e9109b2-4e1e-40d1-9aac-bdb00dbf2c60',
'8d26144f-7486-4046-a006-8b8c4f7900ac'
]
const weekdayPreferences = [
1080,
1020,
1140
];
const weekendPreferences = [
600,
540,
660
];
const weekendDays = [6, 0];
const preferences = weekendDays.includes(date.getDay()) ? weekendPreferences : weekdayPreferences;
(async () => {
const browser = await chromium.launch()
const context = await browser.newContext();
const page = await context.newPage()
await login(page)
const sessions = await getSessions(page);
let preference = null;
let bothHourCourtIds = [];
let firstHourCourtIds = [];
let secondHourCourtIds = [];
for (preference of preferences) {
firstHourCourtIds = courtIds.filter(courtId => sessions.some(session => session.courtId === courtId && session.startTime === preference.toString(10)))
secondHourCourtIds = courtIds.filter(courtId => sessions.some(session => session.courtId === courtId && session.startTime === (preference + 60).toString(10)))
bothHourCourtIds = firstHourCourtIds.filter(firstHourCourtId => secondHourCourtIds.includes(firstHourCourtId))
console.log(preference)
console.log(firstHourCourtIds)
console.log(secondHourCourtIds)
console.log(bothHourCourtIds)
if (!bothHourCourtIds.length && (!firstHourCourtIds.length || !secondHourCourtIds.length)) {
console.log(`Insufficient sessions found for ${preference}`)
continue
}
break
}
if (!firstHourCourtIds.length || !secondHourCourtIds.length) {
console.log("Insufficent sessions found");
browser.close()
}
if (bothHourCourtIds.length) {
console.log('we have a double')
const session = sessions.find(session => session.startTime === preference.toString() && session.courtId === bothHourCourtIds[0])
await book(page, bookingPageUrl(bothHourCourtIds[0], dateString, session.sessionId, preference, preference + 120))
console.log("Double booked")
} else if (firstHourCourtIds.length && secondHourCourtIds.length) {
console.log('going for two singles')
const firstSession = sessions.find(session => session.startTime === preference.toString() && session.courtId === firstHourCourtIds[0])
const secondSession = sessions.find(session => session.startTime === (preference + 60).toString() && session.courtId === secondHourCourtIds[0])
await book(page, bookingPageUrl(firstHourCourtIds[0], dateString, firstSession.sessionId, preference, preference + 60))
await book(page, bookingPageUrl(secondHourCourtIds[0], dateString, secondSession.sessionId, preference + 60, preference + 120))
console.log("Two singles booked")
}
browser.close()
})()
const login = async page => {
await page.goto('https://clubspark.lta.org.uk/FinsburyPark/Account/SignIn?returnUrl=%2FFinsburyPark%2FBooking%2FBookByDate')
const ltaLoginSelector = 'button.lta'
await page.locator(ltaLoginSelector).click()
const usernameSelector = 'input[placeholder=Username]'
await page.locator(usernameSelector).fill(process.env.LTA_USERNAME)
const passwordSelector = 'input[placeholder=Password]'
await page.locator(passwordSelector).fill(process.env.LTA_PASSWORD)
const submitSelector = 'button[title=Login]'
await page.locator(submitSelector).click()
const sessionsSelector = '.booking-sheet'
return await page.waitForSelector(sessionsSelector)
}
const getSessions = async (page) => {
const url = `https://clubspark.lta.org.uk/FinsburyPark/Booking/BookByDate#?date=${dateString}`;
console.log(url)
await page.goto(url);
const sessionsSelector = '.booking-sheet';
await page.waitForSelector(sessionsSelector);
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
await delay(3000)
const sessions = await page.evaluate(() => {
return Array.from(document.querySelectorAll('[data-availability=true] .book-interval'))
.map((node) => {
const courtId = node.dataset.testId.split('booking-')[1].split('|')[0];
const startTime = node.dataset.testId.split('|')[2];
const sessionId = node.parentNode.parentNode.dataset.sessionId;
return { courtId, startTime, sessionId };
});
});
console.log(sessions);
console.log(sessions.length)
return sessions;
}
const book = async (page, url) => {
console.log(url)
await page.goto(url)
const confirmationButtonSelector = 'button#paynow'
await page.locator(confirmationButtonSelector).click()
await page.getByText('Card number').click();
await page.frameLocator('iframe[title="Secure card number input frame"]').getByPlaceholder('1234 1234 1234 1234').fill(process.env.CC_NUMBER);
await page.getByText('MM/YY').click();
await page.frameLocator('iframe[title="Secure expiration date input frame"]').getByPlaceholder('MM / YY').fill(process.env.CC_EXPIRY);
await page.getByText('CVC').click();
await page.frameLocator('iframe[title="Secure CVC input frame"]').getByPlaceholder('CVC').fill(process.env.CC_CVC);
await page.locator('button[type=submit]').click()
return await page.waitForNavigation()
}
const bookingPageUrl = (courtId, date, sessionId, startTime, endTime) => {
return `https://clubspark.lta.org.uk/FinsburyPark/Booking/Book?` +
`Contacts%5B0%5D.IsPrimary=true&` +
`ResourceID=${courtId}&` +
`Date=${date}&` +
`SessionID=${sessionId}&` +
`StartTime=${startTime.toString()}&` +
`EndTime=${endTime.toString()}`
}