-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_breach_info.js
More file actions
84 lines (67 loc) · 2.96 KB
/
update_breach_info.js
File metadata and controls
84 lines (67 loc) · 2.96 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
const fs = require('fs');
const axios = require('axios');
const cheerio = require('cheerio');
const { getBreachesDB } = require('./scripts/db');
const URL_TO_SCRAPE = 'https://bonjourlafuite.eu.org/';
// Helper to normalize strings for matching
const normalizeString = (str) => {
if (!str) return '';
return str
.toLowerCase()
.normalize("NFD") // Decompose accented characters
.replace(/[\u0300-\u036f]/g, "") // Remove diacritical marks
.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()']/g, "") // Remove special characters
.replace(/\s+/g, ' ') // Replace multiple spaces with a single one
.trim();
};
const run = async () => {
try {
// 1. Fetch data from the URL
console.log(`Fetching data from ${URL_TO_SCRAPE}...`);
const { data: html } = await axios.get(URL_TO_SCRAPE);
const $ = cheerio.load(html);
// 2. Scrape data and create a map
const scrapedData = new Map();
$('.timeline-entry').each((i, element) => {
const name = $('h2', element).text().trim();
const normalizedName = normalizeString(name);
const descriptionP = $('.timeline-description p', element).first();
const p_text = descriptionP.text();
const victimMatch = p_text.trim().match(/^([\d\s.,]+)/);
if (normalizedName && victimMatch && victimMatch[1]) {
const victimCountText = victimMatch[1].trim();
scrapedData.set(normalizedName, victimCountText);
}
});
console.log(`Found ${scrapedData.size} potential breaches with victim counts from the URL.`);
// 3. Load the local breaches.json file
console.log(`Loading local data...`);
const db = await getBreachesDB();
const localData = db.data;
// 4. Iterate and update local data
let updatedCount = 0;
localData.breaches.forEach(breach => {
const normalizedBreachName = normalizeString(breach.Name);
const normalizedBreachTitle = normalizeString(breach.Title);
const key = [normalizedBreachName, normalizedBreachTitle].find(k => scrapedData.has(k));
if (key) {
const victimText = scrapedData.get(key);
const victimCount = parseInt(victimText.replace(/[\s,.]/g, ''), 10);
if (!isNaN(victimCount)) {
breach.pwnCountFr = victimCount;
breach.sourceFr = URL_TO_SCRAPE;
breach.victimTextFr = victimText.trim();
updatedCount++;
}
}
});
console.log(`Updated ${updatedCount} records in the local data.`);
// 5. Save the updated file
console.log(`Saving updated data...`);
await db.save();
console.log('Update complete!');
} catch (error) {
console.error('An error occurred:', error.message);
}
};
run();