-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
100 lines (83 loc) · 2.31 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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"os"
"sort"
"time"
"github.com/diurnalist/ourkino/internal/config"
"github.com/diurnalist/ourkino/internal/model"
"github.com/diurnalist/ourkino/internal/renderer"
"github.com/diurnalist/ourkino/internal/scraper"
"golang.org/x/sync/errgroup"
)
func StartOfDay(t time.Time) time.Time {
year, month, day := t.Date()
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
}
func main() {
var err error
configFile := flag.String("config-file", "config.json", "path to a JSON config file")
output := flag.String("output", "html", "type of output to generate")
days := flag.Int("days", 2, "number of days to collect showtimes for, from today")
flag.Parse()
rawConfig, err := os.ReadFile(*configFile)
check(err)
conf := config.Config{}
err = json.Unmarshal(rawConfig, &conf)
check(err)
timeLoc, err := time.LoadLocation(conf.Timezone)
check(err)
switch {
case len(conf.Theatres) < 1:
check(fmt.Errorf("no theatres defined in configuration"))
}
if conf.Days == 0 {
conf.Days = *days
}
date, dayRange := StartOfDay(time.Now().In(timeLoc)), make([]time.Time, 1)
dayRange[0] = date
for i := 1; i < conf.Days; i++ {
date = date.Add(time.Hour * 24)
dayRange = append(dayRange, date)
}
errs, _ := errgroup.WithContext(context.Background())
// Pass the set of all showtimes for a given theatre on a channel
chanMap := make([]chan []model.Showtime, len(conf.Theatres))
for i, theatreConf := range conf.Theatres {
ch := make(chan []model.Showtime, 1)
scraper, err := scraper.Instance(theatreConf.Driver, theatreConf.DriverArgs)
check(err)
errs.Go(func() error {
return scraper.Scrape(ch, dayRange, timeLoc)
})
chanMap[i] = ch
}
err = errs.Wait()
check(err)
entries := make([]model.ShowtimeEntry, 0)
for i, theatreConf := range conf.Theatres {
for _, showtime := range <-chanMap[i] {
entries = append(entries, model.ShowtimeEntry{Theatre: theatreConf.Name, Showtime: showtime})
}
}
sort.Slice(entries, func(i, j int) bool {
return entries[i].Showtime.When.Before(entries[j].Showtime.When)
})
var r renderer.Renderer
switch *output {
case "html":
r = renderer.HtmlRenderer{}
default:
r = renderer.ConsoleRenderer{}
}
err = r.Render(entries)
check(err)
}
func check(e error) {
if e != nil {
panic(e)
}
}