Skip to content

Instantly share code, notes, and snippets.

@umanghome
Last active April 27, 2021 20:10
Show Gist options
  • Save umanghome/f99522da7835ff9eeb40513b65035718 to your computer and use it in GitHub Desktop.
Save umanghome/f99522da7835ff9eeb40513b65035718 to your computer and use it in GitHub Desktop.
Did CoWIN deploy JS yet?
/**
* This could probably be done in like three lines of shell script
* but I have no idea how to write those.
*
* run `npm init -y`
* run `npm i node-fetch jsdom`
* run `node index.js`
* turn Slack notifications on
* and go to bed.
*
* But wake up every hour to check your notifications.
*/
const fetch = require('node-fetch');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
const url = `https://selfregistration.cowin.gov.in`;
const slackWebhookUrl = ''; // TODO: https://api.slack.com/messaging/webhooks
function getScriptPath(text) {
const dom = new JSDOM(text);
return `${url}/${dom.window.document.querySelector('script').src}`;
}
function fetchScriptLastModified(path) {
return fetch(path).then((res) => {
const headers = res.headers;
return headers.get('last-modified');
});
}
function isAfter24(date) {
const d = new Date(date);
return d.getDate() > 24;
}
function sleep(time) {
return new Promise((resolve) => {
setTimeout(resolve, time);
});
}
async function main() {
while (true) {
const d = new Date();
console.log('Checking at', d.toLocaleTimeString());
const changed = await check();
if (changed) {
break;
}
// sleep for 5 mins
await sleep(300000);
}
}
function check() {
return fetch(url)
.then((res) => res.text())
.then(getScriptPath)
.then(fetchScriptLastModified)
.then((lastModifiedAt) => {
if (isAfter24(lastModifiedAt)) {
fetch(slackWebhookUrl, {
body: JSON.stringify({
text: `JS bundle's last modified header changed to ${lastModifiedAt}`,
}),
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
});
return true;
} else {
return false;
}
});
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment