Skip to content

Instantly share code, notes, and snippets.

@mikeg0
Created January 27, 2019 04:07
Show Gist options
  • Save mikeg0/46f04af581e03262a117464e96888f21 to your computer and use it in GitHub Desktop.
Save mikeg0/46f04af581e03262a117464e96888f21 to your computer and use it in GitHub Desktop.
node-js file to pull webcam images
var config = require('./config.json')
var http = require(config.schema);
var fs = require('fs');
console.log("========= startup =========")
var trafficCamHistory = []
// create required directories on startup
config.camImages.forEach(camera => {
fs.mkdir(process.cwd() + "/cam-history/" + camera.id, { recursive: true }, (err) => {
if (err.code !== 'EEXIST') throw err
if (err.code === 'EEXIST') return
console.log("created cam directory: " + process.cwd() + "/cam-history/" + camera.id)
});
})
var camPullInterval = setInterval(function() {
console.log("checking: " + new Date())
config.camImages.forEach(camera => {
var options = {method: 'HEAD', host: config.domain, port: 80, path: config.path + camera.id + config.extension};
var req = http.request(options, function(res) {
var eTag = res.headers['etag']
var history = trafficCamHistory[camera.id] || []
var found = history.find(eTagHistory => {
return (eTagHistory == eTag)
})
if (!found) {
var options = {method: 'GET', host: config.domain, port: 80, path: config.path + camera.id + config.extension};
var getRequest = http.request(options, function(getResponse) {
var lastModified = getResponse.headers['last-modified']
var filename = (new Date(lastModified).toISOString().replace(/T/, '_').replace(/\..+/, '').replace(/:/g, '-')) + ".jpeg"
var file = fs.createWriteStream(process.cwd() + "/cam-history/" + camera.id + "/" + filename);
getResponse.pipe(file);
console.log(camera.id + " saved: " + new Date())
})
getRequest.end();
history.push(eTag)
trafficCamHistory[camera.id] = history
console.log("new eTag found: " + camera.id + " date: " + new Date())
}
});
req.end();
});
}, 60000)
// uncomment to stop script after 1 hour ...
// setTimeout(function() {
// clearInterval(camPullInterval)
// }, 60 * 60 * 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment