7911eb50a4
* hopefully fixed evermany cinema breaking * little tidyup of events.js
148 lines
4.3 KiB
JavaScript
148 lines
4.3 KiB
JavaScript
const http = require('http');
|
|
const request = require('request');
|
|
const cheerio = require('cheerio');
|
|
const logger = require('log4js').getLogger('Events');
|
|
|
|
const eventTimer = 0;
|
|
const eventCache = {
|
|
'last': 0,
|
|
'data': {},
|
|
'expire': ((60 * 1000) * 60) * 12,
|
|
'cinema': {}
|
|
};
|
|
// '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/' },
|
|
{ 'id':'evr', 'url':'https://film.list.co.uk/cinema/111845-everyman-glasgow/' }];
|
|
|
|
module.exports = {
|
|
'getEvents': function (req, res) {
|
|
logger.debug('Getting events...');
|
|
const j = [], url = 'https://www.list.co.uk/events/days-out/when:this%20weekend/location:Dumbarton(55.9460,-4.5556)/distance:20/';
|
|
|
|
const now = new Date();
|
|
|
|
if ((now - eventCache.last) > eventCache.expire)
|
|
request(url, function (err, resp, body) {
|
|
if (err)
|
|
throw err;
|
|
$ = cheerio.load(body);
|
|
// logger.debug($);
|
|
// TODO: scraping goes here!
|
|
|
|
$('.resultsRow').each(function (div) {
|
|
const item = {};
|
|
const eventSummary = $(this).find('.eventSummary').first();
|
|
const byDate = $(this).find('.byDate').first();
|
|
|
|
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')}`;
|
|
|
|
const price = byDate.find('.price').first();
|
|
const dt = byDate.find('.dtstart').first().attr('title');
|
|
|
|
item.title = title.text();
|
|
item.description = description.text();
|
|
item.link = link;
|
|
item.price = price.text();
|
|
item.date = dt;
|
|
|
|
j.push(item);
|
|
});
|
|
|
|
eventCache.last = now;
|
|
eventCache.data = j;
|
|
|
|
res.render('pages/events', eventCache);
|
|
}, function(error, response, body) {
|
|
if(response.statusCode !== 200) {
|
|
logger.error(response.statusCode);
|
|
logger.error(body);
|
|
}
|
|
});
|
|
else {
|
|
logger.info('Using event cache...');
|
|
|
|
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 };
|
|
logger.debug(cinemaID);
|
|
logger.debug(url);
|
|
const j = [];
|
|
|
|
const now = new Date();
|
|
|
|
if ((now - thisCinema.last) > eventCache.expire)
|
|
request(url, function (err, resp, body) {
|
|
logger.debug('Working');
|
|
if (err)
|
|
throw err;
|
|
|
|
const $ = 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;
|
|
logger.debug('returning');
|
|
|
|
return thisCinema;
|
|
}, (error, response, body) => {
|
|
if(response.statusCode !== 200) {
|
|
console.error(response.statusCode);
|
|
console.error(body);
|
|
}
|
|
});
|
|
else {
|
|
logger.debug('Using event cache...');
|
|
|
|
return thisCinema;
|
|
}
|
|
},
|
|
'getCinema': function (req, res) {
|
|
const id = parseInt(req.params.id);
|
|
logger.debug(`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);
|
|
output = module.exports.doGetCinema(3);
|
|
|
|
setTimeout(function () {
|
|
module.exports.preLoad();
|
|
}, eventCache.expire);
|
|
}
|
|
};
|
|
|
|
setTimeout(function () {
|
|
logger.debug('Pre loading cinemas...');
|
|
module.exports.preLoad();
|
|
}, 10000);
|
|
|