-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
94 lines (75 loc) · 1.7 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
package main
import (
"math/rand"
"net/http"
"net/url"
"reflect"
"regexp"
"strings"
"gopkg.in/telebot.v3/react"
)
func isURL(s string) bool {
u, err := url.Parse(s)
if err != nil || u.Scheme == "" || u.Host == "" {
return false
}
return true
}
func urlExists(url string) bool {
resp, err := http.Get(url)
if err != nil {
return false
}
defer resp.Body.Close()
return resp.StatusCode == http.StatusOK
}
func hostnameOf(s string) string {
parts := strings.Split(s, ".")
if len(parts) >= 2 {
return parts[len(parts)-2]
}
return ""
}
func reverseSlice(s interface{}) interface{} {
val := reflect.ValueOf(s)
if val.Kind() != reflect.Slice {
return nil
}
reversed := reflect.MakeSlice(val.Type(), val.Len(), val.Cap())
for i := 0; i < val.Len(); i++ {
reversed.Index(i).Set(val.Index(val.Len() - 1 - i))
}
return reversed.Interface()
}
func whichFormat(f string) int {
vPattern := regexp.MustCompile(`\d{3,4}x\d{3,4}`)
if vPattern.MatchString(f) || strings.Contains(f, "video") {
return Video
}
if strings.Contains(f, "audio") {
return Audio
}
return Unknown
}
// getEmojiFor returns an emoji based on the provided URL string.
func getEmojiFor(s string) react.Reaction {
parsedUrl, err := url.Parse(s)
if err != nil {
return getRandomEmoji()
}
for _, data := range sources {
urls := strings.Split(data, "\n")
for _, u := range urls {
mn := parsedUrl.Hostname()
if hostnameOf(mn) == hostnameOf(u) {
return react.EvilFace
}
}
}
return getRandomEmoji()
}
// getRandomEmoji returns a random emoji from a predefined list.
func getRandomEmoji() react.Reaction {
options := []react.Reaction{react.Eyes, react.Sunglasses}
return options[rand.Intn(len(options))]
}