-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
201 lines (170 loc) · 4.17 KB
/
main.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
/*
14-09-2024 by github.com/i4k1
simple utility for parsing files by keywords on the 2ch.hk imageboard
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strconv"
"strings"
)
var (
sosuchUrl = "https://2ch.hk/" // alternative: https://2ch.life/
boards = flag.String("boards", "b", "Which board to parse")
keywords = flag.String("keywords", "", "Use keywords for parsing")
fileformats = flag.String("fileformats", "", "What file formats to download")
path = flag.String("path", "src", "In which directory to save files")
)
// for catalog parsing
type OPs struct {
Comment string `json:"comment"`
Date string `json:"date"`
Num int `json:"num"`
}
type Catalog struct {
Threads []OPs `json:"threads"`
}
// for thread parsing
type File struct {
Name string `json:"name"`
Path string `json:"path"`
}
type Post struct {
Comment string `json:"comment"`
Date string `json:"date"`
Files []File `json:"files"`
Num int64 `json:"num"`
}
type Thread struct {
Posts []Post `json:"posts"`
}
type PostsData struct {
Threads []Thread `json:"threads"`
}
func downloadFile(url string, filename string) error {
// getting response from server
response, err := http.Get(url)
if err != nil {
panic(err)
}
defer response.Body.Close()
// сhecking response status and error handling
if response.StatusCode != http.StatusOK {
panic(response.Status)
}
// creating file for writing
out, err := os.Create(filename)
if err != nil {
panic(err)
}
defer out.Close()
// copy response content to file
_, err = io.Copy(out, response.Body)
if err != nil {
panic(err)
}
fmt.Println("downloaded:", filename)
return nil
}
func parseThread(threadNumber string, board string, dirToSave string, formats string) {
fformats := strings.Split(formats, ",")
threadUrl := sosuchUrl + board + "/res/" + threadNumber + ".json" // full link to thread's json
// GET-request
response, err := http.Get(threadUrl)
if err != nil {
panic(err)
}
defer response.Body.Close()
// status code of response and error handling
if response.StatusCode != http.StatusOK {
panic(response.StatusCode)
}
// reading body response
body, err := io.ReadAll(response.Body)
if err != nil {
panic(err)
}
// catalog.json decode
var postsData PostsData
err = json.Unmarshal(body, &postsData)
if err != nil {
panic(err)
}
// if the directory does not exist, create it
if _, err := os.Stat(dirToSave); os.IsNotExist(err) {
err := os.Mkdir(dirToSave, 0755) // 0755 - access rights
if err != nil {
panic(err)
}
fmt.Println("directory created:", dirToSave)
}
for _, thread := range postsData.Threads {
for _, post := range thread.Posts {
for _, file := range post.Files {
fileUrl := sosuchUrl + file.Path
wichDir := dirToSave + "/" + file.Name
for _, ext := range fformats {
if strings.HasSuffix(file.Name, ext) {
fmt.Println("post:", post.Num)
err := downloadFile(fileUrl, wichDir)
if err != nil {
panic(err)
}
}
}
}
}
}
}
func parseCatalog(board string, keywordPattern string) {
// link to catalog
sosuchCatalogUrl := sosuchUrl + board + "/catalog.json"
// GET-request
response, err := http.Get(sosuchCatalogUrl)
if err != nil {
panic(err)
}
defer response.Body.Close()
// status code of response and error handling
if response.StatusCode != http.StatusOK {
panic(response.StatusCode)
}
// reading body response
body, err := io.ReadAll(response.Body)
if err != nil {
panic(err)
}
// catalog.json decode
var catalog Catalog
err = json.Unmarshal(body, &catalog)
if err != nil {
panic(err)
}
var keywordRegex *regexp.Regexp
if keywordPattern != "" {
var err error
keywordRegex, err = regexp.Compile(keywordPattern)
if err != nil {
panic(err)
}
}
for _, thread := range catalog.Threads {
if keywordRegex != nil && !keywordRegex.MatchString(thread.Comment) {
fmt.Println("skiped:", thread.Num)
continue
}
fmt.Println("\nparsing:", thread.Num)
threadNumber := strconv.Itoa(thread.Num)
parseThread(threadNumber, *boards, *path, *fileformats)
}
}
func main() {
flag.Parse()
parseCatalog(*boards, *keywords)
}