-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch-data.js
More file actions
94 lines (76 loc) · 3.31 KB
/
Copy pathfetch-data.js
File metadata and controls
94 lines (76 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// fetch-data.js
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { execSync } from 'child_process';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const SPREADSHEET_ID = '1XQoY_4MpFV0nr9Ohm1ZoSClq29pCtwrPNYlezuws8lM';
const SHEET_RANGE = 'Sheet1';
const OUTPUT_FILE = path.join(__dirname, 'src', 'data', 'ofertas.json');
const CREDENTIALS_FILE = path.join(__dirname, 'service-account.json');
async function fetchFromSheets() {
try {
console.log('Descargando datos desde Google Sheets...');
const gwsCommand = `gws sheets +read --spreadsheet "${SPREADSHEET_ID}" --range "${SHEET_RANGE}" --format json`;
const result = execSync(gwsCommand, {
env: {
...process.env,
GOOGLE_WORKSPACE_CLI_CREDENTIALS_FILE: CREDENTIALS_FILE
},
encoding: 'utf8'
});
const data = JSON.parse(result);
const values = data.values;
if (!values || values.length < 2) {
console.warn('El sheet parece estar vacío o solo tiene los encabezados.');
return;
}
const headers = values[0].map(h => h.toLowerCase().trim());
const results = [];
for (let i = 1; i < values.length; i++) {
const row = values[i];
if (row.length > 0 && row[0]) {
const rowObject = {};
for (let j = 0; j < headers.length; j++) {
rowObject[headers[j]] = row[j] || '';
}
const active = (rowObject['active'] || '').toLowerCase().trim();
if (active !== 'si' && active !== 'yes' && active !== 'true' && active !== '1') {
continue;
}
const rawUrl = rowObject['url'] || rowObject['amazonurl'] || rowObject['enlace afiliado'] || '';
let detectedPlatform = rowObject['plataform'] || rowObject['platform'] || '';
if (!detectedPlatform || detectedPlatform.toLowerCase() === 'amazon') {
if (rawUrl.includes('target.com')) detectedPlatform = 'Target';
else if (rawUrl.includes('walmart.com')) detectedPlatform = 'Walmart';
else if (rawUrl.includes('bestbuy.com')) detectedPlatform = 'BestBuy';
else if (rawUrl.includes('ebay.com')) detectedPlatform = 'eBay';
else detectedPlatform = 'Amazon';
}
const product = {
id: rowObject['id'] || String(i),
title: rowObject['title'] || `Producto ${i}`,
image: rowObject['image'] || 'https://via.placeholder.com/300',
price: rowObject['price'] || '0.00',
originalPrice: rowObject['originalprice'] || rowObject['precio original'] || null,
rating: parseFloat(rowObject['rating'] || rowObject['estrellas'] || '5'),
amazonUrl: rawUrl || '#',
platform: detectedPlatform,
category: rowObject['category'] || rowObject['categoria'] || 'General'
};
results.push(product);
}
}
const dataDir = path.dirname(OUTPUT_FILE);
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
}
fs.writeFileSync(OUTPUT_FILE, JSON.stringify(results, null, 2));
console.log(`✅ ¡Éxito! ${results.length} productos actualizados y guardados en ${OUTPUT_FILE}`);
} catch (error) {
console.error('❌ Error al actualizar los datos:', error.message);
process.exit(1);
}
}
fetchFromSheets();