-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·92 lines (83 loc) · 2.13 KB
/
main.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
86
87
88
89
90
91
92
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"time"
)
const (
linsen = "b672efaf-032a-4bb8-d2a5"
expressen = "3d519481-1667-4cad-d2a3"
)
type restaurant struct {
DisplayName string
URL string
}
func main() {
var arg = 1
if len(os.Args) > 1 {
arg, _ = strconv.Atoi(os.Args[1])
}
getFood(arg)
}
func getFood(arg int) {
restaurants := []restaurant{restaurant{
DisplayName: "Express",
URL: expressen,
}, restaurant{
DisplayName: "Linsen",
URL: linsen,
}}
restaurantChannels := make([]chan []*Response, len(restaurants))
restaurantResponses := make([][]*Response, len(restaurants))
for i := range restaurantChannels {
restaurantChannels[i] = make(chan []*Response)
}
for i := range restaurants {
go getFoodFromRestaurant(arg, &(restaurants[i]), restaurantChannels[i])
}
for i, c := range restaurantChannels {
restaurantResponses[i] = <-c
}
for i, r := range restaurants {
PrintColor(Green, r.DisplayName)
for _, res := range restaurantResponses[i] {
englishFirst := r.DisplayName == linsen
PrintFood(res, englishFirst)
}
}
}
func getFoodFromRestaurant(arg int, restaurant *restaurant, c chan []*Response) {
channels := make([]chan *Response, arg)
for i := range channels {
channels[i] = make(chan *Response)
}
for i, channel := range channels {
go getOneDay(restaurant, i, channel)
}
res := make([]*Response, arg)
for i := 0; i < arg; i++ {
res[i] = <-channels[i]
}
c <- res
}
func getOneDay(restaurant *restaurant, daysFromNow int, c chan *Response) {
res, err := http.Get(getURL((*restaurant).URL, daysFromNow))
if err != nil {
panic("failed to get")
}
defer res.Body.Close()
var result Response
json.NewDecoder(res.Body).Decode(&result)
c <- &result
}
func getURL(restaurant string, daysAgo int) string {
formatedDate := daysAgoString(daysAgo)
return fmt.Sprintf("http://carbonateapiprod.azurewebsites.net/api/v1/mealprovidingunits/%s-08d558129279/dishoccurrences?startDate=%s", restaurant, formatedDate)
}
func daysAgoString(daysFromNow int) string {
t := time.Now().AddDate(0, 0, daysFromNow)
return (t.Format(time.RFC3339)[:10])
}