-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
243 lines (184 loc) · 7.41 KB
/
worker.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
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
import { parse } from 'node-html-parser';
export default {
async fetch(request, env) {
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET',
};
if (request.method !== 'GET') {
return new Response('This worker only supports GET requests', {
status: 400,
headers: corsHeaders
})
}
const refuseCollectionJsonKeys = ['Location', 'Area', 'Calendar PDF URL', 'Email Subscribe URL', 'Schedule Identifier', 'Schedule Name', 'Calendar URL'];
const gardenWasteCollectionJsonKeys = ['Location', 'Numbers', 'Area', 'Calendar PDF URL', 'Email Subscribe URL', 'Schedule Identifier', 'Schedule Name', 'Calendar URL'];
const gedlingAppsUrl = env.GEDLING_APPS_URL;
const refuseSearchUrl = new URL('refuse/search.aspx', gedlingAppsUrl);
let refuseCollectionData = [];
let gardenWasteCollectionData = [];
function getAttributeValue(root, selector) {
return parse(root).querySelector(selector).getAttribute('value');
}
function getCollectionIdentifier(url) {
return url.split("/").pop() || null;
}
function formatRefuseCalendarPDFUrl(path) {
return new URL(`/refuse/${path}`, gedlingAppsUrl);
}
function formatGardenCalendarPDFUrl(path) {
return new URL(path, gedlingAppsUrl);
}
function formatCollectionUrl(slug, $isGardenBinType = false) {
let pathName = null;
if ($isGardenBinType) {
pathName = 'garden';
}
else {
pathName = 'refuse';
}
return new URL(`collections/${pathName}/${slug}`, env.BASE_URL);
}
function formatCollectionName(url) {
let urlParsed = url.split("/").pop().split('-');
if (urlParsed.length < 2) {
return null;
}
// First part is the weekday
let weekDay = urlParsed[0].replace(/\b[a-z]/g, function(letter) {
return letter.toUpperCase();
});
// Second part is the collection schedule name
let schedule = urlParsed[1].toUpperCase();
return `${weekDay} ${schedule}`;
}
const url = new URL(request.url);
const streetName = url.searchParams.get('streetName');
if (!streetName) {
return new Response('Missing street name parameter.', {
status: 400,
headers: corsHeaders
});
}
if (streetName.length < 5) {
return new Response('Street name query should be 5 or more characters.', {
status: 400,
headers: corsHeaders
});
}
if (streetName.match(/\d+/)) {
return new Response('For more accurate results, please enter only street name values, no property numbers or other address information.', {
status: 400,
headers: corsHeaders
});
}
// Make GET request to search page to get ASP.NET hidden input values required for POST request
const searchPageFormData = await fetch(refuseSearchUrl)
.then((response) => {
if (!response.ok) {
throw new Error(`Failed to fetch ${refuseSearchUrl}. HTTP error: ${response.status} ${response.statusText}.`);
}
return response.text();
})
.then((data) => {
// Parse the input values needed
return {
'__VIEWSTATE': getAttributeValue(data, 'input#__VIEWSTATE'),
'__VIEWSTATEGENERATOR': getAttributeValue(data, 'input#__VIEWSTATEGENERATOR'),
'__EVENTVALIDATION': getAttributeValue(data, 'input#__EVENTVALIDATION')
}
})
.catch((error) => {
console.log(error);
})
// Build POST parameters for search
let formData = new FormData();
for (var key in searchPageFormData) {
formData.append(key, searchPageFormData[key]);
}
// Pass the street value from URL query as form data
formData.append('ctl00$MainContent$street', streetName);
formData.append('ctl00$MainContent$mybutton', 'Search');
const searchRequestResults = await fetch(refuseSearchUrl, {
method: 'POST',
body: formData
}).then((response) => response.text());
let refuseData = parse(searchRequestResults).querySelectorAll('table#ctl00_MainContent_streetgridview tbody tr');
let gardenWasteData = parse(searchRequestResults).querySelectorAll('table#ctl00_MainContent_gardenGridView tbody tr');
if (refuseData.length > 0) {
let refuseSubscribeUrl = null;
refuseData.forEach((row) => {
const cells = row.removeWhitespace().querySelectorAll('td');
const rowData = cells.map(function(cell) {
if (cell.text === 'Download Calendar' || cell.text === 'Subscribe') {
let href = cell.querySelector('a').getAttribute('href');
if (cell.text === 'Download Calendar') {
return formatRefuseCalendarPDFUrl(href);
}
if (cell.text === 'Subscribe') {
refuseSubscribeUrl = href;
return href;
}
}
return cell.text;
});
let identifier = getCollectionIdentifier(refuseSubscribeUrl);
rowData.push(getCollectionIdentifier(identifier));
rowData.push(formatCollectionName(refuseSubscribeUrl));
rowData.push(formatCollectionUrl(identifier));
const rowObject = {};
for (const [key, value] of refuseCollectionJsonKeys.entries()) {
rowObject[value] = rowData[key];
}
refuseCollectionData.push(rowObject);
});
}
if (gardenWasteData.length > 0) {
let gardenSubscribeUrl = null;
gardenWasteData.forEach((row) => {
const cells = row.removeWhitespace().querySelectorAll('td');
const rowData = cells.map(function(cell, index) {
if (cell.text === 'Download Calendar' || cell.text === 'Subscribe') {
let href = cell.querySelector('a').getAttribute('href');
if (cell.text === 'Download Calendar') {
return formatGardenCalendarPDFUrl(href);
}
if (cell.text === 'Subscribe') {
gardenSubscribeUrl = href;
return href;
}
}
return cell.text || null;
});
let identifier = getCollectionIdentifier(gardenSubscribeUrl);
rowData.push(getCollectionIdentifier(identifier));
rowData.push(formatCollectionName(gardenSubscribeUrl));
rowData.push(formatCollectionUrl(identifier, true));
const rowObject = {};
for (const [key, value] of gardenWasteCollectionJsonKeys.entries()) {
rowObject[value] = rowData[key];
}
gardenWasteCollectionData.push(rowObject);
});
}
if (refuseCollectionData.length === 0 && gardenWasteCollectionData.length === 0) {
return new Response('The street name query did not return any bin collection data. Please check the street name entered is valid and within the Gedling Borough Council district and try again.', {
status: 404,
headers: corsHeaders
});
}
return new Response(JSON.stringify({
'streetNameQuery': streetName,
'refuseCollections': refuseCollectionData,
'gardenWasteCollections': gardenWasteCollectionData,
'viewState': searchPageFormData['__VIEWSTATE'],
'viewStateGenerator': searchPageFormData['__VIEWSTATEGENERATOR'] || null,
'eventValidation': searchPageFormData['__EVENTVALIDATION'] || null
}), {
headers: {
'content-type': 'application/json; charset=UTF-8',
...corsHeaders
}
});
},
};