Skip to content

Instantly share code, notes, and snippets.

@joemaller
Created September 6, 2017 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joemaller/eb5f93c78fe5edc1f8e928f52de9dd05 to your computer and use it in GitHub Desktop.
Save joemaller/eb5f93c78fe5edc1f8e928f52de9dd05 to your computer and use it in GitHub Desktop.
Initial, rough AWS lambda script for the webcam.
"use strict";
const aws = require("aws-sdk");
const s3 = new aws.S3({ apiVersion: "2006-03-01" });
const dynamodb = new aws.DynamoDB();
const isNum = n => !isNaN(parseFloat(n)) && isFinite(n);
exports.handler = (event, context, callback) => {
const jsonFileName = "14th-street-webcam.json";
const minsAgo = 45;
const startTime = new Date(Date.now() - (minsAgo + 1) * 60 * 1000)
.getTime()
.toString();
const key = decodeURIComponent(
event.Records[0].s3.object.key.replace(/\+/g, " ")
);
const keyName = key.slice(4, -4).replace(/_/g, ":");
const timestamp = new Date(
isNum(keyName) ? keyName * 1000 : keyName
).getTime();
// console.log("now", Date.now());
// console.log("tzoffset", new Date().getTimezoneOffset());
// console.log("startTime", startTime);
// console.log("key", key.slice(4, -4));
// console.log("keyName", keyName);
// console.log("timestamp", timestamp);
const newItem = {
Item: {
webcam: { S: "14th-street" },
filename: { S: key },
timestamp: { N: timestamp.toString() }
},
ReturnConsumedCapacity: "TOTAL",
TableName: "webcams"
};
dynamodb.putItem(newItem, function(err, data) {
if (err) {
console.log(err, err.stack);
} else {
console.log(data);
}
});
const query = {
ExpressionAttributeValues: {
":v1": { S: "14th-street" },
":startTime": { N: startTime }
},
KeyConditionExpression: "webcam = :v1 AND #ts > :startTime",
ExpressionAttributeNames: { "#ts": "timestamp" }, // 'timestamp' is a reserved word
ProjectionExpression: "#ts, filename",
TableName: "webcams"
};
dynamodb.query(query, (err, data) => {
if (err) {
console.log(err, err.stack);
} else {
const feed = {
startTime: new Date(parseInt(startTime, 10)).toLocaleString("en-us", {
hour12: false,
timeZone: "America/New_York",
month: "long",
day: "2-digit",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
timeZoneName: "short"
}),
count: data.Items.length,
images: data.Items.map(
e => "https://s3.amazonaws.com/14th-street-webcam/" + e.filename.S
)
};
s3.putObject(
{
Body: JSON.stringify(feed, null, 2),
Bucket: "14th-st-test-feed-bucket",
Key: jsonFileName,
ContentType: "application/json"
},
(err, data) => {
if (err) console.log(err, err.stack);
else console.log(data);
}
);
}
});
};
#! /usr/bin/env bash
curl --digest -u webcam:14street http://192.168.0.14/mjpeg/snap.cgi?chn=0 > /tmp/cam.jpg
# FILENAME="cam-$(stat -c %Y /tmp/cam.jpg).jpg"
FILENAME="cam-$(date --iso-8601=seconds -d @$(stat -c %Y /tmp/cam.jpg) | tr : _).jpg"
scp /tmp/cam.jpg joemaller.com:/home/joemaller/webapps/joemaller/webcam-new/cam.jpg
#scp /tmp/cam.jpg joemaller.com:/home/joemaller/webapps/joemaller/webcam-new/$FILENAME
aws s3 cp /tmp/cam.jpg s3://14th-street-webcam/$FILENAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment