-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_reddit.go
149 lines (120 loc) · 4.06 KB
/
url_reddit.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
package url
import (
"fmt"
"net/url"
"regexp"
"github.com/seabird-chat/seabird-go/pb"
"github.com/seabird-irc/seabird-url-plugin/internal"
)
type redditUser struct {
Data struct {
Name string `json:"name"`
LinkKarma int `json:"link_karma"`
CommentKarma int `json:"comment_karma"`
IsGold bool `json:"is_gold"`
IsMod bool `json:"is_mod"`
} `json:"data"`
}
type redditSub struct {
Data struct {
URL string `json:"url"`
Subscribers int `json:"subscribers"`
Description string `json:"public_description"`
Actives int `json:"accounts_active"`
} `json:"data"`
}
type redditComment struct {
Data struct {
Children []struct {
Data struct {
Title string `json:"title"`
Author string `json:"author"`
Score int `json:"score"`
Subreddit string `json:"subreddit"`
} `json:"data"`
} `json:"children"`
} `json:"data"`
}
var (
redditPrefix = "[Reddit]"
// /r/subreddit
redditPrivmsgSubRegex = regexp.MustCompile(`(?:\s|^)/?r/([^\s/]+)`)
// /u/username
redditPrivmsgUserRegex = regexp.MustCompile(`(?:\s|^)/?(?:u|user)/([^\s/]+)`)
// URL matches
redditUserRegex = regexp.MustCompile(`^/(?:u|user)/([^\s/]+)$`)
redditCommentRegex = regexp.MustCompile(`^/r/[^/]+/comments/([^/]+)/.*$`)
redditSubRegex = regexp.MustCompile(`^/r/([^\s/]+)/?.*$`)
)
type RedditProvider struct{}
func NewRedditProvider() *RedditProvider {
return &RedditProvider{}
}
func (p *RedditProvider) GetCallbacks() map[string]URLCallback {
return map[string]URLCallback{
"reddit.com": redditCallback,
"old.reddit.com": redditCallback,
}
}
func (p *RedditProvider) GetMessageCallback() MessageCallback {
return redditPrivmsgCallback
}
func redditPrivmsgCallback(c *Client, source *pb.ChannelSource, text string) {
for _, matches := range redditPrivmsgSubRegex.FindAllStringSubmatch(text, -1) {
redditGetSub(c, source, matches[1])
}
for _, matches := range redditPrivmsgUserRegex.FindAllStringSubmatch(text, -1) {
redditGetUser(c, source, matches[1])
}
}
func redditCallback(c *Client, source *pb.ChannelSource, u *url.URL) bool {
text := u.Path
//nolint:gocritic
if matches := redditUserRegex.FindStringSubmatch(text); len(matches) == 2 {
return redditGetUser(c, source, matches[1])
} else if matches := redditCommentRegex.FindStringSubmatch(text); len(matches) == 2 {
return redditGetComment(c, source, matches[1])
} else if matches := redditSubRegex.FindStringSubmatch(text); len(matches) == 2 {
return redditGetSub(c, source, matches[1])
}
return false
}
func redditGetUser(c *Client, source *pb.ChannelSource, text string) bool {
ru := &redditUser{}
if err := internal.GetJSON(fmt.Sprintf("https://www.reddit.com/user/%s/about.json", text), ru); err != nil {
return false
}
// jsvana [gold] has 1 link karma and 1337 comment karma
gold := ""
if ru.Data.IsGold {
gold = " [gold]"
}
c.Replyf(source, "%s %s%s has %d link karma and %d comment karma", redditPrefix, ru.Data.Name, gold, ru.Data.LinkKarma, ru.Data.CommentKarma)
return true
}
func redditGetComment(c *Client, source *pb.ChannelSource, text string) bool {
rc := []redditComment{}
if err := internal.GetJSON(fmt.Sprintf("https://www.reddit.com/comments/%s.json", text), rc); err != nil || len(rc) < 1 {
return false
}
cm := rc[0].Data.Children[0].Data
// Title title - jsvana (/r/vim, score: 5)
c.Replyf(source, "%s %s - %s (/r/%s, score: %d)", redditPrefix, cm.Title, cm.Author, cm.Subreddit, cm.Score)
return true
}
func redditGetSub(c *Client, source *pb.ChannelSource, text string) bool {
rs := &redditSub{}
if err := internal.GetJSON(fmt.Sprintf("https://www.reddit.com/r/%s/about.json", text), rs); err != nil {
return false
}
// /r/vim - Description description (1 subscriber, 2 actives)
c.Replyf(source, "%s %s - %s (%s %s, %s %s)",
redditPrefix,
rs.Data.URL,
rs.Data.Description,
internal.PrettifySuffix(rs.Data.Subscribers),
internal.PluralizeWord(rs.Data.Subscribers, "subscriber"),
internal.PrettifySuffix(rs.Data.Actives),
internal.PluralizeWord(rs.Data.Actives, "active"))
return true
}