generated from 11ty/eleventy-base-blog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinks.js
84 lines (70 loc) · 2.78 KB
/
links.js
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
const fetch = require('node-fetch');
const cheerio = require('cheerio');
const fs = require('fs');
const path = require('path');
const readline = require('readline');
function getCurrentDateRfc3339() {
return new Date().toISOString();
}
async function extractMetadataAndImages(url) {
try {
const response = await fetch(url);
const html = await response.text();
const $ = cheerio.load(html);
const title = $('meta[property="og:title"]').attr('content') || $('title').text();
const description = $('meta[property="og:description"]').attr('content') || $('meta[name="description"]').attr('content');
// Usar un Set para almacenar URLs únicas de imágenes
const imageUrls = new Set();
// Obtener imágenes de las etiquetas Open Graph
$('meta[property="og:image"]').each((i, el) => {
const src = $(el).attr('content');
if (src) {
imageUrls.add(src);
}
});
// Buscar en el cuerpo del documento si se necesitan más imágenes
if (imageUrls.size < 3) {
$('img').each((i, elem) => {
const src = $(elem).attr('src');
if (src) {
imageUrls.add(src);
}
});
}
// Convertir el Set en un Array y limitar a las 3 primeras imágenes
const uniqueImageUrls = Array.from(imageUrls).slice(0, 3);
return { title, description, imageUrls: uniqueImageUrls };
} catch (error) {
console.error(`Error fetching or processing URL ${url}:`, error);
return { title: '', description: '', imageUrls: [] };
}
}
async function processFile(filePath) {
const fileStream = fs.createReadStream(filePath);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
const linksData = []; // Arreglo para almacenar los datos de cada link
for await (const line of rl) {
if (line) {
try {
const metadata = await extractMetadataAndImages(line);
if (metadata) { // Comprobar que metadata no sea undefined
const currentDate = getCurrentDateRfc3339();
linksData.push({ url: line, processedDate: currentDate, ...metadata });
}
} catch (error) {
console.error(`Error processing URL ${line}:`, error);
}
}
}
// Escribir el arreglo como un archivo JSON
try {
fs.writeFileSync(path.join(__dirname, '_data', 'links.json'), JSON.stringify(linksData, null, 2));
} catch (error) {
console.error("Error writing links.json file:", error);
}
}
const linksFilePath = path.join(__dirname, '_data', 'links');
processFile(linksFilePath);