-
Notifications
You must be signed in to change notification settings - Fork 1
/
post.go
75 lines (61 loc) · 1.8 KB
/
post.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
package ginside
import (
"context"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
)
type PostDetails struct {
ID string
Title string
Author string
Date time.Time
URL string
ContentHTML string
Attachments []PostAttachment
}
type PostAttachment struct {
URL string
Filename string
}
func (g *GInside) PostDetails(ctx context.Context, link string) (*PostDetails, error) {
resp, err := makeRequest(ctx, g.httpClient, link)
if err != nil {
return nil, err
}
defer resp.Body.Close() // nolint: errcheck
// parse html
mainDoc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, err
}
mainDocHTML, err := mainDoc.Html()
if err != nil {
return nil, err
}
// follow JavaScript forwarding if necessary
parts := forwardRegex.FindStringSubmatch(mainDocHTML)
if len(parts) >= 2 {
return g.PostDetails(ctx, parts[1])
}
var details PostDetails
details.ID, _ = mainDoc.Find("input[name=gallery_no]").First().Attr("value")
details.Title = mainDoc.Find(".title_subject").First().Text()
details.Author, _ = mainDoc.Find(".gall_writer").First().Attr("data-nick")
details.Date, _ = parseDate(mainDoc.Find(".gall_date").First().Text())
details.URL = link
postHTML, _ := mainDoc.Find(".writing_view_box").First().Html()
postHTML = strings.Replace(strings.Replace(postHTML, "\n", "", -1), "\t", "", -1)
details.ContentHTML = postHTML
attachments := mainDoc.Find("ul.appending_file li")
var attachment PostAttachment
attachments.Each(func(_ int, selection *goquery.Selection) {
attachment = PostAttachment{}
attachment.URL, _ = selection.Find("a").Attr("href")
attachment.Filename = selection.Find("a").Text()
if attachment.URL != "" {
details.Attachments = append(details.Attachments, attachment)
}
})
return &details, nil
}