Created
October 19, 2017 22:19
-
-
Save karamarimo/0bf38a4c7c8c52dd2d64130c8bd88077 to your computer and use it in GitHub Desktop.
periodically check if doto blog has got updated
This file contains hidden or 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 http = require('http') | |
const interval = 3000 | |
console.log('ctrl+c to stop') | |
checkUpdate() | |
let dotohtml = null | |
async function checkUpdate () { | |
try { | |
const newhtml = await getPage('http://blog.dota2.com/') | |
if (dotohtml !== null) { | |
if (dotohtml === newhtml) { | |
console.log('no change') | |
} else { | |
console.log('doto page updated!!!\u0007') | |
} | |
} else { | |
console.log('first fetching') | |
dotohtml = newhtml | |
} | |
} catch (error) { | |
console.error('failed to get the page') | |
} | |
setTimeout(checkUpdate, interval) | |
} | |
function getPage(url) { | |
return new Promise((resolve, reject) => { | |
http.get(url, (res) => { | |
let data = '' | |
res.on('data', (chunk) => { | |
data += chunk | |
}) | |
res.on('end', () => { | |
resolve(data) | |
}) | |
}).on('error', (err) => { | |
reject(err) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment