-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
296 lines (254 loc) · 8.57 KB
/
core.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Dependencies
const fs = require('fs');
const readline = require('readline');
const request = require('request');
const async = require('async');
const homeDir = require('home-dir');
function Core () {
const baseUrls = {
stopSearch: 'https://api.resrobot.se/v2/location.name',
tripSearch: 'https://api.resrobot.se/v2/trip'
}
const storeDir = homeDir('/.tramz');
const storeFile = storeDir + '/store.json';
let data = getStoreData();
// Will fetch the locally stored data and if it doesn't exist, make sure to create a base.
function getStoreData () {
if (!fs.existsSync(storeDir)) {
fs.mkdirSync(storeDir);
}
if (!fs.existsSync(storeFile)) {
fs.writeFileSync(storeFile, JSON.stringify({ stops: {}, trips: {} }));
}
return JSON.parse(fs.readFileSync(storeFile, 'utf8'));
}
// Simple method to update the contents of the store file with new data.
function updateStoreData () {
fs.writeFileSync(storeFile, JSON.stringify(data));
}
// Deprecated method that will fetch the Västtrafik token for the old version (0.2)
function getToken () {
return new Promise((resolve, reject) => {
request.get('https://tramz.solvd.se/token', (error, response, body) => {
if (error) {
if (error.code === 'ENOTFOUND') {
reject('Sorry, but you don\'t seem to have an internet connection, so we can\'t proceed with this action at the moment.');
} else {
reject('Oops, something went wrong when contacting the API. Sorry about that.');
}
return;
}
resolve(JSON.parse(body).token);
});
});
}
function getPlanKey () {
return new Promise((resolve, reject) => {
request('https://tramz.solvd.se/keys/plan', (error, response, body) => {
if (error) {
if (error.code === 'ENOTFOUND') {
reject('Sorry, but you don\'t seem to have an internet connection, so we can\'t proceed with this action at the moment.');
} else {
reject('Oops, something went wrong when contacting the API. Sorry about that.');
}
return;
}
resolve(JSON.parse(body).key);
});
});
}
function getListKey () {
return new Promise((resolve, reject) => {
request('https://tramz.solvd.se/keys/list', (error, response, body) => {
if (error) {
if (error.code === 'ENOTFOUND') {
reject('Sorry, but you don\'t seem to have an internet connection, so we can\'t proceed with this action at the moment.');
} else {
reject('Oops, something went wrong when contacting the API. Sorry about that.');
}
return;
}
resolve(JSON.parse(body).key);
});
});
}
function buildStopSearchUrl (key, input) {
let url = baseUrls.stopSearch + '?key=' + key;
url += '&input=' + encodeURIComponent(input);
url += '&format=json';
return url;
}
function buildTripSearchUrl (options) {
let url = baseUrls.tripSearch;
url += '?key=' + options.key;
url += '&originId=' + options.origin.id;
url += '&destId=' + options.destination.id;
if (options.via) {
url += '&viaId=' + options.via.id;
}
url += '&date=' + options.date + '&time=' + options.time + '&format=json';
return url;
}
function trimTimeString (time) {
return time.substring(0, 5);
}
function trimLineName (name) {
if (name.indexOf('Länstrafik - ') > -1) {
name = name.replace('Länstrafik - ', '');
} else if (name.indexOf('Regional Tåg ') === 0) {
name = name.replace('Regional Tåg ', '') + '(Regional Tåg)';
}
return name;
}
function getAllStops () {
return (data.stops) ? data.stops:{};
}
function getStop (key) {
if (!data.stops || !data.stops[key]) return false;
return data.stops[key];
}
function addStop (stop, name) {
data.stops[name] = stop;
updateStoreData();
}
function addStopOld (stop) {
return new Promise((resolve, reject) => {
data.stops = data.stops || {};
delete stop.idx;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log('');
rl.question('What would you like to save this stop as?\n', (name) => {
if (!name) {
rl.close();
console.log('Yeah, you need to actually write something to choose a name?');
return console.log('Try again.');
}
data.stops[name] = stop;
updateStoreData();
console.log('');
console.log('Success!\n"' + stop.name + '" was saved with the name "' + name +'".');
console.log('');
rl.close();
resolve();
});
});
}
function removeStop (name) {
if (!getStop(name)) return console.log('Could not find the stop "' + name + '"');
delete data.stops[name];
updateStoreData();
console.log('The stop "' + name + '" was successfully removed.');
}
function getAllTrips () {
return (data.trips) ? data.trips:{};
}
function getTrip (name) {
if (!data.trips[name]) return false;
return data.trips[name];
}
function addTrip (trip, name) {
data.trips[name] = trip;
updateStoreData();
}
function addTripOld (trip) {
return new Promise((resolve, reject) => {
data.trips = data.trips || {};
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log('');
rl.question('What would you like to save this trip as?\n', (name) => {
if (!name) {
rl.close();
console.log('Yeah, you need to actually write something to choose a name?');
return console.log('Try again.');
}
data.trips[name] = trip;
updateStoreData();
const tripString = 'from "' + trip.origin.name + '" to "' + trip.destination.name + ((trip.via) ? '" via "' + trip.via.name +'"':'"');
console.log('');
console.log('Success!\nYour trip ' + tripString + ' was saved as "' + name + '"');
console.log('');
rl.close();
resolve();
});
});
}
function removeTrip (name) {
if (!getTrip(name)) return console.log('Could not find the trip "' + name + '".');
delete data.trips[name];
updateStoreData();
console.log('The trip "' + name + '" was successfully removed.');
}
function insertCharacters (string, number) {
let res = '';
for (let i = 0; i < number; i++) {
res += string;
}
return res;
}
function showHelp () {
console.log('');
console.log('\x1b[36m%s\x1b[0m', 'Tramz.');
console.log('');
console.log('Usage:');
console.log('\x1b[32m%s\x1b[0m', ' tramz <trip>');
console.log(' Will search a locally saved trip by name.');
console.log('');
console.log('\x1b[32m%s\x1b[0m', ' tramz <origin> <destination> <via (optional)>');
console.log(' Will search a specific trip from origin to destination, via an optional stop.');
console.log('');
console.log('\x1b[32m%s\x1b[0m', ' tramz stops');
console.log(' Will show a list of your locally saved stops.');
console.log('');
console.log('\x1b[32m%s\x1b[0m', ' tramz stops add <string>');
console.log(' Will find a stop with a name similar to the provided string, and save it locally.');
console.log('');
console.log('\x1b[32m%s\x1b[0m', ' tramz stops remove <name>');
console.log(' Will remove a locally saved stop by the provided name.');
console.log('');
console.log('\x1b[32m%s\x1b[0m', ' tramz trips');
console.log(' Will show a list of your locally saved trips.');
console.log('');
console.log('\x1b[32m%s\x1b[0m', ' tramz trips add <origin> <destination> <via (optional)>');
console.log(' Will search for a trip from origin to destination, via an optional stop, and save it locally.');
console.log('');
console.log('\x1b[32m%s\x1b[0m', ' tramz trips remove <name>');
console.log(' Will remove a locally saved trip based on the provided name.');
console.log('');
console.log('Options:');
console.log(' -h --help \t Will show this screen.');
console.log(' -v \t\t Will show the installed version.');
console.log('');
}
return {
data,
storeDir,
storeFile,
updateStoreData,
getToken,
getPlanKey,
getListKey,
buildStopSearchUrl,
buildTripSearchUrl,
trimTimeString,
trimLineName,
getAllStops,
getStop,
addStop,
addStopOld,
removeStop,
getAllTrips,
getTrip,
addTrip,
addTripOld,
removeTrip,
insertCharacters,
showHelp
}
}
module.exports = Core;