Answer the question
In order to leave comments, you need to log in
Why is there a problem of jumping images through a node js loop?
There is an associative array in which there are a lot of url (thousands) by which pictures should be downloaded. The problem is that I can download one image by url, but when I try the script in a loop, it does not work.
The script creates a bunch of files weighing 0kb (that is, the picture is not loaded with an empty file), then it stalls completely.
My assumptions are that there are a lot of files, plus a double cycle, perhaps because of this he simply does not have time to upload files?
If this is the case, how can I make the next image load after the previous one has loaded?
Here is an example of the array itself:
var json = {
"1223": {
"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", "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"],
},
"21323": {
"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"],
},
"23423": {
"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"],
},
"5432": {
"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"],
},
"234234": {
"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"],
},
"34234": {
"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"],
},
"23421": {
"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) {
//--- console.log(json.PHOTO)
json[property].PHOTO.forEach(function(item, i, arr) {
//--console.log(item);
var file = fs.createWriteStream(i);
var request = http.get(item, function(response) {
response.pipe(file);
});
})
}
Answer the question
In order to leave comments, you need to log in
I just tweaked the naming of the files a little, everything seems to be working.
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);
}
})
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question