-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDriveBack.js
103 lines (83 loc) · 2.42 KB
/
DriveBack.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"use strict"
let request = require('request');
let cheerio = require('cheerio');
let moment = require('moment');
let dateRegExp = /^\d{4}-\d{2}-\d{2}$/;
let DriveBackTrip = require('./DriveBackTrip.js');
let DriveBackRoute = require('./DriveBackRoute.js');
class DriveBack{
static search(routes, sender){
let f = new DriveBack();
f.search(routes, sender);
}
static createSearch(from, to, bothways){
return new DriveBackSearch(from, to, bothways);
}
search(routes, sender){
this.getTrips(function(trips){
let matches = [];
let ids = [];
for (let i = trips.length - 1; i >= 0; i--) {
for (let j = routes.length - 1; j >= 0; j--) {
if(trips[i].matches(routes[j]) && ids.indexOf(trips[i].idstring) == -1){
ids.push(trips[i].idstring);
matches.push(trips[i]);
}
}
}
for(let i = matches.length - 1; i >= 0; i--){
sender.send(matches[i].getMessage());
}
})
}
setTrips(trips){
this.trips = trips;
}
getTrips(callback){
if(this.trips !== undefined){
console.log('DriveBack: Returning cached trips');
callback(this.trips);
}else{
this.fetchTrips(callback);
}
}
clearTrips(){
delete this.trips;
}
fetchTrips(callback){
console.log('DriveBack: Fetching trips');
let _this = this;
request("http://www.driveback.se/resor.json", function(err, res, body){
if(err) throw err;
body = JSON.parse(body);
let trips = [];
let ids = [];
for (var i = 0; i < body.length; i++) {
let trip = body[i];
let from = trip.from_station.area + ' (' + trip.from_station.location.city + ')';
let fromDate = moment(trip.first_pickup).format('D MMM HH:mm');
let toDate = moment(trip.last_deliver).format('D MMM HH:mm');
let car;
if(trip.vehicle.car_model !== ""){
car = trip.vehicle.car_model + ' (' + trip.vehicle.car_size + ')';
}else{
car = trip.vehicle.car_size;
}
for (var j = 0; j < trip.to_stations.length; j++) {
let idstring = trip.id + '.' + trip.to_stations[j].id;
if(ids.indexOf(idstring) == -1){
ids.push(idstring);
let to = trip.to_stations[j].area + ' (' + trip.to_stations[j].location.city + ')';
let dbtrip = new DriveBackTrip(from, to, fromDate, toDate, car);
dbtrip.idstring = idstring;
trips.push(dbtrip);
}
}
}
console.log('DriveBack: Found ' + trips.length + ' trips');
_this.trips = trips;
callback(trips);
});
}
}
module.exports = DriveBack;