Last active
June 15, 2022 10:17
-
-
Save lwwcas/15f8d7c4a9e5ac111df5b325ed281cd8 to your computer and use it in GitHub Desktop.
Script para exportar todos os itens do carrinho do Continente Online para CSV via Terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
run(); | |
function run(){ | |
let rawList = getRawProducts(); | |
let processedList = []; | |
let self = this; | |
rawList.forEach((item) => { | |
let categoryObj = { | |
'name': self.getCategoryName(item), | |
'products': [] | |
} | |
let productsList = item.getElementsByClassName('product-info'); | |
productsList.forEach((product) => { | |
let productData = []; | |
productData['id'] = self.getProductId(product); | |
productData['nome'] = self.getProductName(product); | |
productData['marca'] = self.getProductBrand(product); | |
productData['quantidade'] = self.getProductQuantity(product); | |
productData['valor'] = self.getProductPrice(product); | |
productData['valorUnidade'] = self.getProductUnitPrice(product).replace('.', ','); | |
productData['valorPeso'] = self.getProductPriceKg(product)[0]; | |
productData['tipoUnidade'] = self.getProductPriceKg(product)[1]; | |
productData['peso'] = self.getProductCapacity(product); | |
categoryObj.products.push(productData); | |
}); | |
processedList.push(categoryObj); | |
}); | |
this.exportToCsv(processedList); | |
} | |
function exportToCsv(list) { | |
const monthNames = ["Janeiro", "Fevereiro", "Marco", "Abril", "Maio", "Junho", | |
"Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro" | |
]; | |
let csv = ''; | |
let today = new Date(); | |
let todayFormated = `${today.getDate()}/${(today.getMonth()+1)}/${today.getFullYear()}`; | |
let month = monthNames[today.getMonth()]; | |
let year = today.getFullYear(); | |
list.forEach((item) => { | |
let line = ''; | |
let products = item.products; | |
products.forEach((product) => { | |
line += `"${product.id}",` //id | |
line += `"${item.name}",` //categoria | |
line += `"${product.nome}",`//nome | |
line += `"${product.marca}",`//marca | |
line += `"${product.peso}",`//peso | |
line += `"${product.valor}",`//valor | |
line += `"${product.valorUnidade}",`//valor unidade | |
line += `"${product.valorPeso}",`//valor por peso | |
line += `"${product.tipoUnidade}",`//medida | |
line += `"${product.quantidade}",` //quantidade | |
line += `"${month}",` //mes | |
line += `${year},` //ano | |
line += `${todayFormated}` //dd/mm/yy | |
line += '\n' | |
}); | |
csv += line | |
}) | |
console.log(csv); | |
} | |
function getProductPriceKg(product) { | |
let priceDiv = product.getElementsByClassName('ct-cart-tile-total-price'); | |
let priceSecondaryDiv = priceDiv[0].getElementsByClassName('ct-tile--price-secondary'); | |
let spanText = priceSecondaryDiv[0].getElementsByClassName('ct-price-value'); | |
let spanUnitText = priceSecondaryDiv[0].getElementsByClassName('ct-m-unit'); | |
let price = spanText[0].innerHTML.replace('€', '').trim(); | |
let unit = spanUnitText[0].innerHTML.replace('/', '').trim(); | |
return [ | |
price, | |
unit | |
]; | |
} | |
function getProductCapacity(product) { | |
let capacityDiv = product.getElementsByClassName('ct-cart-tile-capacity-container'); | |
let spanText = capacityDiv[0].getElementsByClassName('ct-tile--quantity'); | |
return spanText[0].innerHTML.trim(); | |
} | |
function getProductQuantity(product) { | |
let data = product.getAttribute('data-product-tile-impression'); | |
data = JSON.parse(data); | |
return data['quantity']; | |
} | |
function getProductPrice(product) { | |
let unitPrice = this.getProductUnitPrice(product); | |
let quantity = this.getProductQuantity(product); | |
let total = unitPrice * quantity; | |
return total.toString().replace('.', ','); | |
} | |
function getProductUnitPrice(product) { | |
let data = product.getAttribute('data-product-tile-impression'); | |
data = JSON.parse(data); | |
let total = data['price'] | |
return total | |
} | |
function getProductBrand(product) { | |
let data = product.getAttribute('data-product-tile-impression'); | |
data = JSON.parse(data); | |
return data['brand'].toString().trim(); | |
} | |
function getProductName(product) { | |
return product.getAttribute('data-product-name'); | |
} | |
function getProductId(product) { | |
return product.getAttribute('data-pid'); | |
} | |
function getCategoryName(category) { | |
return category.getAttribute('data-category-name'); | |
} | |
function getRawProducts() { | |
return document.getElementsByClassName('ct-category-line-items-container'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment