-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnchan.go
198 lines (174 loc) · 5.73 KB
/
nchan.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
package main
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/jaytaylor/html2text"
)
const (
yotsubaBlue = 0xEEF2FF
yotsubaRed = 0xFFFFEE
yotsubaGreen = 0x35B214
gifBanners = 253
pngBanners = 262
jpgBanners = 224
)
const (
apiRoot = "https://a.4cdn.org"
chanUsage = "4chan usage:\n!4chan BOARD\nWhere BOARD is one of the standard board names."
catalogRoot = "catalog.json"
bannerRoot = "http://s.4cdn.org/image/title"
countryRoot = "http://s.4cdn.org/image/country"
)
var bannedBoards = []string{"cm", "y", "gif", "e", "h", "hc", "b", "mlp", "lgbt", "soc", "s", "hm", "d", "t", "aco", "r", "pol", "trash"}
func BannedBoard(s string) bool {
for _, v := range bannedBoards {
if v == s {
return true
}
}
return false
}
type thread struct {
No int `json:"no"`
Sticky int `json:"sticky,omitempty"`
Closed int `json:"closed,omitempty"`
Now string `json:"now"`
Name string `json:"name"`
Sub string `json:"sub,omitempty"`
Com string `json:"com"`
Filename string `json:"filename"`
Ext string `json:"ext"`
W int `json:"w"`
H int `json:"h"`
TnW int `json:"tn_w"`
TnH int `json:"tn_h"`
Tim int64 `json:"tim"`
Time int `json:"time"`
Md5 string `json:"md5"`
Fsize int `json:"fsize"`
Resto int `json:"resto"`
ID string `json:"id"`
Country string `json:"country"`
SemanticURL string `json:"semantic_url"`
CountryName string `json:"country_name"`
Replies int `json:"replies"`
Images int `json:"images"`
LastModified int `json:"last_modified"`
Bumplimit int `json:"bumplimit,omitempty"`
Imagelimit int `json:"imagelimit,omitempty"`
OmittedPosts int `json:"omitted_posts,omitempty"`
OmittedImages int `json:"omitted_images,omitempty"`
LastReplies []struct {
No int `json:"no"`
Now string `json:"now"`
Name string `json:"name"`
Com string `json:"com"`
Time int `json:"time"`
Resto int `json:"resto"`
ID string `json:"id"`
Country string `json:"country"`
CountryName string `json:"country_name"`
Filename string `json:"filename,omitempty"`
Ext string `json:"ext,omitempty"`
W int `json:"w,omitempty"`
H int `json:"h,omitempty"`
TnW int `json:"tn_w,omitempty"`
TnH int `json:"tn_h,omitempty"`
Tim int64 `json:"tim,omitempty"`
Md5 string `json:"md5,omitempty"`
Fsize int `json:"fsize,omitempty"`
} `json:"last_replies,omitempty"`
MImg int `json:"m_img,omitempty"`
}
func fourchan(s *discordgo.Session, m *discordgo.MessageCreate, board string) {
type Catalog []struct {
Page int `json:"page"`
Threads []thread `json:"threads"`
}
//TODO(sjon): Implement this in a cleaner manner.
if board == "help" {
s.ChannelMessageSend(m.ChannelID, chanUsage)
return
}
boardCatalog := apiRoot + "/" + board + "/" + catalogRoot
resp, err := http.Get(boardCatalog)
if err != nil {
log.Fatal("Do: ", err)
return
}
if resp.StatusCode != http.StatusOK {
log.Println("Error: " + http.StatusText(resp.StatusCode))
return
}
defer resp.Body.Close()
var record Catalog
if err := json.NewDecoder(resp.Body).Decode(&record); err != nil {
log.Println(err)
}
//Extract random board page
page := record[rand.Intn(len(record)-1)]
//Extract random thread from the page
thread := page.Threads[rand.Intn(len(page.Threads)-1)]
//Try and filter out general threads, this method is awfully poor
for strings.Contains(strings.ToLower(thread.Sub), "general") || thread.Sticky == 1 && thread.Replies < 10 {
page = record[rand.Intn(len(record)-1)]
thread = page.Threads[rand.Intn(len(page.Threads)-1)]
}
//Clean up text from the selected thread
thread.Sub, err = html2text.FromString(strings.Replace(thread.Sub, "<wbr>", "", -1))
if err != nil {
log.Println(err)
return
}
thread.Com, err = html2text.FromString(strings.Replace(thread.Com, "<wbr>", "", -1))
if err != nil {
log.Println(err)
return
}
reply := formatThread(thread, board)
if _, err := s.ChannelMessageSendEmbed(m.ChannelID, reply); err != nil {
log.Println(err)
}
}
func formatThread(thread thread, board string) *discordgo.MessageEmbed {
img := fmt.Sprintf("https://i.4cdn.org/%s/%d%s", board, thread.Tim, thread.Ext)
//thumb := fmt.Sprintf("https://i.4cdn.org/%s/%ds%s", board, thread.Tim, ".jpg")
link := fmt.Sprintf("https://i.4cdn.org/%s/thread/%d", board, thread.No)
var banner string
switch rand.Intn(2) {
case 0:
banner = fmt.Sprintf(bannerRoot+"/%d%s", rand.Intn(jpgBanners), ".jpg")
case 1:
banner = fmt.Sprintf(bannerRoot+"/%d%s", rand.Intn(pngBanners), ".png")
case 2:
banner = fmt.Sprintf(bannerRoot+"/%d%s", rand.Intn(gifBanners), ".gif")
}
var replies string
if thread.Replies == 0 {
replies = "There are no replies ;_;"
} else if thread.Replies == 1 {
replies = "There is one reply."
} else {
replies = fmt.Sprintf("There are %d replies.", thread.Replies)
}
var title string
if thread.Sub == "" {
title = "link"
} else {
title = thread.Sub
}
embed := &discordgo.MessageEmbed{URL: link,
Title: title,
Color: yotsubaGreen,
Footer: &discordgo.MessageEmbedFooter{Text: replies},
Author: &discordgo.MessageEmbedAuthor{Name: thread.Name, IconURL: countryRoot + "/" + strings.ToLower(thread.Country) + ".gif"},
Thumbnail: &discordgo.MessageEmbedThumbnail{URL: banner},
Description: thread.Com,
Image: &discordgo.MessageEmbedImage{URL: img, Width: thread.W, Height: thread.H}}
return embed
}