I am getting a some youtube video ids and storing them in an array then I looped on each video id to determine the size of that id's thumbnail. The process im using works until Error: connect ETIMEDOUT happens. This error happens on different loop indexs so the problem is not with inputting a broken url. How can I get all the thumbnail size without having timeout error. Thanks in advance.
note: the process works to a certain random video id then error happens.
const url = require('url')
const http = require('http')
const sizeOf = require('image-size')
//example array
const myArr = ['ficzsusamA0Mw', 'ficzsusamA0Mw', 'ficzsusamA0Mw', 'ficzsusamA0Mw', 'ficzsusamA0Mw', 'ficzsusamA0Mw', 'ficzsusamA0Mw', 'ficzsusamA0Mw', 'ficzsusamA0Mw', 'ficzsusamA0Mw', 'ficzsusamA0Mw', 'ficzsusamA0Mw', 'ficzsusamA0Mw', 'ficzsusamA0Mw', 'ficzsusamA0Mw'......]
for (var i = 0; i < myArr.length; i++) {
const imgUrl = `http://img.youtube.com/vi/${myArr[i}/maxresdefault.jpg`
const options = url.parse(imgUrl)
http.get(options, function(response) {
const chunks = []
response.on('data', function(chunk) {
chunks.push(chunk)
}).on('end', function() {
const buffer = Buffer.concat(chunks)
if (sizeOf(buffer).width == 120 && sizeOf(buffer).height == 90) {
console.log(`{"video_thumbnail": "http://img.youtube.com/vi/${myArr[i]}/hqdefault.jpg"}`)
} else {
console.log(`{"video_thumbnail": "http://img.youtube.com/vi/${myArr[i]}/maxresdefault.jpg"}`)
}
})
})
}
