-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
172 lines (161 loc) · 4.72 KB
/
index.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
var fs = require('fs')
var request = require('request')
var rp = require('request-promise')
var Promise = require('bluebird')
var path = require('path')
var exec = require('child_process').execSync
var FileCookieStore = require('file-cookie-store');
var https = require('https')
var urls = {
readerHome: 'https://reader.readmoo.com/reader/index.html',
readerHost: 'https://reader.readmoo.com',
loginPage: 'https://member.readmoo.com/login',
library: 'https://new-read.readmoo.com/api/me/library/books?count=100&sort=buy'
}
var cookieFile = 'cookies.txt'
var headers = {
'Referer': urls.readerHome,
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:10.0) Gecko/20100101 Firefox/10.0',
'Accept-Language': 'zh-TW,zh;q=0.5',
'Origin': urls.readerHost
}
var Rmoo = function (params) {
_this = this;
['username', 'password'].forEach(function (key) {
if(typeof(params[key]) == 'undefined') throw 'Too few arguments'
_this[key] = params[key]
})
_this.httpAgent = new https.Agent()
_this.httpAgent.maxSockets = params.maxSockets || 5
if (!fs.existsSync(cookieFile)) fs.writeFileSync(cookieFile, '')
_this.jar = new FileCookieStore(cookieFile, {lockfile : true})
}
Rmoo.prototype.init = function () {
var _this = this
return new Promise(function (resolve, reject) {
_this.getLibrary().then(function () {
resolve()
}).catch(function (err) {
_this.login().then(function () {
resolve()
}).catch(function (err) {
reject(err)
})
})
})
}
Rmoo.prototype.login = function () {
var _this = this
return rp({
uri: urls.loginPage,
headers: headers,
jar: _this.jar
}).then(function () {
return rp({
method: 'POST',
uri: urls.loginPage,
form: {
email: _this.username,
password: _this.password
},
json: true,
jar: _this.jar,
headers: {
'Referer': urls.loginPage,
'User-Agent': headers['User-Agent'],
'X-Requested-With': 'XMLHttpRequest'
}
})
}).then(function (response) {
if (response['status'] !== 'ok') throw ''
console.log('Login success')
}).error(function (err) {
throw 'Login failed'
})
}
Rmoo.prototype.getLibrary = function () {
var _this = this
return rp({
uri: urls.library,
jar: _this.jar,
json: true
}).then(function (response) {
if (typeof(response['data']) == 'undefined' || response['status'] == 'error_login') throw 'Not logged in'
return response
}).catch(function (err) {
throw err
})
}
Rmoo.prototype.getBookMeta = function (id) {
var _this = this
return rp({
headers: headers,
uri: urls.readerHost + '/api/book/' + id + '/nav',
jar: _this.jar,
json: true
}).then(function (response) {
if (typeof(response['message']) == 'undefined' || response['message'] !== 'success') throw 'Invalid book ID'
return response
}).catch(function (err) {
throw err
})
}
Rmoo.prototype.getBookEpub = function (id, verbose) {
var _this = this
var opf = ''
var base = ''
var navDir = ''
return this.getBookMeta(id).then(function (meta) {
console.log('Downloading', id)
base = meta.base
navDir = meta.nav_dir.replace(meta.base, '')
opf = meta.opf
exec('rm -rf ' + id)
return new Promise.all([
'mimetype',
'META-INF/container.xml',
'META-INF/encryption.xml',
'META-INF/manifest.xml',
'META-INF/metadata.xml',
'META-INF/rights.xml',
'META-INF/signatures.xml',
navDir + opf
].map(function (fn) {
return _this.downloadFile(base, id, fn, verbose)
}))
}).then(function () {
var rootFile = fs.readFileSync(id + '/' + navDir + opf, 'utf-8')
var p = []
rootFile.split('href="').forEach(function (element) {
var fn = element.split('"')
if (fn.length == 1) return;
p.push(_this.downloadFile(base, id, navDir + fn[0], verbose))
})
return new Promise.all(p)
}).then(function () {
exec('zip -r ../' + id + '.epub *', {cwd: id})
exec('rm -rf ' + id)
console.log('Done', id)
})
}
Rmoo.prototype.downloadFile = function (base, wd, fn, verbose, tries) {
var _this = this
return rp({
headers: headers,
uri: urls.readerHost + base + fn,
jar: _this.jar,
encoding: 'binary',
pool: _this.httpAgent
}).then(function (response) {
if (verbose) console.log('Downloaded', fn)
exec('mkdir -p ' + path.dirname(wd + '/' + fn))
return fs.writeFileSync(wd + '/' + fn, response, 'binary')
}).catch(function (e) {
if (typeof(e.response) == 'undefined' || !e.response.complete) {
var toTry = typeof(tries) == 'number' ? tries++ : 1
console.log('Error', fn, ', retrying for', toTry)
return _this.downloadFile(base, wd, fn, verbose, toTry)
}
})
}
module.exports = Rmoo