forked from MemeLabs/chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entities.go
199 lines (171 loc) · 4.46 KB
/
entities.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"regexp"
"time"
parser "github.com/MemeLabs/chat-parser"
"mvdan.cc/xurls/v2"
)
var entities *EntityExtractor
func init() {
var err error
entities, err = NewEntityExtractor()
if err != nil {
log.Fatal(err)
}
go entities.scheduleEmoteSync()
}
func loadEmoteManifest() ([]string, error) {
resp, err := http.Get("https://chat.strims.gg/emote-manifest.json")
if err != nil {
return nil, fmt.Errorf("failed to get emotes: %w", err)
}
defer resp.Body.Close()
manifest := struct {
Emotes []struct {
Name string `json:"name"`
} `json:"emotes"`
}{}
if err := json.NewDecoder(resp.Body).Decode(&manifest); err != nil {
return nil, fmt.Errorf("failed to parse emotes manifest: %w", err)
}
emotes := make([]string, len(manifest.Emotes))
for i, e := range manifest.Emotes {
emotes[i] = e.Name
}
return emotes, nil
}
func NewEntityExtractor() (*EntityExtractor, error) {
emotes, err := loadEmoteManifest()
if err != nil {
return nil, err
}
return &EntityExtractor{
parserCtx: parser.NewParserContext(parser.ParserContextValues{
Emotes: emotes,
Nicks: []string{},
Tags: []string{"nsfw", "weeb", "nsfl", "loud"},
EmoteModifiers: []string{"mirror", "flip", "rain", "snow", "rustle", "worth", "love", "spin", "wide", "lag", "hyper"},
}),
urls: xurls.Relaxed(),
}, nil
}
type EntityExtractor struct {
parserCtx *parser.ParserContext
urls *regexp.Regexp
}
func (x *EntityExtractor) scheduleEmoteSync() {
for range time.NewTicker(time.Minute).C {
emotes, err := loadEmoteManifest()
if err != nil {
log.Printf("failed to update emotes: %v", err)
continue
}
x.parserCtx.Emotes.Replace(parser.RunesFromStrings(emotes))
}
}
func (x *EntityExtractor) AddNick(nick string) {
x.parserCtx.Nicks.Insert([]rune(nick))
}
func (x *EntityExtractor) RemoveNick(nick string) {
x.parserCtx.Nicks.Remove([]rune(nick))
}
func (x *EntityExtractor) Extract(msg string) *Entities {
e := &Entities{}
for _, b := range x.urls.FindAllStringIndex(msg, -1) {
e.Links = append(e.Links, &Link{
URL: msg[b[0]:b[1]],
Bounds: [2]int{b[0], b[1]},
})
}
addEntitiesFromSpan(e, parser.NewParser(x.parserCtx, parser.NewLexer(msg)).ParseMessage())
return e
}
func addEntitiesFromSpan(e *Entities, span *parser.Span) {
switch span.Type {
case parser.SpanCode:
e.Codes = append(e.Codes, &Code{
Bounds: [2]int{span.Pos(), span.End()},
})
case parser.SpanSpoiler:
e.Spoilers = append(e.Spoilers, &Spoiler{
Bounds: [2]int{span.Pos(), span.End()},
})
case parser.SpanGreentext:
e.Greentext = &Generic{
Bounds: [2]int{span.Pos(), span.End()},
}
case parser.SpanMe:
e.Me = &Generic{
Bounds: [2]int{span.Pos(), span.End()},
}
}
EachNode:
for _, ni := range span.Nodes {
for _, l := range e.Links {
if l.Bounds[0] <= ni.Pos() && l.Bounds[1] >= ni.End() {
continue EachNode
}
}
switch n := ni.(type) {
case *parser.Emote:
e.Emotes = append(e.Emotes, &Emote{
Name: n.Name,
Modifiers: n.Modifiers,
Bounds: [2]int{n.Pos(), n.End()},
})
case *parser.Nick:
e.Nicks = append(e.Nicks, &Nick{
Nick: n.Nick,
Bounds: [2]int{n.Pos(), n.End()},
})
case *parser.Tag:
e.Tags = append(e.Tags, &Tag{
Name: n.Name,
Bounds: [2]int{n.Pos(), n.End()},
})
case *parser.Span:
addEntitiesFromSpan(e, n)
}
}
}
type Link struct {
URL string `json:"url,omitempty"`
Bounds [2]int `json:"bounds,omitempty"`
}
type Emote struct {
Name string `json:"name,omitempty"`
Modifiers []string `json:"modifiers,omitempty"`
Bounds [2]int `json:"bounds,omitempty"`
Combo int `json:"combo,omitempty"`
}
type Nick struct {
Nick string `json:"nick,omitempty"`
Bounds [2]int `json:"bounds,omitempty"`
}
type Tag struct {
Name string `json:"name,omitempty"`
Bounds [2]int `json:"bounds,omitempty"`
}
type Code struct {
Bounds [2]int `json:"bounds,omitempty"`
}
type Spoiler struct {
Bounds [2]int `json:"bounds,omitempty"`
}
type Generic struct {
Bounds [2]int `json:"bounds,omitempty"`
}
type Entities struct {
Links []*Link `json:"links,omitempty"`
Emotes []*Emote `json:"emotes,omitempty"`
Nicks []*Nick `json:"nicks,omitempty"`
Tags []*Tag `json:"tags,omitempty"`
Codes []*Code `json:"codes,omitempty"`
Spoilers []*Spoiler `json:"spoilers,omitempty"`
Greentext *Generic `json:"greentext,omitempty"`
Me *Generic `json:"me,omitempty"`
}