forked from geeksbaek/goinside
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
393 lines (367 loc) · 11.1 KB
/
reader.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package goinside
import (
"errors"
"fmt"
"io/ioutil"
"regexp"
"strings"
)
var (
filenameRe = regexp.MustCompile(`image/(.*)`)
)
func fetchSomething(formMap map[string]string, api dcinsideAPI, data interface{}) (err error) {
resp, err := api.get(formMap)
if err != nil {
return
}
valid := make(jsonValidation, 1)
if err = responseUnmarshal(resp, data, &valid); err != nil {
return
}
if err = checkJSONResult(&valid); err != nil {
return
}
return
}
type jsonGallery []struct {
Category string `json:"category"`
ID string `json:"name"`
Name string `json:"ko_name"`
Number string `json:"no"`
Depth string `json:"depth"`
CanWrite bool `json:"no_write"`
IsAdultOnly bool `json:"is_adult"`
}
// FetchAllMajorGallery 함수는 모든 일반 갤러리의 정보를 가져옵니다.
func FetchAllMajorGallery() (mg []*MajorGallery, err error) {
resp, err := majorGalleryListAPI.getWithoutHash()
if err != nil {
return
}
jsonResp := jsonGallery{}
if err = responseUnmarshal(resp, &jsonResp); err != nil {
return
}
mg = make([]*MajorGallery, len(jsonResp))
for i, v := range jsonResp {
mg[i] = &MajorGallery{
ID: v.ID,
Name: v.Name,
Number: v.Number,
CanWrite: !v.CanWrite,
}
}
return
}
type jsonMonirGallery []struct {
Category string `json:"category"`
ID string `json:"name"`
Name string `json:"ko_name"`
Number string `json:"no"`
Depth string `json:"depth"`
CanWrite bool `json:"no_write"`
IsAdultOnly bool `json:"is_adult"`
Manager string `json:"manager"`
SubManagers string `json:"submanager"`
}
// FetchAllMinorGallery 함수는 모든 마이너 갤러리의 정보를 가져옵니다.
func FetchAllMinorGallery() (mg []*MinorGallery, err error) {
resp, err := minorGalleryListAPI.getWithoutHash()
if err != nil {
return
}
jsonResp := jsonMonirGallery{}
if err = responseUnmarshal(resp, &jsonResp); err != nil {
return
}
mg = make([]*MinorGallery, len(jsonResp))
for i, v := range jsonResp {
mg[i] = &MinorGallery{
ID: v.ID,
Name: v.Name,
Number: v.Number,
CanWrite: !v.CanWrite,
Manager: v.Manager,
SubManagers: strings.Split(v.SubManagers, ","),
}
}
return
}
type jsonList []struct {
GallInfo []struct {
CategoryName string `json:"category_name"`
FileCount string `json:"file_cnt"`
FileSize string `json:"file_size"`
} `json:"gall_info"`
GallList []struct {
Subject string `json:"subject"`
Name string `json:"name"`
Level string `json:"level"`
ImageIcon string `json:"img_icon"`
WinnertaIcon string `json:"winnerta_icon"`
ThumbsUp string `json:"recommend"`
ThumbsUpIcon string `json:"recommend_icon"`
IsBest string `json:"best_chk"`
Hit string `json:"hit"`
UserID string `json:"user_id"`
MemberIcon string `json:"member_icon"`
IP string `json:"ip"`
TotalComment string `json:"total_comment"`
TotalVoice string `json:"total_voice"`
Number string `json:"no"`
Date string `json:"date_time"`
} `json:"gall_list"`
}
// FetchList 함수는 해당 갤러리의 해당 페이지에 있는 글의 목록을 가져옵니다.
func FetchList(gallID string, page int) (l *List, err error) {
return fetchList(gallID, page, false)
}
// FetchBestList 함수는 해당 갤러리의 해당 페이지에 있는 개념글의 목록을 가져옵니다.
func FetchBestList(gallID string, page int) (l *List, err error) {
return fetchList(gallID, page, true)
}
func fetchList(gallID string, page int, fetchBestPage bool) (l *List, err error) {
URL := gallURL(gallID)
gall := &Gall{ID: gallID, URL: URL}
formMap := map[string]string{
"app_id": dummyGuest.getAppID(),
"id": gallID,
"page": fmt.Sprint(page),
}
if fetchBestPage {
formMap["recommend"] = "1"
}
respJSON := make(jsonList, 1)
if err = fetchSomething(formMap, readListAPI, &respJSON); err != nil {
return
}
r := respJSON[0]
l = &List{
Info: &ListInfo{
CategoryName: r.GallInfo[0].CategoryName,
FileCount: r.GallInfo[0].FileCount,
FileSize: r.GallInfo[0].FileSize,
Gall: gall,
},
Items: []*ListItem{},
}
for _, a := range r.GallList {
item := &ListItem{
Gall: gall,
URL: articleURL(gallID, a.Number),
Subject: a.Subject,
Name: a.Name,
Level: Level(a.Level),
HasImage: a.ImageIcon == "Y",
ArticleType: articleType(a.ImageIcon, a.IsBest),
ThumbsUp: mustAtoi(a.ThumbsUp),
IsBest: a.IsBest == "Y",
Hit: mustAtoi(a.Hit),
GallogID: a.UserID,
GallogURL: gallogURL(a.UserID),
IP: a.IP,
CommentLength: mustAtoi(a.TotalComment),
VoiceCommentLength: mustAtoi(a.TotalVoice),
Number: a.Number,
Date: dateFormatter(a.Date),
}
l.Items = append(l.Items, item)
}
return
}
// Fetch 메소드는 해당 글의 세부 정보(본문, 이미지 주소, 댓글)를 가져옵니다.
func (i *ListItem) Fetch() (*Article, error) {
return FetchArticle(i.URL)
}
// FetchImageURLs 메소드는 해당 글의 이미지 주소의 슬라이스만을 가져옵니다.
func (i *ListItem) FetchImageURLs() (imageURLs []ImageURLType, err error) {
formMap := map[string]string{
"app_id": dummyGuest.getAppID(),
"id": i.Gall.ID,
"no": fmt.Sprint(i.Number),
}
images := make(jsonArticleImages, 1)
err = fetchSomething(formMap, readArticleImageAPI, &images)
if err != nil {
return
}
imageURLs = func() (ret []ImageURLType) {
for _, v := range images {
ret = append(ret, ImageURLType(v.Image))
}
return
}()
return
}
// Fetch 메소드는 해당 이미지 주소를 참조하여 이미지의 []byte와 filename을 반환합니다.
func (i ImageURLType) Fetch() (data []byte, filename string, err error) {
resp, err := doImage(i)
if err != nil {
return
}
defer resp.Body.Close()
contentType := resp.Header.Get("Content-Type")
matched := filenameRe.FindStringSubmatch(contentType)
if len(matched) != 2 {
err = errors.New("cannot found filename from Content-Type")
return
}
filename = strings.ToLower(matched[1])
data, err = ioutil.ReadAll(resp.Body)
return
}
type jsonArticle []struct {
ViewInfo struct {
GallTitle string `json:"galltitle"`
Category string `json:"category"`
Subject string `json:"subject"`
Number string `json:"no"`
Name string `json:"name"`
Level string `json:"level"`
MemberIcon string `json:"member_icon"`
TotalComment string `json:"total_comment"`
IP string `json:"ip"`
HasImage string `json:"img_chk"`
IsBest string `json:"recommend_chk"`
IsWinnerta string `json:"winnerta_chk"`
HasVoice string `json:"voice_chk"`
Hit string `json:"hit"`
WriteType string `json:"write_type"`
UserID string `json:"user_id"`
PrevArticleNumber string `json:"prev_link"`
PrevArticleSubject string `json:"prev_subject"`
HeadTitle string `json:"headtitle"`
NextArticleNumber string `json:"next_link"`
NextArticleSubject string `json:"next_subject"`
BestCheck string `json:"best_chk"` // ?
IsNotice string `json:"isNotice"`
Date string `json:"date_time"`
} `json:"view_info"`
ViewMain struct {
Memo string `json:"memo"`
ThumbsUp string `json:"recommend"`
ThumbsUpMember string `json:"recommend_member"`
ThumbsDown string `json:"nonrecommend"`
} `json:"view_main"`
}
type jsonArticleImages []struct {
Image string `json:"img"`
// ImageClone string `json:"img_clone"`
}
// FetchArticle 함수는 해당 글의 정보를 가져옵니다.
func FetchArticle(URL string) (a *Article, err error) {
gallID := gallID(URL)
gall := &Gall{ID: gallID, URL: gallURL(gallID)}
formMap := map[string]string{
"app_id": dummyGuest.getAppID(),
"id": gallID,
"no": articleNumber(URL),
}
view := make(jsonArticle, 1)
images := make(jsonArticleImages, 1)
ch := func() <-chan error {
ch := make(chan error)
go func() {
ch <- fetchSomething(formMap, readArticleAPI, &view)
}()
go func() {
fetchSomething(formMap, readArticleImageAPI, &images)
ch <- nil
}()
return ch
}()
for i := 0; i < 2; i++ {
if err := <-ch; err != nil {
return nil, err
}
}
v := view[0]
article := &Article{
Gall: gall,
URL: articleURL(gallID, v.ViewInfo.Number),
Subject: v.ViewInfo.Subject,
Content: v.ViewMain.Memo,
ThumbsUp: mustAtoi(v.ViewMain.ThumbsUp) + mustAtoi(v.ViewMain.ThumbsUpMember),
ThumbsDown: mustAtoi(v.ViewMain.ThumbsDown),
Name: v.ViewInfo.Name,
Number: v.ViewInfo.Number,
Level: MemberType(mustAtoi(v.ViewInfo.MemberIcon)).Level(),
IP: v.ViewInfo.IP,
CommentLength: mustAtoi(v.ViewInfo.TotalComment),
HasImage: v.ViewInfo.HasImage == "Y",
Hit: mustAtoi(v.ViewInfo.Hit),
ArticleType: articleType(v.ViewInfo.HasImage, v.ViewInfo.IsBest),
GallogID: v.ViewInfo.UserID,
GallogURL: gallogURL(v.ViewInfo.UserID),
IsBest: v.ViewInfo.IsBest == "Y",
ImageURLs: func() (ret []ImageURLType) {
for _, v := range images {
ret = append(ret, ImageURLType(v.Image))
}
return
}(),
Comments: []*Comment{},
Date: dateFormatter(v.ViewInfo.Date),
}
if article.CommentLength > 0 {
article.Comments, err = fetchComment(URL, article)
if err != nil {
return
}
}
if article.Subject == "" {
return nil, errors.New("본문을 가져올 수 없음")
}
return article, nil
}
type jsonComment []struct {
CommentCount string `json:"total_comment"`
TotalPage string `json:"total_page"`
NowPage string `json:"re_page"`
Comments []struct {
MemberIcon string `json:"member_icon"`
IP string `json:"ipData"`
Name string `json:"name"`
UserID string `json:"user_id"`
Content string `json:"comment_memo"`
Number string `json:"comment_no"`
Date string `json:"date_time"`
} `json:"comment_list"`
}
func fetchComment(URL string, parents *Article) (cs []*Comment, err error) {
gallID := gallID(URL)
gallURL := gallURL(gallID)
gall := &Gall{ID: gallID, URL: gallURL}
cs = []*Comment{}
for commentPage := 1; ; commentPage++ {
formMap := map[string]string{
"app_id": dummyGuest.getAppID(),
"id": gallID,
"no": parents.Number,
"re_page": fmt.Sprint(commentPage),
}
respJSON := make(jsonComment, 1)
if err = fetchSomething(formMap, readCommentAPI, &respJSON); err != nil {
return
}
r := respJSON[0]
for _, c := range r.Comments {
comment := &Comment{
Gall: gall,
Parents: parents,
Name: c.Name,
GallogID: c.UserID,
GallogURL: gallogURL(c.UserID),
IP: c.IP,
Number: c.Number,
Date: dateFormatter(c.Date),
}
comment.Content, comment.HTML = c.Content, c.Content
cs = append(cs, comment)
}
if mustAtoi(r.NowPage) >= mustAtoi(r.TotalPage) {
break
}
}
return
}