Check for new XKCD comic
#!/usr/bin/env /usr/local/bin/node
/*
# <xbar.title>New XKCD</xbar.title>
# <xbar.version>v1.0.0</xbar.version>
# <xbar.author>Rob Tarr</xbar.author>
# <xbar.author.github>robtarr</xbar.author.github>
# <xbar.desc>Check for new XKCD comic</xbar.desc>
# <xbar.dependencies>node</xbar.dependencies>
# <xbar.abouturl>https://github.com/robtarr/bitbar-plugins</xbar.abouturl>
*/
const https = require("https");
const fs = require('fs');
const url = 'https://xkcd.com/atom.xml';
const icon = '| image=AAABAAIAEBAAAAAAAABoBQAAJgAAACAgEAAAAAAA6AIAAI4FAAAoAAAAEAAAACAAAAABAAgAAAAAAAABAAAAAAAAAAAAAAABAAAAAAAAAAAAAJycnABGRkYAenp6AIODgwAkJCQA6+vrAIyMjAAdHR0A29vbAHx8fAAvLy8Ajo6OADg4OAD///8AFhYWAEpKSgB+fn4AsrKyACgoKAAGBgYA+Pj4AKKiogDW1tYAd3d3AICAgAAqKioAiYmJAOjo6AC9vb0AXl5eAPHx8QBnZ2cA+vr6AHBwcADh4eEAgoKCAIuLiwBgYGAA8/PzAJ2dnQBHR0cAe3t7AOPj4wAlJSUAn5+fANPT0wB0dHQAsbGxAFtbWwCPj48AMDAwADk5OQChoaEAdnZ2AH9/fwApKSkAiIiIAJqamgBvb28AeHh4ANfX1wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKAAAACAAAABAAAAAAQAEAAAAAAAAAgAAAAAAAAAAAAAQAAAAAAAAAAAAAADPz88A39/fACAgIAD///8AcHBwAICAgACQkJAAoKCgANfX1wAoKCgAf39/AIiIiAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABEAAREREAARAAEREAAREQAREAERERABERABERABEREAERABEREQAREQAVEUARERABEQwNERDA0REYHRHDEREQAREQAREQAREREAEQAFEREAEREAJREAERERABmAERERABERDCkQwNERETAiAxEREQARERABEAEREREEAABREREAEAEQwAANEADREMANEQwRABAA0QwA0QwADREAERDA0QARABEQAREAEMDRABEMDREAEQwNEAEQwNEMDQAQwNERABEQwNABDA0REMAAAA0REQAREQAQAQAREREAAAAREREAEREMAAAA0RFAAAAAABURABEREMAADRERQAAAAAAVEQAREREQAREREQAAAAAAEREAEREQwAANEREADREQwBERABEQwAAAAA0RAJEREQgREQAREAAREQABEQEREREQEREAEQwRERERANEADREREBERABEAEREREQARAADREQwREQARAREREREQEQAAERCAEREAEQARERERABEAAAAAABERABEAEREREQARDAAAAADREQARDAAREQAA0RDAAAANEREAERAAAAAAARERLAAC0RERABEQwAAAAA0REREREREREQARERDAAA0REREREREREREAERERERERERERERERERERAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
const errorIcon = '| image=iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAAAAACo4kLRAAAACXBIWXMAAAsTAAALEwEAmpwYAAAGVWlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cG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DwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+olXGbQAAAUtJREFUGJVt0M9KAmEABPDZ9ds1NRGlbaOkBSmi8FTgoUOF1EkihHqBnqAXiPIBrFuHLoVQ1CEpCIKCCiQqFNKyUPuDEAsGuYa6mdr6dTADsTn+mLkMIrQlMot/0sDvS7UFE5tKMNOE6okSmvIMXP0hAcAn0pIEZ7GpSbSvnl2cxhvGsACeuWTUubFSywIANJljAfTOc8HQuX8mDACI+XMEAA+XXVhyvJD62uFj2wE8xQIuK6QqAKiWfJxEA0Zd0bKQSgliBgBMN8k1sj/Up7eLBjk5vCMCr++EjDFkcBEA0KEJ/ET+QBSsSmKSdRcAAGUaNmdJmr7tHbkkHKuUUkorZ0Fl+55GZqczVCa/j3A5avVuGUZWbxlUWVWr66MJbd6Hdd/13EUnKaRt1VpJX+keBWyeZe+4+5NnDvtLDKPpyl3GEkBsug8zc/cDY8OIy7Tw+C0AAAAASUVORK5CYII=';
function isNew(xml) {
const updatedOn = xml.match(/<updated>(.*?)Z<\/updated>.*/);
if (!updatedOn || !updatedOn[1]) return false;
const updatedDate = new Date(updatedOn[1]);
return updatedDate.getDay() === new Date().getDay();
}
https.get(url, res => {
res.setEncoding('utf8');
let body = '';
res.on('data', data => {
body += data;
});
res.on('end', () => {
if (isNew(body)) {
console.log(icon);
console.log('---');
console.log('xkcd.com | href=http://www.xkcd.com');
}
});
});