-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
275 lines (211 loc) · 6.36 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
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
/* Config */
const BASE_URL = 'https://www.rmv.de/hapi/latest';
let API_KEY = args.widgetParameter;
const FALLBACK_API_KEY = ''; // paste in your api key for debugging inside scriptable app
const MAX_JOURNEYS = 4;
const URLS = {
stops: BASE_URL + '/location.nearbystops',
departures: BASE_URL + '/departureBoard',
}
const NOW = new Date().toLocaleTimeString('de-DE', { hour: '2-digit', minute: '2-digit' });
const MAX_CHAR_LENGTH = 24;
const GRADIENT_COLORS = ['#4b749f', '#243748'];
const gradient = new LinearGradient();
gradient.colors = GRADIENT_COLORS.map(color => new Color(color));
gradient.locations = [0, 1];
if (!API_KEY || !API_KEY.length) {
console.warn('No API_KEY found!');
if (!args.runsInWidget) {
API_KEY = FALLBACK_API_KEY;
}
}
const DEFAULT_PARAMS = {
accessId: API_KEY,
format: 'json',
}
const fm = FileManager.local()
const dir = fm.documentsDirectory()
const path = fm.joinPath(dir, "location.json")
const widget = await createWidget();
if (!config.runsInWidget) {
widget.presentMedium();
}
Script.setWidget(widget);
Script.complete();
async function createWidget() {
const { currentStop, departures, error } = await getData();
if (error) {
return ErrorWidget(error.title, error.message);
}
let widget = new ListWidget();
widget.backgroundGradient = gradient;
let nextRefresh = Date.now() + 1000 * 30
widget.refreshAfterDate = new Date(nextRefresh)
widget.useDefaultPadding();
let headline = widget.addText('🚍 ' + currentStop);
headline.lineLimit = 1;
headline.font = Font.mediumSystemFont(16)
widget.addSpacer();
let depStack = widget.addStack();
depStack.layoutVertically();
departures.forEach((dep, idx) => {
if (idx >= MAX_JOURNEYS) return;
let rowStack = depStack.addStack();
rowStack.setPadding(2, 4, 2, 4)
rowStack.cornerRadius = 5;
if (idx % 2) {
rowStack.backgroundColor = new Color('#11111160');
} else {
rowStack.backgroundColor = new Color('#22222260');
}
let lineCell = rowStack.addStack();
lineCell.layoutVertically();
lineCell.size = new Size(60, 16);
let line = lineCell.addText(dep.line);
line.leftAlignText();
line.font = Font.mediumMonospacedSystemFont(14)
rowStack.addSpacer(16);
let time = rowStack.addText(dep.time);
time.font = Font.regularMonospacedSystemFont(14)
rowStack.addSpacer();
let dir = rowStack.addText(dep.direction);
dir.font = Font.regularMonospacedSystemFont(14)
dir.rightAlignText();
depStack.addSpacer(2);
})
const lastUpdatedStack = widget.addStack();
lastUpdatedStack.setPadding(2, 4, 2, 4);
lastUpdatedStack.addSpacer();
const lastUpdated = lastUpdatedStack.addText('Last Update: ' + NOW)
lastUpdated.font = Font.lightSystemFont(10);
lastUpdated.textColor = new Color('#eeeeee');
lastUpdatedStack.addSpacer();
return widget;
}
async function fetch(url, params) {
const query = Object.keys(params).map(key => key + '=' + params[key]).join('&');
const fetchUrl = url + '?' + query;
try {
const data = await new Request(fetchUrl).loadJSON();
return data;
} catch (e) {
console.error('Error fetching data');
console.error(e);
return null;
}
}
async function getLocation() {
try {
const { latitude, longitude } = await Location.current();
console.log(`Lat: ${latitude}, Long: ${longitude}`);
const location = { lat: latitude, long: longitude };
fm.writeString(path, JSON.stringify(location, null, 2));
return location;
} catch (e) {
console.error('Cannot get location');
console.error(e);
const cachedLocation = fm.readString(path);
if (!cachedLocation || cachedLocation === '') return null;
console.log('Using cached location');
const cLoc = JSON.parse(cachedLocation, null)
return cLoc
}
}
async function getNearbyStop(location) {
const { lat, long } = location;
console.log(location);
const params = {
...DEFAULT_PARAMS,
originCoordLat: lat,
originCoordLong: long,
};
console.log(params);
try {
const data = await fetch(URLS.stops, params)
if (!data || !data.stopLocationOrCoordLocation) {
throw new Error('No stops found');
}
const stopId = data.stopLocationOrCoordLocation[0].StopLocation.extId;
const name = data.stopLocationOrCoordLocation[0].StopLocation.name;
return { name, stopId };
} catch (e) {
console.error('Error fetching nearby stops');
console.error(e);
return null;
}
}
function replace(str) {
const REPLACEMENTS = {
'Frankfurt (Main)': 'FFM',
'Hauptbahnhof': 'Hbf',
'Bahnhof': 'Bf',
'Flughafen': '🛫',
}
const replaced = Object.entries(REPLACEMENTS).reduce((acc, [key, value]) => {
return acc.replace(key, value);
}, str);
return replaced;
}
async function getDepartures(stopId) {
const params = {
...DEFAULT_PARAMS,
id: stopId,
maxJourneys: MAX_JOURNEYS
}
const data = await fetch(URLS.departures, params)
if (!data || !data.Departure) {
return null;
}
const sanitizeDir = (direction) => {
const replaced = replace(direction);
const shortened = replaced.length >= MAX_CHAR_LENGTH ? replaced.substring(0, MAX_CHAR_LENGTH) + '...' : replaced;
return shortened;
}
return data.Departure.map(dep => ({
line: dep.name,
time: (dep.rtTime || dep.time).slice(0, -3),
direction: sanitizeDir(dep.direction)
}));
}
function ErrorWidget(title, message) {
let errorWidget = new ListWidget();
errorWidget.backgroundGradient = gradient;
let stack = errorWidget.addStack();
stack.layoutVertically();
stack.centerAlignContent();
let widgetTitle = stack.addText(title);
if (message) {
widgetTitle.font = Font.title2();
stack.addText(message);
}
return errorWidget;
}
async function getData() {
const location = await getLocation();
if (!location) return {
error: {
title: `Can't get location!`,
message: `Please try again later`
}
}
const nearByStop = await getNearbyStop(location);
if (!nearByStop) {
return {
error: {
title: `Can't get nearby stops!`,
message: `Please try again later`
}
}
}
const { name, stopId } = nearByStop;
const departures = await getDepartures(stopId);
if (!departures) {
return {
error: {
title: `Can't get departures!`,
message: `Please try again later`
}
}
}
return { currentStop: name, departures };
}