Created
August 28, 2019 18:25
-
-
Save yousefamar/58b4de09b2a40259b2542e70e4610f7c to your computer and use it in GitHub Desktop.
A script to watch the cats at Battersea (https://www.battersea.org.uk/cats/cat-rehoming-gallery) and notify us by email when a new one shows up
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const https = require('https') | |
const { execSync } = require('child_process'); | |
const queryFrequencyMs = 5 * 60 * 1000; | |
let seenNids = {}; | |
const generateEmail = (() => { | |
const footer = `<h1>New cat alert!</h1>`; | |
return cats => { | |
let body = `<p>Hi humans!</p><p>You're getting this email because you're watching for new unreserved, unrehomed cats at the Battersea centre. If you want to stop receiving this email, talk to Yousef.</p><p>Also, click the cat thumbnails for full info.</p>`; | |
for(let c of cats) { | |
let age = new Date(new Date() - new Date(c.field_animal_age)).getFullYear() - 1970; | |
body += `<div style="overflow: auto"> | |
<h2>${c.title}</h2> | |
<div style="float: left"><a href="https://www.battersea.org.uk${c.path}" target="_blank"><img src="${c.field_animal_thumbnail}"/></a></div> | |
<ul style="overflow: hidden"> | |
<li>Sex: ${c.field_animal_sex}</li> | |
<li>Birthday: ${c.field_animal_age} (${age} years old)</li> | |
<li>Type: ${c.field_animal_size}-sized, ${c.field_animal_breed}</li> | |
</ul> | |
</div>`; | |
} | |
return body; | |
}; | |
})(); | |
setInterval(async () => { | |
console.log('Querying battersea...'); | |
https.get('https://www.battersea.org.uk/api/animals/cats', res => { | |
let body = ''; | |
res.on('data', chunk => body += chunk); | |
res.on('end', () => { | |
let json = JSON.parse(body); | |
let cats = json.animals.filter(a => a.field_animal_centre === 'battersea' && !a.field_animal_reserved && !a.field_animal_rehomed); | |
let newCats = []; | |
for (let c of cats) { | |
if (c.nid in seenNids) | |
continue; | |
seenNids[c.nid] = true; | |
newCats.push(c); | |
} | |
if (newCats.length) { | |
console.log('New cats!'); | |
fs.writeFileSync('/tmp/catmail.txt', generateEmail(newCats)); | |
execSync(`cat /tmp/catmail.txt | mail -s "New cat alert!" -a "From: Watchdog <watchdog@amar.io>" -a "Content-type: text/html" yousefamar@gmail.com veronicanacci97@gmail.com`); | |
return; | |
} | |
console.log('No new cats.'); | |
}); | |
}).on('error', e => { | |
console.error(e); | |
}); | |
}, queryFrequencyMs); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment