Skip to content

Instantly share code, notes, and snippets.

@karamarimo
Created October 19, 2017 22:19
Show Gist options
  • Save karamarimo/0bf38a4c7c8c52dd2d64130c8bd88077 to your computer and use it in GitHub Desktop.
Save karamarimo/0bf38a4c7c8c52dd2d64130c8bd88077 to your computer and use it in GitHub Desktop.
periodically check if doto blog has got updated
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