From 9408e409ff673035832db3232202dfc95abf06e3 Mon Sep 17 00:00:00 2001 From: Wilkor Almeida Date: Tue, 9 Apr 2019 21:55:51 -0300 Subject: [PATCH 1/5] =?UTF-8?q?meu=20primeiro=20PR=20para=20um=20projeto?= =?UTF-8?q?=20openSource.=20Eu=20criei=20uma=20logica=20para=20que=20o=20b?= =?UTF-8?q?ot=20n=C3=A3o=20fa=C3=A7a=20um=20downloado=20de=20uma=20imagem?= =?UTF-8?q?=20repetida,=20para=20isso,=20precisei=20criar=20=20uma=20funct?= =?UTF-8?q?ion=20chamada=20controlArrayImageRepeat=20que=20recebe=20um=20c?= =?UTF-8?q?ontent=20como=20parametro=20e=20itera=20um=20array=20e=20retorn?= =?UTF-8?q?a=20uma=20array=20sem=20repeti=C3=A7=C3=B5es=20em=20seus=20inde?= =?UTF-8?q?x?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robots/image.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/robots/image.js b/robots/image.js index 2c7d160..c1d84bd 100644 --- a/robots/image.js +++ b/robots/image.js @@ -40,7 +40,7 @@ async function robot() { async function downloadAllImages(content) { content.downloadedImages = [] - + for (let sentenceIndex = 0; sentenceIndex < content.sentences.length; sentenceIndex++) { const images = content.sentences[sentenceIndex].images @@ -48,10 +48,6 @@ async function robot() { 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}`) @@ -61,6 +57,7 @@ async function robot() { } } } + content.downloadedImages = [ ...new Set(content.downloadedImages) ] } async function downloadAndSave(url, fileName) { From a4742472eba7f46750d90c5dacc67fafbce2a712 Mon Sep 17 00:00:00 2001 From: Wilkor Almeida Date: Tue, 9 Apr 2019 21:56:07 -0300 Subject: [PATCH 2/5] =?UTF-8?q?meu=20primeiro=20PR=20para=20um=20projeto?= =?UTF-8?q?=20openSource.=20Eu=20criei=20uma=20logica=20para=20que=20o=20b?= =?UTF-8?q?ot=20n=C3=A3o=20fa=C3=A7a=20um=20downloado=20de=20uma=20imagem?= =?UTF-8?q?=20repetida,=20para=20isso,=20precisei=20criar=20=20uma=20funct?= =?UTF-8?q?ion=20chamada=20controlArrayImageRepeat=20que=20recebe=20um=20c?= =?UTF-8?q?ontent=20como=20parametro=20e=20itera=20um=20array=20e=20retorn?= =?UTF-8?q?a=20uma=20array=20sem=20repeti=C3=A7=C3=B5es=20em=20seus=20inde?= =?UTF-8?q?x?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robots/image.js | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/robots/image.js b/robots/image.js index c1d84bd..2787155 100644 --- a/robots/image.js +++ b/robots/image.js @@ -9,6 +9,7 @@ async function robot() { const content = state.load() await fetchImagesOfAllSentences(content) + await controlArrayImageRepeat() await downloadAllImages(content) state.save(content) @@ -38,26 +39,34 @@ async function robot() { return imagesUrl } - async function downloadAllImages(content) { + async function downloadAllImages(content){ + content.downloadedImages = [] - + let arrayWithoutRepeatedImages = await controlArrayImageRepeat(); + + 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 controlArrayImageRepeat(content) { + + let arrayControlImage = [] + 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 { - 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}`) - } - } - } - content.downloadedImages = [ ...new Set(content.downloadedImages) ] + arrayControlImage.push(content.sentences[sentenceIndex].images) + } + return arrayControlImage = [ ...new Set(arrayControlImage)] + } async function downloadAndSave(url, fileName) { From 411ecfa9f2651145ca5d89ef2d6633bd57b869d0 Mon Sep 17 00:00:00 2001 From: Wilkor Almeida Date: Tue, 9 Apr 2019 22:03:23 -0300 Subject: [PATCH 3/5] adicionando parametro contex na chamada da function controlArrayImageRepeat --- robots/image.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/robots/image.js b/robots/image.js index 2787155..128d76b 100644 --- a/robots/image.js +++ b/robots/image.js @@ -9,7 +9,7 @@ async function robot() { const content = state.load() await fetchImagesOfAllSentences(content) - await controlArrayImageRepeat() + await controlArrayImageRepeat(content) await downloadAllImages(content) state.save(content) From 056b74b2f97a6ed00b281f1690cdc0b016548704 Mon Sep 17 00:00:00 2001 From: Wilkor Almeida Date: Wed, 10 Apr 2019 13:50:03 -0300 Subject: [PATCH 4/5] =?UTF-8?q?alterando=20nome=20da=20fun=C3=A7=C3=A3o=20?= =?UTF-8?q?que=20itera=20todos=20as=20images=20e=20tira=20as=20repetida=20?= =?UTF-8?q?para=20controlArrayOfImagesRepeated?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robots/image.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/robots/image.js b/robots/image.js index 128d76b..01795e2 100644 --- a/robots/image.js +++ b/robots/image.js @@ -9,7 +9,6 @@ async function robot() { const content = state.load() await fetchImagesOfAllSentences(content) - await controlArrayImageRepeat(content) await downloadAllImages(content) state.save(content) @@ -42,7 +41,7 @@ async function robot() { async function downloadAllImages(content){ content.downloadedImages = [] - let arrayWithoutRepeatedImages = await controlArrayImageRepeat(); + let arrayWithoutRepeatedImages = await controlArrayOfImagesRepeated(); for (let imageIndex = 0; imageIndex < arrayWithoutRepeatedImages.length; imageIndex++) { const imageUrl = arrayWithoutRepeatedImages[imageIndex] @@ -55,17 +54,15 @@ async function robot() { console.log(`> [${sentenceIndex}][${imageIndex}] Erro ao baixar (${imageUrl}): ${error}`) } } - - } - async function controlArrayImageRepeat(content) { + async function controlArrayOfImagesRepeated(content) { - let arrayControlImage = [] + let arrayOflImage = [] for (let sentenceIndex = 0; sentenceIndex < content.sentences.length; sentenceIndex++) { - arrayControlImage.push(content.sentences[sentenceIndex].images) + arrayOflImage.push(content.sentences[sentenceIndex].images) } - return arrayControlImage = [ ...new Set(arrayControlImage)] + return arrayOflImage = [ ...new Set(arrayOflImage)] } From 336ea894426d6b776fff4a9a9992af95c3f3c249 Mon Sep 17 00:00:00 2001 From: Wilkor Almeida Date: Wed, 10 Apr 2019 13:53:47 -0300 Subject: [PATCH 5/5] adicionando content na chamada da function controlArrayOfImagesRepeated --- robots/image.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/robots/image.js b/robots/image.js index 01795e2..cccaf25 100644 --- a/robots/image.js +++ b/robots/image.js @@ -41,7 +41,7 @@ async function robot() { async function downloadAllImages(content){ content.downloadedImages = [] - let arrayWithoutRepeatedImages = await controlArrayOfImagesRepeated(); + let arrayWithoutRepeatedImages = await controlArrayOfImagesRepeated(content); for (let imageIndex = 0; imageIndex < arrayWithoutRepeatedImages.length; imageIndex++) { const imageUrl = arrayWithoutRepeatedImages[imageIndex]