-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetVisits.js
53 lines (41 loc) · 1.42 KB
/
getVisits.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
import fs from 'node:fs';
const pat = fs.readFileSync('./pat').toString().trim();
async function fetchDataUntilDate (targetDate) {
let today = new Date().toISOString().split('T')[0]; // Get today's date in YYYY-MM-DD format
let url = `https://www.recurse.com/api/v1/hub_visits?start_date=${targetDate}&end_date=${today}&page=1`;
let valetVisits = [];
let page = 1;
while (true) {
let response = await fetch(url, {
headers: {
'Authorization': `Bearer ${pat}`
}
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
let visits = await response.json();
if (visits.length > 0) {
for (let visit of visits) {
if (visit.created_by_app === 'V.A.L.E.T.' || visit.updated_by_app === 'V.A.L.E.T.') {
valetVisits.push(visit);
}
}
// Set the next URL for pagination
page += 1;
url = `https://www.recurse.com/api/v1/hub_visits?start_date=${targetDate}&end_date=${today}&page=${page}`;
} else {
break;
}
}
return valetVisits;
}
let date = Date.parse(process.argv[2]);
let since = '2023-08-03';
if (date) {
since = new Intl.DateTimeFormat('en-US', {year:'numeric', month:'2-digit', day:'2-digit', timeZone:'UTC'}).format(date);
console.log(since);
}
fetchDataUntilDate(since)
.then(data => console.log(`Total V.A.L.E.T. visits since ${new Date(since).toDateString()}: ${data.length}`))
.catch(error => console.error("Error fetching data: ", error));