-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (67 loc) · 2.03 KB
/
index.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 express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var app = express();
app.get('/otg/events', function(req, res) {
var url = 'http://offthegridsf.com/wp-admin/admin-ajax.php?action=otg_events_list';
request(url, function(error, response, html){
if (!error) {
var $ = cheerio.load(html);
var events = [];
$('.otg-markets-day').each(function(i, $el){
var parentIndex = i;
var $this = $(this);
var day = $this.find('.otg-markets-dayname').text().replace(/[\(\)]/g, '');
events[i] = {date: day, markets: []};
$this.find('.otg-markets-event').each(function(index, $elem){
var $child = $(this);
if ($child.find('.otg-markets-event-name').length == 0) {
return false;
}
var name = $child.find('.otg-markets-event-name').text();
var hours = $child.find('.otg-markets-event-hours').text();
var id = $child.data('otg-market-id');
events[parentIndex].markets[index] = {
id: id,
name: name,
hours: hours
};
});
});
return res.json({events});
} else {
res.send(404);
}
});
});
app.get('/otg/market/:id', function(req, res) {
var url = 'http://offthegridsf.com/wp-admin/admin-ajax.php?action=otg_market&delta=0&market=' + req.params.id;
request(url, function(error, response, html){
if (!error) {
var $ = cheerio.load(html);
var market = {};
market.name = $('.otg-market-data-name').text();
market.addr = $('.otg-market-data-address a').text();
market.events = [];
$('.otg-market-data-vendors').each(function(i, el){
var $this = $(this);
var date = $this.prev().text();
var vendors = [];
$this.find('.otg-markets-data-vendor-name').each(function(index, elem){
var name = $(this).text().replace(/.\(.*?\)/g, '');
vendors[index] = name;
});
market.events[i] = {
date: date,
vendors: vendors
}
});
res.json(market);
} else {
res.send(404);
}
});
});
app.listen('3000');
exports = module.exports = app;