-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·92 lines (76 loc) · 2.71 KB
/
Copy pathindex.js
File metadata and controls
executable file
·92 lines (76 loc) · 2.71 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env node
const colors = require('colors')
const parseString = require('xml2js').parseString
const http = require('http')
const loaderOptions = {
frames: [
' _____________🚌',
' ____________🚌_',
' ___________🚌__',
' __________🚌___',
' _________🚌____',
' ________🚌💨____',
' _______🚌__💨___',
' ______🚌____💨__',
' _____🚌______💨_',
' ____🚌________💨',
' ___🚌__________',
' __🚌___________',
' _🚌____________',
' 🚌_____________',
],
interval: 80
}
const loader = require('cli-loader')(loaderOptions)
const API_URL = 'http://www.labs.skanetrafiken.se/v2.2/stationresults.asp?selPointFrKey=80147'
const xmlToJson = (url, callback) => {
var req = http.get(url, function(res) {
var xml = '';
res.on('data', function(chunk) {
xml += chunk;
});
res.on('error', function(e) {
callback(e, null);
});
res.on('timeout', function(e) {
callback(e, null);
});
res.on('end', function() {
parseString(xml, function(err, result) {
callback(null, result);
});
});
});
}
loader.start()
console.log()
xmlToJson(API_URL, (err, data) => {
if (err) {
return console.err(`Oh no! Error! ${err}`)
}
// so seductive
let allBusses = data['soap:Envelope']['soap:Body'][0].GetDepartureArrivalResponse[0].GetDepartureArrivalResult[0].Lines[0].Line
let myBusses = allBusses.filter(bus => {
return bus.Towards[0] === 'Västra hamnen via Dockan'
})
// Timeouting the output because loader is cute
// One of those cases where slower performance === a better user experience
setTimeout(() => {
loader.stop()
myBusses.forEach(bus => {
const next = new Date(bus.JourneyDateTime[0]).getTime()
const now = Date.now()
const difference = new Date(next - now)
const hoursLeft = String(difference.getHours()).substr(-2)-2 // TODO - not sure why -2 is needed
const minutesLeft = String(difference.getMinutes()).substr(-2)
const preOutput = ' 🚌 💨 En buss går om'
const postHours = hoursLeft <= 1 ? 'timme' : 'timmar'
const postMinutes = minutesLeft <= 1 ? 'minut' : 'minuter'
if (hoursLeft == 0) {
console.log(`${preOutput} ${colors.green(minutesLeft)} ${postMinutes}.`)
} if(hoursLeft == 1) {
console.log(`${preOutput} ${colors.green(hoursLeft)} ${postHours} och ${colors.green(minutesLeft)} ${postMinutes}.`)
}
})
}, 2000)
})