Created
January 23, 2015 20:22
-
-
Save misterwell/b5365e8843e5bb810321 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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