diff --git a/robots/image.js b/robots/image.js index 2c7d160..cccaf25 100644 --- a/robots/image.js +++ b/robots/image.js @@ -38,29 +38,32 @@ async function robot() { return imagesUrl } - async function downloadAllImages(content) { + async function downloadAllImages(content){ + content.downloadedImages = [] + let arrayWithoutRepeatedImages = await controlArrayOfImagesRepeated(content); + + for (let imageIndex = 0; imageIndex < arrayWithoutRepeatedImages.length; imageIndex++) { + const imageUrl = arrayWithoutRepeatedImages[imageIndex] + try { + await downloadAndSave(imageUrl, `${sentenceIndex}-original.png`) + content.downloadedImages.push(imageUrl) + console.log(`> [${sentenceIndex}][${imageIndex}] Baixou imagem com sucesso: ${imageUrl}`) + break + } catch(error) { + console.log(`> [${sentenceIndex}][${imageIndex}] Erro ao baixar (${imageUrl}): ${error}`) + } + } + } + async function controlArrayOfImagesRepeated(content) { + + let arrayOflImage = [] for (let sentenceIndex = 0; sentenceIndex < content.sentences.length; sentenceIndex++) { - const images = content.sentences[sentenceIndex].images - - for (let imageIndex = 0; imageIndex < images.length; imageIndex++) { - const imageUrl = images[imageIndex] - - try { - if (content.downloadedImages.includes(imageUrl)) { - throw new Error('Imagem já foi baixada') - } - - await downloadAndSave(imageUrl, `${sentenceIndex}-original.png`) - content.downloadedImages.push(imageUrl) - console.log(`> [${sentenceIndex}][${imageIndex}] Baixou imagem com sucesso: ${imageUrl}`) - break - } catch(error) { - console.log(`> [${sentenceIndex}][${imageIndex}] Erro ao baixar (${imageUrl}): ${error}`) - } - } - } + arrayOflImage.push(content.sentences[sentenceIndex].images) + } + return arrayOflImage = [ ...new Set(arrayOflImage)] + } async function downloadAndSave(url, fileName) {