This repository was archived by the owner on Jun 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.js
More file actions
211 lines (204 loc) · 6.6 KB
/
reader.js
File metadata and controls
211 lines (204 loc) · 6.6 KB
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
/**
* A dumb tool to grab Comic Earth Star
*/
const puppeteer = require('puppeteer')
const escapeRegExp = require('lodash.escaperegexp')
const { URL } = require('url')
const EventEmitter = require('events')
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter()
/**
* Use yargs to handle arguments
*/
const yargs = require('yargs') // eslint-disable-line
.command('start', 'start grabbing', (yargs) => {
yargs.option({
'url': {
alias: 'u',
describe: 'Page to grab'
},
'interval': {
alias: 'i',
default: 1,
describe: 'Interval seconds between shots, increase when suffering from bad network. Shall be larger than 1'
}
})
}, (argv) => {
if (argv.interval < 1) {
console.log('Interval shall be larger than 1')
process.exit()
}
if (argv.url) main(new URL(argv.url))
})
.demandOption(['url'], 'Please provide the URL to work with')
.argv
const interval = yargs.i
/**
* Main entry function
* @async
* @function main
* @param {string} url - The URL of the page
*/
async function main (url) {
/** Mocking mobile platform to get rid of page scroll animation */
const mobileUa = 'Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Mobile Safari/537.36'
/**
* Getting a random number between two values
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random | Random() on MDN}
* @param {number} min - min value for random number
* @param {number} max - max value for random number
*/
function getRandomArbitrary (min, max) {
return Math.random() * (max - min) + min
}
/** Init puppeteer */
const browser = await puppeteer.launch()
const page = await browser.newPage()
try {
let pageData = null
let totalPages = null
let dimension = null
let counter = 0
let readyPages = []
/** Set a large viewport first and mock user-agent */
await page.emulate({
viewport: {
width: 2160,
height: 3840,
isMobile: true,
hasTouch: true
},
userAgent: mobileUa
})
/** Handle 'loaddone' event */
myEmitter.on('loaddone', async () => {
console.log('invisible page loaded!')
/** Check if page data is ready */
if (pageData) {
/** Manga seems has 'cty=1' in URL */
if (url.searchParams.get('cty') === '1') {
totalPages = pageData.configuration.contents.length
}
/** Light novel seems has 'cty=0 in URL */
if (url.searchParams.get('cty') === '0') {
for (let key of pageData.configuration.contents) {
const filename = key.file
totalPages += pageData[filename].FileLinkInfo.PageCount
}
}
console.log(`Total Pages: ${totalPages}`)
/**
* While counter is smaller then total pages
* grab the current page until counter reaches
* total pages then close browser.
* Flawed here and there, not perfect.
*/
while (counter < totalPages) {
let retry = 0
while (readyPages.length > 0) {
console.log(`Cache size: ${readyPages.length}`)
await intervalScreenshot(interval * 1000, counter, dimension)
const shiftFileName = readyPages.shift()
counter++
console.log(`Done: ${counter}, name: ${shiftFileName}`)
}
while (readyPages.length === 0) {
if (counter === totalPages) {
await browser.close()
return
}
const waitInterval = interval * 1000
await page.waitFor(waitInterval)
++retry
console.log(`Retried ${retry} time(s)`)
if (retry === 60) {
console.log(`Retried ${retry} times, aborting...`)
await browser.close()
return
}
}
}
}
})
/** Handle 'requestfinished' event */
page.on('requestfinished', async req => {
console.log(req.url)
try {
/**
* When configuration_pack.json loaded get all the content
* which contains much useful infomation about page structure
*/
if (req.response().url.endsWith('configuration_pack.json')) {
if (!pageData) {
pageData = await req.response().json()
}
}
if (req.response().url.endsWith('invisible.png')) {
/**
* Loaddone event
*
* @event loaddone
*/
myEmitter.emit('loaddone')
}
if (pageData) {
/**
* Iterate through pageData.configuration.contents, set viewport according to dimension.
* Add counts to readPages
*/
for (let key of pageData.configuration.contents) {
const filename = key.file
/**
* File name are like
* '../shared/item/xhtml/p-001.xhtml/number.jpeg' or
* 'item/xhtml/p-001.xhtml/number.jpeg'
*/
const re = new RegExp(`${escapeRegExp(filename.replace(/\.\.\//i, ''))}/[0-9]*.jpeg`, 'i')
if (re.test(req.response().url)) {
dimension = pageData[filename].FileLinkInfo.PageLinkInfoList[0].Page.Size
dimension = {
height: dimension.Height,
width: dimension.Width,
isMobile: true,
hasTouch: true
}
await page.setViewport(dimension)
readyPages.push(re.exec(req.response().url))
}
}
}
} catch (error) {
console.log(error)
}
})
/** Navigate to the page, set wait method to idle, time out 10 seconds */
await page.goto(url.toString(), {
waitUntil: 'networkidle',
networkIdleTimeout: 10000
// timeout: 10000
})
} catch (error) {
console.log(error)
}
/**
* Take screenshots at interval
*
* @async
* @function intervalScreenshot
* @param {number} interval - interval between
* @param {string} filename - file name to save
* @param {object} dimension - file dimension
*/
async function intervalScreenshot (interval, filename, dimension) {
try {
/**
* Click mocking
*/
await page.waitFor(interval * getRandomArbitrary(1, 1.1))
await page.screenshot({ path: `output/${filename}.png` })
await page.touchscreen.tap(dimension.width * 0.1 + getRandomArbitrary(-5, 5), dimension.height * 0.7 + getRandomArbitrary(-5, 5))
} catch (error) {
console.log(error)
}
}
}