-
Notifications
You must be signed in to change notification settings - Fork 0
/
brigaden.js
71 lines (67 loc) · 1.9 KB
/
brigaden.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
var scraper = require('scraper');
var days2 = 'Måndag:Tisdag:Onsdag:Torsdag:Fredag:';
var Offer = require('./offer');
exports.scrape = function (req, res, resultHandler) {
scraper(
{
'uri': 'http://www.brigaden.net/1/1.0.1.0/26/1/',
'encoding': 'utf8'
}
,
function(err, jQuery) {
if (err) {throw err;}
parseJQuery(jQuery, resultHandler);
});
}
exports.title = function() {
return 'Brigaden';
}
function parseJQuery(jQuery, resultHandler) {
console.log("Parsing");
var header = '';
var offers = new Array();
var dishes = new Array();
var dishesLeft = 0;
var dishText = '';
var curDay = '';
header = 'Brigaden ' + jQuery('span').find('h2').text().trim();
jQuery('span').find('p,div').
each(function() {
var curText = jQuery(this).find('strong').text().trim();
if (curText.length > 0 && days2.indexOf(curText) != -1) {
console.log('Found new day: ' + curText);
curDay = curText.substring(0, curText.length - 1);
dishes = new Array();
if (curText === 'Måndag:') {
dishText = jQuery(this).contents().last().text().trim();
if (dishText.length > 0) {
dishesLeft = 3;
dishes.push(dishText);
} else {
dishesLeft = 4;
}
} else {
dishText = '';
dishesLeft = 4;
}
} else if (dishesLeft > 0) {
curText = jQuery(this).text().trim();
if (dishesLeft == 4) {
dishText = curText;
} else {
dishText += '<BR>' + curText;
}
dishes.push(curText);
if (dishesLeft == 1) {
var o = new Offer();
o.day = curDay;
o.shortDay = curDay.substring(0, 2);
o.dishes = dishes;
offers.push(o);
}
console.log('dish=' + curText);
dishesLeft--;
}
});
resultHandler(offers, header);
}