old-silvrgit/lib/events.js

73 lines
1.9 KiB
JavaScript
Raw Normal View History

2016-01-08 14:53:49 +00:00
var http = require('http'), request = require('request'), cheerio = require('cheerio');
var eventCache = {
last: 0,
data: {},
expire: ((60 * 60) * 12) * 1000
};
module.exports = {
getEvents: function (req, res) {
console.log('Getting events...');
var j=[], url = 'https://www.list.co.uk/events/days-out/when:this%20weekend/location:Dumbarton(55.9460,-4.5556)/distance:20/';
var 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)
{
var item={};
var eventSummary = $(this).find('.eventSummary').first();
var byDate = $(this).find('.byDate').first();
var title = eventSummary.find('.head').first();
var description = eventSummary.find('P').first();
var link = ' https://www.list.co.uk' + eventSummary.find('A').first().attr('href');
var price = byDate.find('.price').first();
var 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 );
});
} else
{
console.log('Using event cache...');
res.render('pages/events',eventCache );
}
}
};