-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.js
145 lines (117 loc) · 4.42 KB
/
crawler.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const Crawler = require("crawler");
const fs = require('fs');
const logger = require('winston');
const path = require('path');
const moment = require('moment');
moment.locale('en-GB');
const FT_LIVE_BASE_URI = 'https://live.ft.com';
const dataArray = [];
const limit = 10;
let c;
let offset = 0;
const getLiveResultsUrl = (givenOffset) => {
const query = `Filters%5Bend_date%5D=comingup&Filters%5Bsectors%5D=&Filters%5Bregions%5D=&SearchText=&SearchCriteriumIdentifier=dateasc&Limit=${limit}&Offset=${givenOffset}&FirstEventsLarge=0`
return `https://live.ft.com/search/results/?${query}`;
};
c = new Crawler({
maxConnections : 10,
// This will be called for each crawled page
callback : function (error, {request, $}, done) {
if (error) {
console.error(error);
return done(error);
}
const events = $('.ft-event');
if (events.length > 0) {
events.each(function() {
const href = $(this).find('a').attr('href');
if (/\/events\//i.test(href)) {
const imageSrc = $(this).find('img').attr('src');
crawlEvent({imageSrc: `${FT_LIVE_BASE_URI}${imageSrc}`})(`${FT_LIVE_BASE_URI}${href}`);
}
});
offset += limit;
c.queue(getLiveResultsUrl(offset));
}
done();
}
});
// Queue just one URL, with default callback
c.queue(getLiveResultsUrl(offset));
const itemProp = (attribute) => `[itemprop="${attribute}"]`;
const DEFAULT_TIME = '9:00am';
const getDate = ($) => {
const date = $(`.ft-event-location ${itemProp('startDate')}`).attr('datetime');
let startTime;
try {
startTime = $('.ft-block-agenda .time').first().text().trim() || DEFAULT_TIME;
} catch (error) {
startTime = DEFAULT_TIME;
}
return moment(`${date} ${startTime}`, 'DD-MM-YYYY HH:mmA').format();
}
const getCategories = ($banner) => {
const categoriesText = $banner.find('.ft-event-category').text().trim().replace(/\s/g, '')
return categoriesText ? categoriesText.split(',') : [];
}
const hasScript = ($element) => $element.find('script').length > 0;
const getDescription = ($) => {
const overview = $('.ft-event-full-overview').text().trim();
const $articleDescription = $('.article-desc');
const description = $articleDescription.text().trim();
return hasScript($articleDescription) ? overview : description;
}
const crawlEvent = ({imageSrc}) => (uri) => {
c.queue([{
uri,
callback(error, {request, $}, done) {
if (error) {
console.error(error);
return done(error);
}
try {
const uri = request.uri.href;
const date = getDate
const $banner = $('.ft-event-top');
const entry = {
uri,
imageSrc,
categories: getCategories($banner) ,
title: $banner.find(itemProp('name')).text().trim(),
location: $banner.find(`.ft-event-location ${itemProp('location')}`).text().trim(),
date: getDate($),
speakers: $('.ft-full-person-details').map(function() {
const $person = $(this);
return {
name: $person.find(itemProp('name')).html().trim(),
company: $person.find(itemProp('worksFor')).html().trim(),
jobTitle: $person.find(itemProp('jobTitle')).html().trim()
};
}).get(),
description: getDescription($)
};
logger.info('Collected event', {uri: entry.uri});
dataArray.push(entry);
// logger.info(entry);
done();
} catch (error) {
logger.error(`Error for uri: ${uri}`, error);
done(error);
}
}
}])
}
process.on('uncaughtException', (error) => {
logger.error(error);
process.exit(1);
});
process.on('SIGINT', () => {
logger.info('Got exit signal')
process.exit();
})
process.on('exit', () => {
// logger.info(dataArray);
logger.info(`Scraped ${dataArray.length} items`);
fs.writeFileSync(path.join(__dirname, 'output/ft-live.json'), JSON.stringify({pages: dataArray}));
process.exit();
})