-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrapper.js
78 lines (52 loc) · 1.95 KB
/
scrapper.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
import axios from 'axios';
import cheerio from 'cheerio';
import { sendEmail } from './mailer.js'
export { scrapTask, prevScrapping };
let prevScrapping = [];
let currentScrapping = [];
const scrapTask = async (url) => {
const scrap = await axios(url)
const html = scrap.data;
const $ = cheerio.load(html);
const items = [];
$('.card-inner', html).each(function() {
const carTitle = $(this).find('.car-name').text();
const carPrice = $(this).find('.payment-amounts').text();
const carUrl = $(this).attr('href');
const carStatus = $(this).attr('data-cy').includes('booked') ? 'Booked' : 'Availabe';
// Get Id of the item
// Execute regular expression to extract the id from the url
const regex = /[0-9]\w+$/g;
const carId = carUrl.match(regex).toString();
items.push({carTitle, carPrice, carId, carStatus, carUrl});
});
if (prevScrapping.length == 0) {
prevScrapping = items;
return 'No items to compare. Current scrapping results were saved and will be compared with the result of the next scheduled scrapping.'
} else {
currentScrapping = items;
return compareScrapResultAndReturnDifference(prevScrapping, currentScrapping);
}
}
const compareScrapResultAndReturnDifference = (last, current) => {
const prevScrappingIdList = extractId(last);
const currentScrappingIdList = extractId(current);
let newIds = currentScrappingIdList.filter(item => !prevScrappingIdList.includes(item));
if (newIds.length != 0) {
const newCars = newIds.map(id => {
return current.find(car => {
return car.carId === id
})
})
sendEmail(newCars);
return newCars;
} else {
return 'No new items.'
}
}
const extractId = (array) => {
const id = array.map(item => {
return item.carId
})
return id
}