var fs = require('fs');
var http = require('http');
var child_process = require('child_process');
var json = {
"1223": {
"PHOTO": ["http://nagadali.ru/wp-content/uploads/2015/12/zhenshhina-rozhdennaya-v-god-krolika-kota-ochen-lyubopytna-500x375", "http://nagadali.ru/wp-content/uploads/2015/12/zhenshhina-rozhdennaya-v-god-krolika-kota-ochen-lyubopytna-500x375.jpg", "http://nagadali.ru/wp-content/uploads/2015/12/zhenshhina-rozhdennaya-v-god-krolika-kota-ochen-lyubopytna-500x375.jpg"]
},
"2323": {
"PHOTO": ["http://nagadali.ru/wp-content/uploads/2015/12/zhenshhina-rozhdennaya-v-god-krolika-kota-ochen-lyubopytna-500x375.jpg", "http://nagadali.ru/wp-content/uploads/2015/12/zhenshhina-rozhdennaya-v-god-krolika-kota-ochen-lyubopytna-500x375.jpg"]
}
};
for (property in json) {
json[property].PHOTO.forEach(function(item, i, arr) {
var path_arr = item.split('/');
var index_of_extension_dot = path_arr[path_arr.length - 1].lastIndexOf('.');
var extension = (index_of_extension_dot === -1) ? false : path_arr[path_arr.length - 1].substr(index_of_extension_dot + 1, path_arr[path_arr.length - 1].length - 1);
var path, file, request;
if (extension) {
path = property + '-' + i + '.' + extension;
file = fs.createWriteStream(path);
request = http.get(item, function(response) {
response.pipe(file);
});
} else {
console.log('error: an extension should be specified to prevent raw HTML loading', property, i, item);
}
})
}