Skip to content

Instantly share code, notes, and snippets.

@cjroebuck
Created November 15, 2012 14:22
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 cjroebuck/4078841 to your computer and use it in GitHub Desktop.
Save cjroebuck/4078841 to your computer and use it in GitHub Desktop.
var config, crypto, logger, webshotr;
crypto = require("crypto");
logger = require('./loggerService').logger;
config = require('config');
webshotr = function(apiKey, secret) {
var buildURL, hmacsha1, lib, prefix;
prefix = config.shot_api.prefix;
buildURL = function(url, width, height, full_page, delay, thumbnailSize) {
var query, ret, token;
if (typeof url !== "string") {
throw new Error("url should be of type string (something like www.google.com)");
}
if (typeof url !== "string") {
throw new Error("url is not valid");
}
if ((width != null) && typeof width !== "number") {
throw new Error("width should be a number");
}
if ((height != null) && typeof height !== "number") {
throw new Error("height should be a number");
}
if ((full_page != null) && typeof full_page !== "boolean") {
throw new Error("full_page should be a boolean");
}
if ((delay != null) && typeof delay !== "number") {
throw new Error("delay should be a number");
}
query = "url=" + url;
if (width != null) {
query += "&width=" + width;
}
if (height != null) {
query += "&height=" + height;
}
if (full_page != null) {
query += "&full_page=" + full_page;
}
if (delay != null) {
query += "&delay=" + delay;
}
if (thumbnailSize != null) {
query += "&thumbnailSize=" + thumbnailSize;
}
logger.info("apiKey: " + apiKey);
logger.info("secret: " + secret);
logger.info("query: " + query);
token = hmacsha1(secret, query);
logger.info("token: " + token);
ret = prefix + apiKey + "/" + token + "/png/?" + query;
logger.info('built url: ' + ret);
return ret;
};
hmacsha1 = function(key, text) {
logger.info("key: " + key);
logger.info("text: " + text);
return crypto.createHmac("sha1", key).update(text).digest("hex");
};
lib = {};
lib.buildURL = buildURL;
return lib;
};
module.exports = webshotr;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment