-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (83 loc) · 2.26 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 (
"flag"
"net/http"
"os"
"sync"
"time"
"log"
"github.com/nlif-m/atomgen/config"
"github.com/nlif-m/atomgen/utils"
"github.com/nlif-m/atomgen/ytdlp"
)
var (
programConfig string
genConfig string
portConfig string
)
func main() {
// TODO: Add a ability to make this configs for each url individually
flag.StringVar(&genConfig, "genConfig", "", "generate default config file")
flag.StringVar(&programConfig, "config", "", "config file")
flag.StringVar(&portConfig, "address", ":3000", "address to listen")
flag.Parse()
if genConfig != "" {
err := config.WriteDefaultTo(genConfig)
if err != nil {
log.Fatalf("ERROR: failed to generate config to: %q:%q\n", genConfig, err)
}
os.Exit(0)
}
if programConfig == "" {
flag.Usage()
os.Exit(1)
}
cfg, err := config.NewFromFile(programConfig)
if err != nil {
log.Fatalf("ERROR: failed to read config from %q:%q\n", programConfig, err)
}
yt := ytdlp.New(cfg.YtdlpProgram)
atomgen := newAtomgen(yt, cfg)
fullUpdateChan := make(chan bool)
atomFileUpdateChan := make(chan bool)
// Run initial update
go func(fullUpdateChan chan bool, atomFileUpdateChan chan bool) {
atomFileUpdateChan <- true
fullUpdateChan <- true
}(fullUpdateChan, atomFileUpdateChan)
// Run Telegram bot
go func() {
TgBot(atomgen, atomFileUpdateChan)
}()
var wg sync.WaitGroup // Why i use it since it non needed
go func(fullUpdateChan chan bool, atomFileUpdateChan chan bool) {
for {
select {
case <-fullUpdateChan:
wg.Add(1)
log.Println("Receive fullUpdateChan request")
err = atomgen.fullUpdate()
utils.CheckErr(err)
err = atomgen.generateAtomFeed()
utils.CheckErr(err)
wg.Done()
case <-atomFileUpdateChan:
wg.Add(1)
log.Println("Receive atomFileUpdateChan request")
err = atomgen.generateAtomFeed()
utils.CheckErr(err)
wg.Done()
}
}
}(fullUpdateChan, atomFileUpdateChan)
// Run timer to call update every cfg.ProgramRestartIntervalMinutes minutes
go func() {
for {
time.Sleep(time.Duration(cfg.ProgramRestartIntervalMinutes) * time.Minute)
log.Println("Time to update, timer tick")
fullUpdateChan <- true
}
}()
http.Handle("/", http.FileServer(http.Dir(cfg.OutputFolder)))
log.Fatalln(http.ListenAndServe(portConfig, nil))
}