-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmensaplan.js
141 lines (106 loc) · 3.34 KB
/
mensaplan.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: orange; icon-glyph: utensils;
/*
note:
-> you can filter vegan/vegetarian meals and hide salads (only LUH)
by using the variables below
-> if you're vegeterian or vegan and the salad isn't shown,
a medium sized widget is enough, otherwise you have to
add a large one
-> the widget is made for the Leibniz Universität in Hanover, but it also works
for others. Please check GitHub for more information.
*/
mensaID = 6
vegan = false
vegetarian = false
priceType = "students" //students, employees, others
accentColor = Color.red()
showTomorrowAt = 18 //time from which on the menu of the next day is shown (in hours); change to 24 for midnight
hidden_categories = ["QUEERBEET"]
veggieTag = "ohne Fleisch"
//beginnig of the code
const url = "https://openmensa.org/api/v2/canteens/" + mensaID + "/days/" + getIsoDate() + "/meals"
let widget = new ListWidget()
//get weekday as string
let df = new DateFormatter()
df.dateFormat = 'EEEE'
let wd = df.string(nextWeekday())
//title
const head = widget.addStack()
head.layoutHorizontally()
head.setPadding(0, 0, 0, 0)
//Mensaplna-Label
title = head.addText("Mensaplan")
title.leftAlignText()
title.font = Font.boldRoundedSystemFont(16)
//Weekday-Label
const subHead = head.addStack()
subHead.setPadding(0, 4, 0, 0)
wd = subHead.addText(wd)
wd.leftAlignText()
wd.textColor = accentColor
wd.font = Font.regularRoundedSystemFont(16)
widget.addSpacer(12)
//load meals from API
try {
meals = await new Request(url).loadJSON()
//the loop creates a new entry for every loaded meal
for (meal of meals) {
mealIsVegan = meal.notes.includes("vegan")
mealIsVegetarian = meal.notes.includes(veggieTag) || mealIsVegan
if ((mealIsVegan || !vegan) && (mealIsVegetarian || !vegetarian) && (!hidden_categories.includes(meal.category))) {
label = widget.addText(meal.name)
label.font = Font.regularSystemFont(12)
label = widget.addText(meal.prices[priceType] + "€")
label.rightAlignText()
label.textColor = accentColor
label.font = Font.boldSystemFont(10)
}
}
} catch (error) {
error_string = String(error).toLowerCase()
error_msg = "Für diesen Tag liegt kein Plan vor."
if (error_string.includes("internet")){
error_msg = "Es gibt Probleme mit der Internetverbindung."
}
widget.addSpacer()
label = widget.addText(error_msg)
label.font = Font.regularSystemFont(14)
label.centerAlignText()
}
widget.addSpacer()
widget.presentLarge()
Script.setWidget(widget)
Script.complete()
/**
* the function returns the date of the next weekday
* as a string in ISO 8601 format
* @returns {String} - the date string
*/
function getIsoDate() {
var dateTime = nextWeekday()
let df = new DateFormatter()
df.dateFormat = 'yyyy-MM-dd'
df.locale = 'de'
return df.string(dateTime)
}
/**
* the function returns the date of today or the next weekday
* (depends on if it is after the in showTomorrowAt configured time or week-end)
* @returns {Date} - next weekday's date
*/
function nextWeekday() {
var date = new Date()
if (date.getHours() >= showTomorrowAt) {
date.setDate(date.getDate() + 1);
}
let df = new DateFormatter()
df.dateFormat = 'EEEEE'
var wd = df.string(date)
while (wd == 'S') {
date.setDate(date.getDate() + 1);
wd = df.string(date);
}
return date
}