const http = require('http'), request = require('request'), cheerio = require('cheerio'); 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/' }]; 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/'; const now = new Date(); if ((now - eventCache.last) > eventCache.expire) request(url, function (err, resp, body) { if (err) throw err; $ = cheerio.load(body); // console.log($); // 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 { console.log('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 }; 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); } }); else { console.log('Using event cache...'); return thisCinema; } }, '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); } }; setTimeout(function () { console.log('Pre loading cinemas...'); module.exports.preLoad(); }, 10000);