-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
383 lines (303 loc) · 14.7 KB
/
index.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/**
* Batch size for processing arrays in chunks.
* @constant {number}
*/
const batchSize = 2;
/**
* Performs a GET request to a specified URL.
* This function is tailored for Procare's API, including authorization and custom headers.
*
* @async
* @param {string} url - The URL to send the GET request to.
* @returns {Promise<Object>} The response data in JSON format.
*/
const curl = async (url) => {
// Retrieve user auth token from local storage
var auth_token = JSON.parse(JSON.parse(localStorage["persist:kinderlime"]).currentUser).data.auth_token;
// Modify the current host for API request
let apiHost = location.host.replace('schools','api-school');
// Execute fetch request with custom headers including authorization
let response = await fetch(url, {
"headers": {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"Authorization": "Bearer " + auth_token,
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"DNT": 1,
"Host": apiHost,
"Origin": location.origin,
"Pragma": "no-cache",
"Referer": location.origin,
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "no-cors",
"Sec-Fetch-Site": "cross-site",
"Sec-GPC": "1",
"TE": "trailers",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0"
}
})
// Parse and return JSON response
let data = await response.json();
return data;
}
/**
* Retrieves a list of children associated with the user.
* @async
* @returns {Promise<Array>} An array of children from the API.
*/
const listChildren = async () => {
// get the actual subdomain we need to use here
let apiHost = location.host.replace('schools','api-school');
// set up the actual URL to hit
let url = `https://${apiHost}/api/web/parent/kids/`
// get information from it
let responseData = await curl(url);
return responseData;
}
/**
* Recursively fetches child data for all pages.
* @async
* @param {string} childId - The child's ID.
* @param {number} [page=1] - Page number for paginated data.
* @param {string} [date_from="2000-01-01"] - Start date for the data.
* @param {string} [date_to="2031-07-30"] - End date for the data.
* @param {Array} [data=[]] - Array to accumulate results.
* @returns {Promise<Array>} The complete dataset for the child.
*/
const extractChildData = async (childId, page, date_from, date_to, data) => {
// Initialize page number if not provided
if (!page) {
page = 1;
}
// Set a default future end date if not provided
if (!date_to) {
date_to = "2031-07-30";
}
// Set a default past start date if not provided
if (!date_from) {
date_from = "2000-01-01";
}
// Modify the current host to form the API endpoint
let apiHost = location.host.replace('schools', 'api-school');
// Construct the URL for the API request
let url = `https://${apiHost}/api/web/parent/daily_activities/?kid_id=${childId}&filters%5Bdaily_activity%5D%5Bdate_to%5D=${date_to}&filters%5Bdaily_activity%5D%5Bdate_from%5D=${date_from}&page=${page}`;
// Make an API call using the constructed URL
let info = await curl(url);
// Check if there are activities returned for the current page
if (info.daily_activities.length !== 0) {
// Add the fetched activities to the accumulated data
info = data.concat(info.daily_activities);
document.querySelector("#marquee").innerText = `Collecting Info From Page ${page}`;
console.log(info);
page++;
// Recursively call function for the next page
return extractChildData(childId, page, date_from, date_to, info);
} else {
// Resolve the promise with the accumulated data when no more activities are found
return new Promise((resolve, reject) => { resolve(data); });
}
};
/**
* Extracts file extension from a URL.
* @param {string} url - URL to extract the file extension from.
* @returns {string} The file extension.
*/
const getFileExtension = (url) => new URL(url).pathname.split('.').pop();
/**
* Fetches a file from a URL and downloads it, naming it according to provided metadata.
* @async
* @param {string} url - The URL of the file to download.
* @param {Object} metadata - Metadata object containing 'created_at' and 'id' properties.
* @returns {Promise<boolean>} True if download is successful, false otherwise.
*/
const fetchAndDownload = async (url, metadata) => {
// Extract date from metadata and format it
let date_of = metadata.created_at.replace(/\..*$/, "");
// Extract ID from metadata
let id = metadata.id;
// Get the file extension from the URL
let extension = getFileExtension(url);
// Default to 'mpeg' if the extension is too long
if (extension.length > 5) {
extension = "mpeg";
}
// Construct the filename using date, id, and file extension
let filename = `${date_of}_${id}.${extension}`;
try {
// Fetch the file from the URL
const response = await fetch(url);
// Throw an error if the fetch operation fails
if (!response.ok) throw new Error(`Failed to fetch ${url}: ${response.statusText}`);
// Create a blob from the response data
const blob = await response.blob();
// Create a download link for the blob
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.setAttribute("download", filename);
// Trigger the file download
link.click();
// Clean up by removing the link and revoking the created URL
link.remove();
URL.revokeObjectURL(link.href);
// Return true indicating the download was successful
return true;
} catch (error) {
// Log and return false in case of an error
console.error(`Error in fetchAndDownload:`, error);
return false;
}
}
/**
* Splits an array into chunks of a specified size.
* @param {Array} array - The array to split.
* @param {number} chunkSize - The size of each chunk.
* @returns {Array<Array>} An array of chunks.
*/
const chunkArray = (array, chunkSize) => {
const chunks = [];
for (let i = 0; i < array.length; i += chunkSize) {
chunks.push(array.slice(i, i + chunkSize));
}
return chunks;
};
/**
* Executes a list of tasks in batches, ensuring that a certain number of tasks are always running in parallel.
*
* @param {Array<Function>} tasks - An array of tasks to be executed. Each task is expected to be a function returning a Promise.
* @param {number} batchSize - The maximum number of tasks to run in parallel.
* @returns {Promise<Array>} A promise that resolves with an array of results from the executed tasks.
*/
const executeInBatches = async (tasks, batchSize) => {
let active = []; // Array to keep track of active promises
let results = []; // Array to store results of all tasks
for (const task of tasks) {
// Determine the URL to fetch based on whether the item is a video or not
const taskURL = task.is_video ? task.video_file_url : task.main_url;
// Create a promise for each task
const promise = fetchAndDownload(taskURL, task).then(result => {
// Once a promise is resolved, remove it from the active array
active = active.filter(p => p !== promise);
return result;
});
// Add the newly created promise to the active and results arrays
active.push(promise);
results.push(promise);
// If the number of active promises reaches the batch size, wait for one to complete before continuing
if (active.length >= batchSize) {
await Promise.race(active);
}
}
// Wait for all promises to complete and return their results
return Promise.all(results);
};
/**
* Main function to orchestrate the fetching, processing, and downloading of children's data.
* Interacts with the UI to provide feedback on the process.
* @async
* @returns {Promise<void>} A promise that resolves when the entire process is completed.
*/
const main = async () => {
const startDate = document.querySelector("#start_date").value;
const endDate = document.querySelector("#end_date").value;
// Step 1: Retrieve list of children
const children = await listChildren();
// Step 2: Collect and compile data for each child
document.querySelector("#marquee").innerText = "Collecting Data";
let data = await Promise.all(
children.kids.map(x => extractChildData(x.id, 1, startDate, endDate, []))
);
// Flatten the array to merge all children's data into a single array
data = data.flat();
// Create and download a blob containing the compiled data
const blob = new Blob([JSON.stringify(data, null, "\t")], {
type: "text/json;charset=utf8;"
});
const link = document.createElement("a");
const dateString = new Date().toISOString();
link.href = window.URL.createObjectURL(blob);
link.download = `${dateString}_primrose.json`;
link.click();
// Step 3: Filter events to include only photos and videos
const multiMedia = data
.filter(x => x.activity_type === "photo_activity" || x.activity_type === "video_activity")
.map(x => x.activiable);
document.querySelector("#marquee").innerText = `Downloading ${multiMedia.length} files...`;
// Step 4: Download multimedia content in batches
await executeInBatches(multiMedia, batchSize);
alert("DONE!");
};
document.querySelector("body").innerHTML = `
<style>
p{padding: 2.5px;}
a{color: blue;}
.section{
margin-left: 0px;
}
.carer-dashboard__section {
width: 100%;
padding-right: 16px;
background: #f4f4f4;
}
</style>
<div id="root-app">
<div class="app">
<div class="app__inner">
<!-- HEADER -->
<header class="topbar"></header>
<!-- MAIN SECTION -->
<section class="section">
<div class="carer-dashboard">
<div class="carer-dashboard__section">
<div class="carer-dashboard__content">
<!-- PREAMBLE -->
<div style="text-align: center;">
<h1 style="padding-top:15px;font-size:75px;color:grey;" data-bind="text: 'procare'">procare excavator</h1>
<h3 style="padding-top:15px;font-size:25px;color:grey;margin-top: 25px; margin-bottom: 25px;">Questions, Comments, Concerns? <br />Feel Free to Contact me::</h3>
<p>source code: <a href="https://github.com/JWally/procare-media-downloader">GitHub</a></p>
<p>e-mail: <a href="mailto: [email protected]">[email protected]</a></p>
<p>linkedin: <a href="https://www.linkedin.com/in/justinwwolcott/">https://www.linkedin.com/in/justinwwolcott/</a></p>
</div>
<!-- FORM -->
<div style="text-align: center; margin-top: 30px; margin-bottom: 30px; height: reset;">
<div style="justify-content: center;">
<div class="form__row form--inline" style="justify-content: center;">
<!-- START DATE -->
<div class="form-date">
<div class="datepicker__wrapper">
<div class="tooltip tooltip--left tooltip--no-arrow tooltip--white datepicker">
<div class="tooltip-trigger">
<input value="2022-01-01" type="date" id="start_date" style="border: none; height: 40px; font-size: large; border-radius: 10px; text-align: center;">
</div>
</div>
</div>
</div>
<span class="form__row-gap">up to</span>
<!-- END DATE -->
<div class="form-date">
<div class="datepicker__wrapper">
<div class="tooltip tooltip--left tooltip--no-arrow tooltip--white datepicker">
<div class="tooltip-trigger">
<input value="2099-12-31" type="date" id="end_date" style="border: none; height: 40px; border-radius: 10px; font-size: large; text-align: center">
</div>
</div>
</div>
</div>
<a onclick="main();" class="button carer-dashboard__button--pay" style="width:20%; margin-left: 10px; display: table;" id="start-the-reactor-quaide">
<span style="display: table-cell; vertical-align: middle; font-size: 25px;">EXTRACT</span>
</a>
</div>
</div>
</div>
<div style="text-align: center;">
<h1 style="padding-top:15px;font-size:75px;color:red;" id="marquee">- - - </h1>
</div>
</div>
</div>
</div>
</section>
</div>
</app>
</div>
`;