-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
207 lines (167 loc) · 4.99 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
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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"sync"
"time"
)
type VaccinationCenter struct {
Name string `json:"Zentrumsname"`
Zip string `json:"PLZ"`
City string `json:"Ort"`
State string `json:"Bundesland"`
BaseUrl string `json:"URL"`
Address string `json:"Adresse"`
}
type LocationResponse struct {
VaccinationAvailable bool `json:"termineVorhanden"`
}
const (
vaccinationsUrl = "https://001-iz.impfterminservice.de/assets/static/its/vaccination-list.json"
vaccinationCentersUrl = "https://www.impfterminservice.de/assets/static/impfzentren.json"
appointmentCheckUrlPart = "rest/suche/termincheck?plz=%v&leistungsmerkmale=%v"
appointmentLinkPart = "impftermine/service?plz=%v"
)
func main() {
// the shared http client
httpClient := http.Client{
Timeout: time.Second * 10,
}
// the CLI flags
vaccineCode := flag.String("vaccineCode", "", "Vaccine Code (L920, L921, L922)")
zipCodes := flag.String("zipCodes", "", "Zip Codes (comma separated, no-spaces)")
listVaccines := flag.Bool("listVaccines", false, "List vaccine details & codes")
flag.Parse()
if *listVaccines {
vaccines, err := getVaccines(&httpClient)
if err != nil {
panic(err)
}
fmt.Print(vaccines)
os.Exit(1)
}
if *vaccineCode == "" || *zipCodes == "" {
flag.PrintDefaults()
os.Exit(1)
}
// we only care about zip codes, so convert the received json response to a map of locations using zipCode as key
vaccinationCentersByZip, err := getVaccinationCentersByZip(&httpClient)
if err != nil {
panic(err)
}
// handle available locations (print it to the console)
availableLocations := make(chan VaccinationCenter, 0)
go func() {
for availableLoc := range availableLocations {
// print location details
fmt.Printf("\n%v, %v %v - %v: \n", availableLoc.Zip, availableLoc.Address, availableLoc.City, availableLoc.Name)
// print url to make an appointment
fmt.Printf(availableLoc.BaseUrl+appointmentLinkPart+"\n\n", availableLoc.Zip)
}
}()
// asynchronously query all locations
var wg sync.WaitGroup
for _, zipCode := range strings.Split(*zipCodes, ",") {
// each zip code can have multiple vaccination centers, so we also check each of them
for _, vaccinationCenter := range vaccinationCentersByZip[zipCode] {
wg.Add(1)
go func(vc string, loc *VaccinationCenter) {
defer wg.Done()
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf(loc.BaseUrl+appointmentCheckUrlPart, loc.Zip, vc), nil)
if err != nil {
panic(err)
}
addHeaders(req)
// call the service
resp, err := httpClient.Do(req)
if err != nil {
panic(err)
}
// extract the response
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
// unmarshal the json response to go structs
var response LocationResponse
err = json.Unmarshal(body, &response)
if err != nil {
panic(err)
}
// if there are appointments available, pass it to the channel for processing
if response.VaccinationAvailable {
availableLocations <- *loc
}
}(*vaccineCode, &vaccinationCenter)
}
}
// wait until all locations are queried
wg.Wait()
time.Sleep(1 * time.Second)
fmt.Println("Finished searching.")
}
func getVaccines(httpClient *http.Client) (string, error) {
req, err := http.NewRequest(http.MethodGet, vaccinationsUrl, nil)
if err != nil {
return "", err
}
addHeaders(req)
resp, err := httpClient.Do(req)
if err != nil {
return "", err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
func getVaccinationCentersByZip(httpClient *http.Client) (map[string][]VaccinationCenter, error) {
// create the map
vaccinationCenterByZip := make(map[string][]VaccinationCenter)
// get the vaccination centers
req, err := http.NewRequest(http.MethodGet, vaccinationCentersUrl, nil)
if err != nil {
panic(err)
}
addHeaders(req)
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var rawResponse map[string]json.RawMessage
err = json.Unmarshal(body, &rawResponse)
if err != nil {
return nil, err
}
vaccinationCentersByState := make(map[string][]VaccinationCenter)
for k, v := range rawResponse {
var vaccinationCenter []VaccinationCenter
err = json.Unmarshal(v, &vaccinationCenter)
if err != nil {
return nil, err
}
vaccinationCentersByState[k] = vaccinationCenter
}
// convert it to a map based on zip code as key (multiple per zip possible (ex: 77656 Offenburg)
for _, l := range vaccinationCentersByState {
for _, v := range l {
vaccinationCenterByZip[v.Zip] = append(vaccinationCenterByZip[v.Zip], v)
}
}
return vaccinationCenterByZip, nil
}
// the REST service appears to need these headers or it just times out
func addHeaders(req *http.Request) {
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux i686; rv:86.0) Gecko/20100101 Firefox/86.0")
req.Header.Set("Accept", "application/json")
}