Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR - Tratando repetições no bot de imagens #118

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions robots/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down