Skip to content

Instantly share code, notes, and snippets.

@misterwell
Created January 23, 2015 20: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 misterwell/b5365e8843e5bb810321 to your computer and use it in GitHub Desktop.
Save misterwell/b5365e8843e5bb810321 to your computer and use it in GitHub Desktop.
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var fs = require('fs');
var request = require('request');
var showUrl = 'http://api.trakt.tv/show/summary.json/e5d5ed575b955ade53c9fa57d218dd05/suits';
var showJson = {};
var xhr = new XMLHttpRequest();
xhr.open("GET", showUrl, true);
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
showJson = JSON.parse(xhr.responseText);
for (key in showJson.images) {
console.log(showJson.images[key]);
saveImage(showJson.title, key, showJson.images[key]);
}
} else {
console.error(xhr.statusText);
}
}
};
xhr.onerror = function (e) {
console.error(xhr.statusText);
};
xhr.send(null);
var saveImage = function (title, type, imagePath) {
// Or with cookies
// var request = require('request').defaults({jar: true});
var slug = title.split(' ').join('-');
console.log(slug);
request.get({url: imagePath, encoding: 'binary'}, function (err, response, body) {
fs.writeFile('./images/' + slug + '_' + type + '.png', body, 'binary', function(err) {
if(err)
console.log(err);
else
console.log("The file was saved!");
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment