failed attempt

This commit is contained in:
2023-01-14 20:10:37 -03:00
parent 6f9cdb0c44
commit 0940558a03

View File

@@ -2,6 +2,7 @@ const { SlashCommandSubcommandBuilder, hyperlink, bold, ActionRowBuilder, Button
const puppeteer = require('puppeteer'); const puppeteer = require('puppeteer');
const jsdom = require('jsdom'); const jsdom = require('jsdom');
const { responses } = require('../constants'); const { responses } = require('../constants');
const { default: axios } = require('axios');
function truncateText(text, max) { function truncateText(text, max) {
return text.substr(0, max - 1).trim() + (text.length > max ? '...' : ''); return text.substr(0, max - 1).trim() + (text.length > max ? '...' : '');
@@ -63,7 +64,7 @@ const countryData = {
const pages = Object.values(countryData).map((country) => country.pages).flat(1); const pages = Object.values(countryData).map((country) => country.pages).flat(1);
const ELEMENTS_LIMIT = 3; const ELEMENTS_LIMIT = 5;
module.exports = { module.exports = {
data: new SlashCommandSubcommandBuilder() data: new SlashCommandSubcommandBuilder()
@@ -135,7 +136,7 @@ module.exports = {
(platform ? (platform ?
Object.entries(countryData) Object.entries(countryData)
.map(([key, value]) => value.pages.some(page => page.name === platform) ? key : false) .map(([key, value]) => value.pages.some(page => page.name === platform) ? key : false)
.filter(a => a)[0] .filter(countryValues => countryValues)[0]
: 'us' : 'us'
); );
const countryPages = countryData[country].pages.filter(countryPage => platform ? countryPage.name === platform : countryPage); const countryPages = countryData[country].pages.filter(countryPage => platform ? countryPage.name === platform : countryPage);
@@ -158,32 +159,73 @@ module.exports = {
if (!products.length) { if (!products.length) {
throw Error(); throw Error();
} }
products const abc = async (element, index) => {
.forEach((element) => { if (index >= ELEMENTS_LIMIT) {
if (productPrices.length >= ELEMENTS_LIMIT) { return;
return; }
} try {
try { const productRelativePath = element
const productRelativePath = element .querySelector(countryPage.selectors.link)
.querySelector(countryPage.selectors.link) .getAttribute('href')
.getAttribute('href') .replace(/.*\/\/[^/]*/, '');
.replace(/.*\/\/[^/]*/, ''); const productName = element.querySelector(countryPage.selectors.title).textContent;
const productName = element.querySelector(countryPage.selectors.title).textContent; const link = countryPage.productUrl.replace('%S', encodeURI(productRelativePath));
const link = hyperlink( console.log(link);
truncateText(productName, 100), const shortlyData = await axios.post('https://shortly.franp.site/add-url', {}, {
countryPage.productUrl.replace('%S', encodeURI(productRelativePath)), headers: {
); Accept: '*/*',
const priceNumber = element.querySelector(countryPage.selectors.price).textContent.replace('$', '').replace(' ', ''); url: link,
const price = `${countryData[country].currency} ${bold(priceNumber)}`; username: process.env.SHORTLY_USERNAME,
productPrices.push(`${link} | ${price}`); password: process.env.SHORTLY_PASSWORD,
} },
catch (err) { });
console.log(`FUCK ${countryPage.name} MAQUETATION`); const formatedLink = hyperlink(
console.error(err); truncateText(productName, 100),
} `https://shortly.franp.site/${shortlyData.data.url.id}`,
}); );
const priceNumber = element.querySelector(countryPage.selectors.price).textContent.replace('$', '').replace(' ', '');
const price = `${countryData[country].currency} ${bold(priceNumber)}`;
productPrices.push(`${formatedLink} | ${price}`);
}
catch (err) {
console.log(`FUCK ${countryPage.name} MAQUETATION`);
console.error(err.response);
}
};
let i = 0;
for (const iterator of products) {
await abc(iterator, i);
i++;
}
// products
// .forEach((element) => {
// if (productPrices.length >= ELEMENTS_LIMIT) {
// return;
// }
// try {
// const productRelativePath = element
// .querySelector(countryPage.selectors.link)
// .getAttribute('href')
// .replace(/.*\/\/[^/]*/, '');
// const productName = element.querySelector(countryPage.selectors.title).textContent;
// const link = hyperlink(
// truncateText(productName, 100),
// countryPage.productUrl.replace('%S', encodeURI(productRelativePath)),
// );
// const priceNumber = element.querySelector(countryPage.selectors.price).textContent.replace('$', '').replace(' ', '');
// const price = `${countryData[country].currency} ${bold(priceNumber)}`;
// productPrices.push(`${link} | ${price}`);
// }
// catch (err) {
// console.log(`FUCK ${countryPage.name} MAQUETATION`);
// console.error(err);
// }
// });
await browser.close(); await browser.close();
pagesScraped.push({ name: countryPage.name, searchUrl: encodeURI(searchUrl) }); const shoppingUrl = encodeURI(searchUrl);
console.log('a');
console.log(shoppingUrl);
pagesScraped.push({ name: countryPage.name, searchUrl: shoppingUrl });
} }
catch (err) { catch (err) {
pagesWithErrorScrapping.push(countryPage.name); pagesWithErrorScrapping.push(countryPage.name);
@@ -206,7 +248,8 @@ module.exports = {
`${productPrices.join('\n')}`, `${productPrices.join('\n')}`,
pagesWithErrorScrapping.length && pagesWithErrorScrapping.length &&
`${responses(userLanguage).errorScrapping} ${pagesWithErrorScrapping.map((name) => name).join(' ')}`, `${responses(userLanguage).errorScrapping} ${pagesWithErrorScrapping.map((name) => name).join(' ')}`,
].filter(a => a); ].filter(text => text);
console.log(replyTexts.join('\n\n').length);
await interaction.editReply({ content: replyTexts.join('\n\n'), components: [...buttons] }); await interaction.editReply({ content: replyTexts.join('\n\n'), components: [...buttons] });
}, },
}; };