-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
130 lines (113 loc) · 4.16 KB
/
main.go
File metadata and controls
130 lines (113 loc) · 4.16 KB
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
package main
import (
"context"
"log/slog"
"os"
"time"
"tkpst_parser/config"
"tkpst_parser/parser"
"github.com/ThisIsHyum/osago"
"github.com/ThisIsHyum/osago/types"
)
type Parser struct {
p parser.Parser
campuses map[string]string
groups map[string][]string
}
func hm(hour, minute int) time.Time {
return time.Date(0, 0, 0, hour, minute, 0, 0, time.UTC)
}
func (p *Parser) GetCalls() ([]types.Call, error) {
saturday := []time.Weekday{time.Saturday}
allDays := []time.Weekday{time.Monday, time.Tuesday, time.Wednesday, time.Thursday, time.Friday}
calls := []types.Call{}
calls = append(calls, types.NewCalls(allDays, 1, hm(8, 30), hm(10, 00))...)
calls = append(calls, types.NewCalls(allDays, 2, hm(10, 10), hm(11, 40))...)
calls = append(calls, types.NewCalls(allDays, 3, hm(12, 20), hm(13, 50))...)
calls = append(calls, types.NewCalls(allDays, 4, hm(14, 00), hm(15, 30))...)
calls = append(calls, types.NewCalls(allDays, 5, hm(15, 40), hm(17, 10))...)
calls = append(calls, types.NewCalls(allDays, 6, hm(17, 20), hm(18, 50))...)
calls = append(calls, types.NewCalls(saturday, 1, hm(8, 30), hm(9, 30))...)
calls = append(calls, types.NewCalls(saturday, 2, hm(9, 40), hm(10, 40))...)
calls = append(calls, types.NewCalls(saturday, 3, hm(10, 50), hm(11, 50))...)
calls = append(calls, types.NewCalls(saturday, 4, hm(12, 10), hm(13, 10))...)
calls = append(calls, types.NewCalls(saturday, 5, hm(13, 20), hm(14, 20))...)
calls = append(calls, types.NewCalls(saturday, 6, hm(14, 30), hm(15, 30))...)
return calls, nil
}
func (p *Parser) GetStudentGroupNames(campusName string) ([]string, error) {
return p.groups[campusName], nil
}
func (p *Parser) SendLessons(groups map[string]uint, lessonsChan chan<- []types.Lesson) error {
for i := 1; ; i++ {
if i > 1 {
time.Sleep(10 * time.Minute)
}
slog.Info("sending lessons started", slog.Any("iter", i))
for campusName, id := range p.campuses {
slog.Info("fetching values", slog.Any("campus", campusName))
values, err := p.p.GetValues(id)
if err != nil {
slog.Error("failed to get values", slog.Any("campus", campusName), slog.Any("error", err))
continue
}
slog.Info("getting lessons", slog.Any("campus", campusName))
lessons, err := p.p.GetLessons(values, groups)
if err != nil {
slog.Error("failed to get lessons", slog.Any("campus", campusName), slog.Any("error", err))
continue
}
slog.Info("sending lessons to channel", slog.Any("campus", campusName), slog.Any("lessons_count", len(lessons)))
lessonsChan <- lessons
}
slog.Info("sending lessons ended", slog.Any("iter", i))
}
}
func NewParser(ctx context.Context, campuses map[string]string) (*Parser, error) {
parser := parser.New()
groups := map[string][]string{}
for campusName, campusId := range campuses {
values, err := parser.GetValues(campusId)
if err != nil {
return nil, err
}
groups[campusName] = parser.GetGroupNames(values)
}
return &Parser{
campuses: campuses,
p: parser,
groups: groups,
}, nil
}
func main() {
ctx := context.Background()
config, err := config.LoadConfig()
if err != nil {
slog.Error("unable load config", "error", err)
os.Exit(1)
}
slog.Info("config is loaded")
client, err := osago.NewParserClient(ctx, config.OsaUrl, config.Token, 10*time.Second)
if err != nil {
slog.Error("unable to create parser client", "error", err)
os.Exit(1)
}
slog.Info("client is created", slog.Any("osa_url", config.OsaUrl))
parser, err := NewParser(ctx, map[string]string{
"Самарцева": "15yQ7MCTqWIIvfb3Qw9ie-4Off7bpR7ovSwbhXw1Iuyg",
"Рылеева": "11y4dLT68xrStKvDSC7LmCNxrGXhkWsRMjOOYHWYDyc0",
"Луначарского 1 курс": "1kTvUP7cH-8l1yJf9cgQ8-hxkk9cPo3N67miH8Nu0abA",
"Луначарского 2 курс": "1PqnXQrm84iRwrR8obKysz6UCZXqbxWuDkhc9c2VYAKY",
"Луначарского 3 и 4 курс": "1h1nu_K66V5KfQDy72rCKXueWSUn84mwl4kQI0aer5B4",
})
if err != nil {
slog.Error("unable to create parser", "error", err)
os.Exit(1)
}
slog.Info("parser is created")
client.SetParser(parser)
if err := client.Run(ctx); err != nil {
slog.Error("unable to run parser client", "error", err)
os.Exit(1)
}
}