silvrgit/lib/events.js

143 lines
4.1 KiB
JavaScript
Raw Normal View History

const http = require('http'), request = require('request'), cheerio = require('cheerio');
const eventTimer = 0;
const eventCache = {
'last': 0,
'data': {},
'expire': ((60 * 1000) * 60) * 12,
'cinema': {}
2016-03-08 21:52:21 +00:00
};
// 'cwr':{data: {}, last:0};
const cinemas = [{ 'id': 'cwr', 'url': 'https://film.list.co.uk/cinema/14982-cineworld-renfrew-street/' },
{ 'id': 'gsc', 'url': 'https://film.list.co.uk/cinema/13590-cineworld-glasgow-science-centre/' },
{ 'id': 'pho', 'url': 'https://film.list.co.uk/cinema/12500-showcase-cinema-paisley/' }];
2016-03-08 21:52:21 +00:00
module.exports = {
'getEvents': function (req, res) {
console.log('Getting events...');
const j = [], url = 'https://www.list.co.uk/events/days-out/when:this%20weekend/location:Dumbarton(55.9460,-4.5556)/distance:20/';
2016-03-08 21:52:21 +00:00
const now = new Date();
2016-03-08 21:52:21 +00:00
if ((now - eventCache.last) > eventCache.expire)
request(url, function (err, resp, body) {
if (err)
throw err;
$ = cheerio.load(body);
2016-03-08 21:52:21 +00:00
// console.log($);
// TODO: scraping goes here!
$('.resultsRow').each(function (div) {
const item = {};
const eventSummary = $(this).find('.eventSummary').first();
const byDate = $(this).find('.byDate').first();
2016-03-08 21:52:21 +00:00
const title = eventSummary.find('.head').first();
const description = eventSummary.find('P').first();
const link = ` https://www.list.co.uk${ eventSummary.find('A').first().attr('href')}`;
2016-03-08 21:52:21 +00:00
const price = byDate.find('.price').first();
const dt = byDate.find('.dtstart').first().attr('title');
2016-03-08 21:52:21 +00:00
item.title = title.text();
item.description = description.text();
item.link = link;
item.price = price.text();
item.date = dt;
2016-03-08 21:52:21 +00:00
j.push(item);
});
2016-03-08 21:52:21 +00:00
eventCache.last = now;
eventCache.data = j;
2016-03-08 21:52:21 +00:00
res.render('pages/events', eventCache);
}, function(error, response, body) {
if(response.statusCode !== 200) {
logger.error(response.statusCode);
logger.error(body);
2016-03-08 21:52:21 +00:00
}
});
else {
console.log('Using event cache...');
2016-03-08 21:52:21 +00:00
res.render('pages/events', eventCache);
}
},
'doGetCinema': function (id) {
const cinemaID = cinemas[id].id;
const url = cinemas[id].url;
const thisCinema = eventCache[cinemaID] || { 'data': {}, 'last': 0 };
console.log(cinemaID);
console.log(url);
const j = [];
const now = new Date();
if ((now - thisCinema.last) > eventCache.expire)
request(url, function (err, resp, body) {
console.log('Working');
if (err)
throw err;
$ = cheerio.load(body);
$('.byEvent').each(function (div) {
const item = {};
const title = $(this).find('H4').first();
const eventSummary = $(this).find('.eventSummary').first();
const description = eventSummary.find('P').first();
const link = ` https://www.list.co.uk${ eventSummary.find('A').first().attr('href')}`;
item.title = title.text();
item.description = description.text();
item.link = link;
j.push(item);
});
thisCinema.last = now;
thisCinema.data = j;
eventCache[cinemaID] = thisCinema;
console.log('returning');
return thisCinema;
}, function(error, response, body) {
if(response.statusCode !== 200) {
console.error(response.statusCode);
console.error(body);
2016-03-08 21:52:21 +00:00
}
});
else {
console.log('Using event cache...');
return thisCinema;
2016-03-08 21:52:21 +00:00
}
},
'getCinema': function (req, res) {
const id = parseInt(req.params.id);
console.log(`Getting cinema: ${ id}`);
const output = module.exports.doGetCinema(id);
res.render('pages/cinema', output);
},
'preLoad': function () {
let output = module.exports.doGetCinema(0);
output = module.exports.doGetCinema(1);
output = module.exports.doGetCinema(2);
setTimeout(function () {
module.exports.preLoad();
}, eventCache.expire);
}
2016-03-08 21:52:21 +00:00
};
setTimeout(function () {
console.log('Pre loading cinemas...');
module.exports.preLoad();
2016-03-08 21:52:21 +00:00
}, 10000);