-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (72 loc) · 1.88 KB
/
index.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
import fs from 'fs';
import { scrape as scrapeBazos } from './scrapers/bazoscz.js'
import { scrape as scrapeSbazar } from './scrapers/sbazarcz.js'
import { scrape as scrapeSauto } from './scrapers/sautocz.js'
const bazosOffers = await scrapeBazos();
const sbazarOffers = await scrapeSbazar();
const sautoOffers = await scrapeSauto();
const allOffers = [
...bazosOffers,
...sbazarOffers,
...sautoOffers,
]
const existingOffers = JSON.parse(fs.readFileSync('twingos.json', 'utf8'));
const newOffers = allOffers.filter(offer => !existingOffers.some(o => o.id === offer.id));
if (newOffers.length === 0) {
// No new twingos for you.
console.log('0');
}
else {
// We found some new twingos yayy
appendNewOffersToJsonFile();
saveNewOffersToEmailFile();
console.log('1');
}
function appendNewOffersToJsonFile() {
fs.writeFileSync('twingos.json', JSON.stringify([...existingOffers, ...newOffers], null, 2));
}
function saveNewOffersToEmailFile() {
let emailBody = `
<html>
<head>
<style>
body { font-family: Arial, sans-serif; }
th, td { padding: 10px; }
</style>
</head>
<body>
<table style="width: 100%">
<tr>
<td>Image</td>
<td>Twingo</td>
<td>Location</td>
<td>Link</td>
</tr>
${newOffers.map(offer => `
<tr>
<td>
<img src="${offer.image}" alt="${offer.title}" width="100" height="100" style="object-fit: contain; background-color: #f3f2f2;">
</td>
<td>
<p>
${offer.title.length > 30 ? offer.title.substring(0, 30) + '...' : offer.title}
<br>
<br>
<strong>${offer.price}</strong>
</p>
</td>
<td>${offer.location}</td>
<td>
<a href="${offer.link}" target="_blank">
${offer.source}
</a>
</td>
</tr>
`).join('')}
</table>
<p>See ya</p>
</body>
</html>
`;
fs.writeFileSync('twingos-email.html', emailBody);
}