-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape.js
259 lines (234 loc) · 8.3 KB
/
scrape.js
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
const fs = require('fs')
const process = require('process')
const puppeteer = require('puppeteer')
const { ArgumentParser } = require('argparse')
class scrapePDF {
// Open browser
// Go to given url
// Login to Kindle Cloud
async openPage() {
// Start browser
this.browser = await puppeteer.launch(this.chromeOptions);
this.page = await this.browser.newPage();
await this.page.goto(this.url, { waitUntil: 'networkidle0' });
await this.page.waitForSelector(this.sel.signInButton);
// Login to Kindle
await this.page.focus(this.sel.emailField)
await this.page.keyboard.type(this.email)
await this.page.focus(this.sel.passwordField)
await this.page.keyboard.type(this.password)
await this.page.click(this.sel.signInButton)
await this.page.waitForNavigation({ waitUntil: 'networkidle0' })
// Switch to content iframe
await this.switchToFrame()
await this.content.waitForSelector(this.sel.loadingWheel, { hidden: true })
}
async switchToFrame() {
this.frame = await this.page.$(this.sel.iframe)
this.content = await this.frame.contentFrame()
}
// Click on page settings button
// Change font size
// Change page margin
// Change page color
// Set one column only
// Apply settings
async changePageLayout() {
await this.content.$eval(this.sel.pageSettingsButton, el => el.click())
await this.content.click(this.sel.fontSize[this.fontSize])
await this.content.click(this.sel.pageMargin[this.pageMargin])
await this.content.click(this.sel.pageColor[this.pageColor])
await this.content.click(this.sel.oneColumnOnly)
await this.content.click(this.sel.applySettingsButton)
await this.page.waitForTimeout(1000)
await this.content.waitForSelector(this.sel.loadingWheel, { hidden: true })
}
// Make right-arrow button visible
// Click on right-arrow button
// Make right-arrow button invisible
async goToNextPage() {
var nextPage = await this.content.$(this.sel.nextPage)
if (nextPage) {
nextPage.evaluate((el) => el.style.display = 'block')
await nextPage.click()
nextPage.evaluate((el) => el.style.display = 'none')
await this.page.waitForTimeout(1000)
await this.switchToFrame()
await this.content.waitForSelector(this.sel.loadingWheel, { hidden: true })
} else {
console.log('right-arrow button not found')
}
}
// Save given screen to pdf with given fileName
async saveToPdf(fileName) {
try {
this.printOptions.path = this.savePath+fileName
const pdf = await this.page.pdf(this.printOptions);
} catch(err) {
console.log("Failed to convert page to PDF ("+err.name+" - "+err.message+")")
console.log(err)
}
}
// Print progress to stdout during scraping
printProgress(page) {
process.stdout.clearLine()
process.stdout.cursorTo(0)
process.stdout.write('Scraping PDF pages from Kindle: '+page+' from '+this.noPages)
}
async run() {
await this.openPage()
await this.changePageLayout()
for (let i=0; i<this.noPages; i++) {
this.printProgress(i)
await this.saveToPdf(i+'.pdf')
await this.goToNextPage()
}
process.stdout.write('\n')
await this.browser.close()
}
constructor(url,
email,
password,
noPages,
savePath,
fontSize=1,
pageMargin=2,
pageColor='white') {
this.url = url
this.email = email
this.password = password
this.noPages = noPages;
this.savePath = savePath;
if (!fs.existsSync(savePath)) {
fs.mkdirSync(savePath);
}
if (![0,1,2,3,4].includes(fontSize)) {
throw "fontSize argument must be one of following [0,1,2,3,4]"
}
this.fontSize = fontSize
if (![0,1,2,3,4].includes(pageMargin)) {
throw "pageMargin argument must be one of following [0,1,2,3,4]"
}
this.pageMargin = pageMargin
if (!["white","sepia","black"].includes(pageColor)) {
throw "pageColor argument must be one of following ['white','sepia','black']"
}
this.pageColor = pageColor
// DOM selectors
this.sel = {
iframe: '#KindleReaderIFrame',
loadingWheel: '#loading_spinner',
nextPage: '#kindleReader_pageTurnAreaRight',
emailField: '#ap_email',
passwordField: '#ap_password',
signInButton: '#signInSubmit',
pageSettingsButton: '#kindleReader_button_controlPanel',
fontSize: {
0: '#kindleReader_controlPanel_xSmallFont',
1: '#kindleReader_controlPanel_smallFont',
2: '#kindleReader_controlPanel_mediumFont',
3: '#kindleReader_controlPanel_largeFont',
4: '#kindleReader_controlPanel_xLargeFont'
},
pageMargin: {
0: '#kindleReader_controlPanel_xSmallMargin',
1: '#kindleReader_controlPanel_smallMargin',
2: '#kindleReader_controlPanel_mediumMargin',
3: '#kindleReader_controlPanel_largeMargin',
4: '#kindleReader_controlPanel_xLargeMargin'
},
pageColor: {
'white': '#kindleReader_controlPanel_colorWhite',
'sepia': '#kindleReader_controlPanel_colorSepia',
'black': '#kindleReader_controlPanel_colorBlack'
},
oneColumnOnly: '#kindleReader_controlPanel_columnButton_on',
applySettingsButton: 'body > div.ui-dialog.ui-widget.ui-widget-content.ui-corner-all.settings_dialog > div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix > div > button:nth-child(2)'
}
// options for puppeteer.launch method
this.chromeOptions = {
headless: true,
defaultViewport: {
width: 960,
height: 900
},
slowMo: 0,
args: []
}
// options for page.pdf method
this.printOptions = {
format: 'A4',
path: null,
scale: 1.0,
displayHeaderFooter: false,
printBackground: true,
landscape: false,
margin: {
top: 0.0,
bottom: 0.0,
left: 0.0,
right: 0.0
},
preferCSSPageSize: false
}
this.run()
}
}
const parser = new ArgumentParser({
description: 'Scrape single PDF pages of given book from Kindle Cloud Reader'
})
parser.add_argument('-url', {
type: 'str',
required: true,
help: 'URL of given book in Kindle Cloud Reader (required)'
})
parser.add_argument('-email', {
type: 'str',
required: true,
help: 'Amazon login email (required)'
})
parser.add_argument('-pwd', {
type: 'str',
required: true,
metavar: 'PASSWORD',
help: 'Amazon login password (required)'
})
parser.add_argument('-pages', {
type: 'int',
required: true,
metavar: 'NO. PAGES',
help: 'Number of pages (from beginning) to scrape (required)'
})
parser.add_argument('-path', {
type: 'str',
required: true,
metavar: 'SAVE PATH',
help: 'Absolute/relative path to save PDF files (required)'
})
parser.add_argument('-fs', '--fontSize', {
type: 'int',
nargs: '?',
default: 2,
help: 'Font size of text 0/1/2/3/4 (default 2)'
})
parser.add_argument('-pm', '--pageMargin', {
type: 'int',
nargs: '?',
default: 1,
help: 'PDF page margins 0/1/2/3/4 (default 1)'
})
parser.add_argument('-pc', '--pageColor', {
type: 'str',
nargs: '?',
default: 'white',
help: 'PDF page color white/sepia/black (default white)'
})
const args = parser.parse_args()
new scrapePDF(url=args.url,
email=args.email,
password=args.pwd,
noPages=args.pages,
savePath=args.path,
fontSize=args.fontSize,
pageMargin=args.pageMargin,
pageColor=args.pageColor)