-
Notifications
You must be signed in to change notification settings - Fork 41
/
meiliExporter.go
134 lines (120 loc) · 3.65 KB
/
meiliExporter.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
package tools
import (
"errors"
"fmt"
"github.com/asticode/go-astisub"
"github.com/TUM-Dev/gocast/dao"
"github.com/meilisearch/meilisearch-go"
log "github.com/sirupsen/logrus"
"strings"
)
type MeiliStream struct {
ID uint `json:"ID"`
Name string `json:"name"`
Description string `json:"description"`
CourseName string `json:"courseName"`
Year int `json:"year"`
TeachingTerm string `json:"semester"`
CourseID uint `json:"courseID"`
}
type MeiliSubtitles struct {
ID string `json:"ID"` // meili id: streamID + timestamp
StreamID uint `json:"streamID"`
Timestamp int64 `json:"timestamp"`
TextPrev string `json:"textPrev"` // the previous subtitle line
Text string `json:"text"`
TextNext string `json:"textNext"` // the next subtitle line
}
type MeiliExporter struct {
c *meilisearch.Client
d dao.DaoWrapper
}
func NewMeiliExporter(d dao.DaoWrapper) *MeiliExporter {
c, err := Cfg.GetMeiliClient()
if err != nil && errors.Is(err, ErrMeiliNotConfigured) {
return nil
} else if err != nil {
log.WithError(err).Error("could not get meili client")
return nil
}
return &MeiliExporter{c, d}
}
func (m *MeiliExporter) Export() {
if m == nil {
return
}
index := m.c.Index("STREAMS")
_, err := m.c.Index("SUBTITLES").DeleteAllDocuments()
if err != nil {
log.WithError(err).Warn("could not delete all old subtitles")
}
m.d.StreamsDao.ExecAllStreamsWithCoursesAndSubtitles(func(streams []dao.StreamWithCourseAndSubtitles) {
meilistreams := make([]MeiliStream, len(streams))
streamIDs := make([]uint, len(streams))
for i, stream := range streams {
streamIDs[i] = stream.ID
meilistreams[i] = MeiliStream{
ID: stream.ID,
CourseID: stream.CourseID,
Name: stream.Name,
Description: stream.Description,
CourseName: stream.CourseName,
Year: stream.Year,
TeachingTerm: stream.TeachingTerm,
}
if stream.Subtitles != "" {
meiliSubtitles := make([]MeiliSubtitles, 0)
vtt, err := astisub.ReadFromWebVTT(strings.NewReader(stream.Subtitles))
if err != nil {
log.WithError(err).Warn("could not parse subtitles")
continue
}
for i, _ := range vtt.Items {
sub := MeiliSubtitles{
ID: fmt.Sprintf("%d-%d", stream.ID, vtt.Items[i].StartAt.Milliseconds()),
StreamID: stream.ID,
Timestamp: vtt.Items[i].StartAt.Milliseconds(),
Text: vtt.Items[i].String(),
}
if i > 0 {
sub.TextPrev = meiliSubtitles[i-1].Text
meiliSubtitles[i-1].TextNext = sub.Text
}
meiliSubtitles = append(meiliSubtitles, sub)
}
if len(meiliSubtitles) > 0 {
_, err := m.c.Index("SUBTITLES").AddDocuments(&meiliSubtitles, "ID")
if err != nil {
log.WithError(err).Error("issue adding subtitles to meili")
}
}
}
}
_, err := index.AddDocuments(&meilistreams, "ID")
if err != nil {
log.WithError(err).Error("issue adding documents to meili")
}
})
}
func (m *MeiliExporter) SetIndexSettings() {
if m == nil {
return
}
index := m.c.Index("STREAMS")
synonyms := map[string][]string{
"W": {"Wintersemester", "Winter", "WS", "WiSe"},
"S": {"Sommersemester", "Sommer", "SS", "SoSe", "Summer"},
}
_, err := index.UpdateSynonyms(&synonyms)
if err != nil {
log.WithError(err).Error("could not set synonyms for meili index STREAMS")
}
_, err = m.c.Index("SUBTITLES").UpdateSettings(&meilisearch.Settings{
FilterableAttributes: []string{"streamID", "courseID"},
SearchableAttributes: []string{"text"},
SortableAttributes: []string{"timestamp"},
})
if err != nil {
log.WithError(err).Warn("could not set settings for meili index SUBTITLES")
}
}