-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4chan.go
243 lines (211 loc) · 4.62 KB
/
4chan.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//
package main
import (
"encoding/json"
"errors"
"fmt"
"html"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
)
type Post struct {
Board string // must be inherited from parent Thread/Catalog
Subject string `json:"sub"` // often empty in Thread
Comment string `json:"com"` // raw html
Filename string // original name at upload time
Ext string // starts with "."
Time int `json:"tim"`
Num int `json:"no"`
// LastModified int `json:"last_modified"` // may be 0
}
// Render comment as HTML, then add quoted post(s) with indentation.
func (p Post) QuoteComment(t *Thread) string {
// comm := renderHTML(p.Comment)
var lines []string
for _, line := range renderHTML(p.Comment) {
lines = append(lines, line)
if strings.HasPrefix(line, ">>") {
quote := strings.Fields(line)[0]
quote = strings.TrimPrefix(quote, ">>")
id, err := strconv.Atoi(quote)
if err != nil {
panic(err)
}
parent, err := t.getIndex(id)
if err != nil {
continue
}
lines = append(lines, indent(t.Posts[parent].htmlComment())...)
}
}
return strings.Join(lines, "\n")
}
func (p Post) htmlComment() []string {
return renderHTML(p.Comment)
}
func (p Post) lineComment() (c string) {
if p.Comment == "" {
comment, _ := p.imageUrl()
return "[" + comment + "]"
}
c = html.UnescapeString(p.Comment)
c = stripHtmlTags(c)
c = strings.ReplaceAll(c, "\n", " ")
return c
}
func (p Post) imageUrl() (url string, err error) {
if p.Time == 0 {
return "", errors.New("no image")
}
if p.Board == "" {
panic("empty board")
}
url = fmt.Sprintf("https://i.4cdn.org/%s/%d%s", p.Board, p.Time, p.Ext)
return url, nil
}
// Returns path to temp image file
func (p Post) imagePath() (fname string, err error) {
url, err := p.imageUrl()
if err != nil {
return "", err
}
path := filepath.Join(tmpDir, filepath.Base(url))
return path, nil
}
func (p Post) download() {
url, err := p.imageUrl()
if err != nil {
return
}
path, err := p.imagePath()
if err != nil {
return
}
if _, err := os.Stat(path); err == nil {
return
}
log.Println("downloading", url)
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
_ = os.Mkdir(tmpDir, os.ModePerm)
if err := os.WriteFile(path, b, 0666); err != nil {
panic(err)
}
}
type Catalog struct {
Board string
Posts []*Post // OPs
}
func getCatalog(board string) Catalog {
url := fmt.Sprintf("https://a.4cdn.org/%s/catalog.json", board)
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
var pages []struct {
Page int
Threads []*Post
}
if err := json.Unmarshal(b, &pages); err != nil {
panic(err)
}
// fmt.Println(pages)
var threads []*Post
for _, p := range pages {
threads = append(threads, p.Threads...)
}
// ensure all posts have Board field set (otherwise leads to erroneous
// image urls)
for _, p := range threads {
p.Board = board
}
return Catalog{Board: board, Posts: threads}
}
// Get thread by subject
func (c Catalog) findThread(subject string) *Thread {
var found *Post
for _, t := range c.Posts {
if strings.ToLower(t.Subject) == subject {
found = t
break
}
}
if found == nil {
return nil
}
// return c.getThread(found.Num)
return getThread(c.Board, found.Num)
}
// Note that Thread has the same structure as Catalog, but lacks access to the
// findThread method
type Thread struct {
Board string
Posts []*Post
// pointer because we need to mutate Post.Board
}
func (t *Thread) getIndex(id int) (int, error) {
for i, p := range t.Posts {
if p.Num == id {
return i, nil
}
}
return 0, errors.New("post not found")
}
func (t *Thread) filterPosts(s string) (matches []int) {
for idx, p := range t.Posts {
if strings.Contains(strings.ToLower(p.Comment+p.Subject), s) {
matches = append(matches, idx)
}
}
return matches
}
func (t *Thread) cleanImages() {
for _, p := range t.Posts {
path, err := p.imagePath()
if err != nil {
continue
}
_ = os.Remove(path)
}
}
// Get thread by id
func getThread(board string, id int) *Thread {
url := fmt.Sprintf("https://a.4cdn.org/%s/thread/%d.json", board, id)
// log.Println("getting", url)
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
var t Thread
if err := json.Unmarshal(b, &t); err != nil {
panic(err)
}
t.Board = board
// log.Printf(`getThread("%s", %d)`, board, id)
for _, p := range t.Posts {
p.Board = t.Board
}
return &t
}