-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.go
226 lines (214 loc) · 5.02 KB
/
utils.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"fmt"
"github.com/Mihonarium/discordgo"
"github.com/getsentry/sentry-go"
"net/url"
"reflect"
"runtime"
"strconv"
"strings"
)
func getBodyToCompare(body string) string {
return "\n" + strings.ReplaceAll(strings.ToLower(replaceSlice(body, "", "'", "’", "`")), "what is", "whats") + "?"
}
func replaceSlice(s, replaceWith string, oldStrings ...string) string {
for _, old := range oldStrings {
s = strings.ReplaceAll(s, old, replaceWith)
}
return s
}
func linksFromMessage(m *discordgo.Message) []string {
links := make([]string, 0)
for _, a := range m.Attachments {
if a == nil {
continue
}
if a.URL != "" {
links = append(links, a.URL)
}
}
plaintextUrls := rxStrict.FindAllString(m.Content, -1)
for i := range plaintextUrls {
plaintextUrls[i] = strings.ReplaceAll(plaintextUrls[i], "\\", "")
if strings.HasPrefix(plaintextUrls[i], "/") {
continue
}
links = append(links, plaintextUrls[i])
}
return links
}
func GetTimeFromText(s string) (timeFrom, timeTo int) {
s = strings.ReplaceAll(s, " - ", "")
words := strings.Split(s, " ")
maxScore := 0
for _, w := range words {
score := 0
w2 := ""
if strings.Contains(w, "-") {
w2 = strings.Split(w, "-")[1]
w = strings.Split(w, "-")[0]
score += 1
}
w = strings.TrimSuffix(w, "s")
w2 = strings.TrimSuffix(w2, "s")
if strings.Contains(w, ":") {
score += 2
}
if score > maxScore {
t, err := TimeStringToSeconds(w)
if err == nil {
timeFrom = t
timeTo, _ = TimeStringToSeconds(w2) // if w2 is empty or not a correct time, timeTo is 0
maxScore = score
}
}
}
return
}
func TimeStringToSeconds(s string) (int, error) {
list := strings.Split(s, ":")
if len(list) > 3 {
return 0, fmt.Errorf("too many : thingies")
}
seconds, multiplier := 0, 1
for i := len(list) - 1; i >= 0; i-- {
c, err := strconv.Atoi(list[i])
if err != nil {
return 0, err
}
seconds += c * multiplier
multiplier *= 60
}
return seconds, nil
}
func SecondsToTimeString(i int, includeHours bool) string {
if includeHours {
return fmt.Sprintf("%02d:%02d:%02d", i/3600, (i%3600)/60, i%60)
}
return fmt.Sprintf("%02d:%02d", i/60, i%60)
}
func GetSkipFirstFromLink(link string) int {
skip := 0
if strings.HasSuffix(link, ".m3u8") {
return skip
}
u, err := url.Parse(link)
if err == nil {
t := u.Query().Get("t")
if t == "" {
t = u.Query().Get("time_continue")
if t == "" {
t = u.Query().Get("start")
}
}
if t != "" {
t = strings.ToLower(strings.ReplaceAll(t, "s", ""))
tInt := 0
if strings.Contains(t, "m") {
s := strings.Split(t, "m")
tsInt, _ := strconv.Atoi(s[1])
tInt += tsInt
if strings.Contains(s[0], "h") {
s := strings.Split(s[0], "h")
if tmInt, err := strconv.Atoi(s[1]); !capture(err) {
tInt += tmInt * 60
}
if thInt, err := strconv.Atoi(s[0]); !capture(err) {
tInt += thInt * 60 * 60
}
} else {
if tmInt, err := strconv.Atoi(s[0]); !capture(err) {
tInt += tmInt * 60
}
}
} else {
if tsInt, err := strconv.Atoi(t); !capture(err) {
tInt = tsInt
}
}
skip += tInt
fmt.Println("skip:", skip)
}
}
return skip
}
func stringInSlice(slice []string, s string) bool {
for i := range slice {
if s == slice[i] {
return true
}
}
return false
}
func substringInSlice(s string, slice []string) (exists bool, substring string) {
for i := range slice {
if strings.Contains(s, slice[i]) {
return true, slice[i]
}
}
return
}
func filterFrames(frames []sentry.Frame) []sentry.Frame {
if len(frames) == 0 {
return nil
}
filteredFrames := make([]sentry.Frame, 0, len(frames))
for _, frame := range frames {
if frame.Module == "runtime" || frame.Module == "testing" {
continue
}
if frame.Module == "main" && strings.HasPrefix(frame.Function, "capture") {
continue
}
filteredFrames = append(filteredFrames, frame)
}
return filteredFrames
}
func capture(err error) bool {
extractFrames := func(pcs []uintptr) []sentry.Frame {
var frames []sentry.Frame
callersFrames := runtime.CallersFrames(pcs)
for {
callerFrame, more := callersFrames.Next()
frames = append([]sentry.Frame{
sentry.NewFrame(callerFrame),
}, frames...)
if !more {
break
}
}
return frames
}
GetStacktrace := func() *sentry.Stacktrace {
pcs := make([]uintptr, 100)
n := runtime.Callers(1, pcs)
if n == 0 {
return nil
}
frames := extractFrames(pcs[:n])
frames = filterFrames(frames)
stacktrace := sentry.Stacktrace{
Frames: frames,
}
return &stacktrace
}
if err == nil {
return false
}
event := sentry.NewEvent()
event.Exception = append(event.Exception, sentry.Exception{
Value: err.Error(),
Type: reflect.TypeOf(err).String(),
Stacktrace: GetStacktrace(),
})
event.Level = sentry.LevelError
hub := sentry.CurrentHub()
client, scope := hub.Client(), hub.Scope()
go client.CaptureEvent(event, &sentry.EventHint{OriginalException: err}, scope)
go fmt.Println(err.Error())
return true
}
func captureFunc(f func() error) bool {
return capture(f())
}