/
pdf.go
188 lines (171 loc) · 4.81 KB
/
pdf.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
package pdf
import (
"context"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/cdproto/runtime"
"github.com/chromedp/chromedp"
"github.com/chromedp/chromedp/device"
"github.com/nicoxiang/geektime-downloader/internal/geektime"
"github.com/nicoxiang/geektime-downloader/internal/pkg/filenamify"
)
// PDFExtension ...
const PDFExtension = ".pdf"
// PrintArticlePageToPDF use chromedp to print article page and save
func PrintArticlePageToPDF(ctx context.Context,
aid int,
dir,
title string,
cookies []*http.Cookie,
downloadComments bool,
waitSeconds int,
) error {
rateLimit := false
// new tab
ctx, cancel := chromedp.NewContext(ctx)
defer cancel()
ctx, cancel = context.WithTimeout(ctx, time.Minute)
defer cancel()
chromedp.ListenTarget(ctx, func(ev interface{}) {
switch responseReceivedEvent := ev.(type) {
case *network.EventResponseReceived:
response := responseReceivedEvent.Response
if response.URL == geektime.DefaultBaseURL+"/serv/v1/article" && response.Status == 451 {
rateLimit = true
cancel()
}
}
})
var buf []byte
err := chromedp.Run(ctx,
chromedp.Tasks{
chromedp.Emulate(device.IPadPro11),
setCookies(cookies),
chromedp.Navigate(geektime.DefaultBaseURL + `/column/article/` + strconv.Itoa(aid)),
chromedp.Sleep(time.Duration(waitSeconds) * time.Second),
hideRedundantElements(downloadComments),
printToPDF(&buf),
},
)
if err != nil {
if rateLimit {
return geektime.ErrGeekTimeRateLimit
}
return err
}
fileName := filepath.Join(dir, filenamify.Filenamify(title)+PDFExtension)
if err := os.WriteFile(fileName, buf, 0666); err != nil {
return err
}
return nil
}
func setCookies(cookies []*http.Cookie) chromedp.ActionFunc {
return chromedp.ActionFunc(func(ctx context.Context) error {
expr := cdp.TimeSinceEpoch(time.Now().Add(180 * 24 * time.Hour))
for _, c := range cookies {
err := network.SetCookie(c.Name, c.Value).WithExpires(&expr).WithDomain(geektime.GeekBangCookieDomain).WithHTTPOnly(true).Do(ctx)
if err != nil {
return err
}
}
return nil
})
}
func hideRedundantElements(downloadComments bool) chromedp.ActionFunc {
return chromedp.ActionFunc(func(ctx context.Context) error {
s :=
`
var headMain = document.getElementsByClassName('main')[0];
if(headMain){
headMain.style.display="none";
}
var bottomWrapper = document.getElementsByClassName('sub-bottom-wrapper')[0];
if(bottomWrapper){
bottomWrapper.style.display="none";
}
var openAppdiv = document.getElementsByClassName('openApp')[0];
if(openAppdiv){
openAppdiv.parentNode.parentNode.parentNode.style.display="none";
}
var audioPlayer = document.querySelector('div[class^="ColumnArticleMiniAudioPlayer"]');
if(audioPlayer){
audioPlayer.style.display="none"
}
var audioFloatBar = document.querySelector('div[class*="audio-float-bar"]');
if(audioFloatBar){
audioFloatBar.style.display="none"
}
var leadsWrapper = document.querySelector('div[class^="leads-wrapper"]');
if(leadsWrapper){
leadsWrapper.style.display="none";
}
var unPreviewImage = document.querySelector('img[alt="unpreview"]');
if(unPreviewImage){
unPreviewImage.style.display="none"
}
var gotoColumn = document.querySelector('div[class^="Index_articleColumn"]');
if(gotoColumn){
gotoColumn.style.display="none"
}
var favBtn = document.querySelector('div[class*="Index_favBtn"]');
if(favBtn){
favBtn.style.display="none"
}
var likeModule = document.querySelector('div[class^="ArticleLikeModuleMobile"]');
if(likeModule){
likeModule.style.display="none"
}
var switchBtns = document.querySelector('div[class^="Index_switchBtns"]');
if(switchBtns){
switchBtns.style.display="none"
}
var writeComment = document.querySelector('div[class*="Index_writeComment"]');
if(writeComment){
writeComment.style.display="none"
}
var moreBtns = document.querySelectorAll('div[class^=CommentItem_more]');
for (let btn of moreBtns) {
btn.click();
}
`
hideCommentsExpression :=
`
var comments = document.querySelector('div[class^="Index_articleComments"]')
if(comments){
comments.style.display="none"
}
`
if !downloadComments {
s = s + hideCommentsExpression
}
_, exp, err := runtime.Evaluate(s).Do(ctx)
if err != nil {
return err
}
if exp != nil {
return exp
}
return nil
})
}
func printToPDF(res *[]byte) chromedp.ActionFunc {
return chromedp.ActionFunc(func(ctx context.Context) error {
data, _, err := page.PrintToPDF().
WithMarginTop(0.4).
WithMarginBottom(0.4).
WithMarginLeft(0.4).
WithMarginRight(0.4).
Do(ctx)
if err != nil {
return err
}
*res = data
return nil
})
}