-
Notifications
You must be signed in to change notification settings - Fork 0
/
mensa.go
85 lines (72 loc) · 1.49 KB
/
mensa.go
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
package main
import (
"fmt"
"os"
"github.com/BenchR267/goalfred"
"github.com/kiliankoe/swdd/speiseplan"
)
var searchMensa = os.Getenv("STANDARD_MENSA")
var isStudent = os.Getenv("STUDENTENPREIS")
func main() {
defer goalfred.Print()
arg, err := goalfred.NormalizedArguments()
handleError(err)
if arg[0] != "" {
searchMensa = arg[0]
}
meals, canteenName, err := speiseplan.GetCurrentForCanteen(searchMensa)
handleError(err)
invalid := false
if len(meals) > 0 {
goalfred.Add(goalfred.Item{
Title: fmt.Sprintf("Heute @ %s:", canteenName),
Valid: &invalid,
})
} else {
goalfred.Add(goalfred.Item{
Title: fmt.Sprintf("Heute gibt's leider nichts @ %s 😢", canteenName),
Valid: &invalid,
})
return
}
for _, meal := range meals {
item := goalfred.Item{
Title: formatTitle(meal),
Subtitle: meal.PageURL,
Arg: meal.PageURL,
Quicklook: meal.ImageURL,
}
goalfred.Add(item)
}
}
func formatTitle(meal *speiseplan.Meal) (title string) {
title += meal.Name
price := ""
if isStudent == "1" {
price = formatPrice(meal.StudentPrice)
} else {
price = formatPrice(meal.EmployeePrice)
}
if price != "" {
title += " - "
title += price
}
return
}
func formatPrice(price float64) string {
if price != 0 {
return fmt.Sprintf("%.2f€", price)
}
return ""
}
func handleError(err error) {
if err == nil {
return
}
goalfred.Add(goalfred.Item{
Title: "Unerwarteter Fehler 😲",
Subtitle: err.Error(),
})
goalfred.Print()
os.Exit(1)
}