-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (53 loc) · 1.46 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
var request = require("request");
var cheerio = require("cheerio");
exports.handler = function (event, context) {
var options = {
uri: 'http://tokyu.bus-location.jp/blsys/navis',
form: {
VID: "rsc",
EID: "rd",
SCT: 2,
DSMK: Number(event.DSMK),
ASMK: Number(event.ASMK)
}
};
function trim(node) {
return node.text().replace(/[\r\n\t]/g, "");
}
request.post(options,
function (error, response, body) {
if (event.test) {
body = require('fs').readFileSync('check.html', 'utf-8');
}
var $ = cheerio.load(body);
var pos = [];
$(".approach tbody tr").each(function () {
var tr = $(this);
if (tr.hasClass("stopline")) {
pos.push([]);
}
if (tr.hasClass("busline") && pos.length) {
tr.children(".colmsg").each(function () {
$(this).children("div").each(function () {
var waittm = $(this).children(".waittm").remove();
var item = {
route: trim($(this)),
wait: parseInt(trim(waittm))
};
pos[pos.length - 1].push(item);
});
});
}
});
pos.shift(); // 先頭 (arrstopline) は不要
//console.log(pos);
var now = new Date();
context.done(null, {
time: ~~(now.getTime() / 1000),
DSMK: Number(event.DSMK),
ASMK: Number(event.ASMK),
item: pos
});
}
);
};