This repository has been archived by the owner on Nov 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
252 lines (219 loc) · 6.65 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
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
import fs from 'fs';
import hawk from 'hawk';
import zip from 'minizip-asm.js';
import fetch from 'node-fetch';
import forge from 'node-forge';
import prompt from 'prompt';
const schema = {
properties: {
email: {
description: 'Email/Username:',
required: true,
},
password: {
description: 'Password:',
required: true,
hidden: true,
},
},
};
prompt.message = '';
prompt.delimiter = '';
prompt.start();
(async () => {
const { email, password } = await prompt.get(schema);
const clientId = await fetch('https://www.pearson.it/login/', {
redirect: 'manual',
})
.then((res) => {
return new URL(res.headers.get('location')).searchParams.get('client_id');
})
.catch((err) => {
console.log(err);
process.exit(1);
});
const loginRedirect = await fetch(
`https://login.pearson.com/v1/piapi/login/oidcauthorize?client_id=${clientId}&redirect_uri=https://reader-prod.gls.pearson-intl.com/bookshelf&nonce=123454321&prompt=login`,
{
redirect: 'manual',
},
)
.then((res) => {
return decodeURI(new URL(res.headers.get('location')).searchParams.get('login_success_url'));
})
.catch((err) => {
console.log(err);
process.exit(1);
});
const iesCode = new URL(loginRedirect).searchParams.get('iesCode');
const loginForm = new URLSearchParams();
loginForm.append('grant_type', 'password');
loginForm.append('username', email);
loginForm.append('password', password);
loginForm.append('client_id', clientId);
loginForm.append('login_success_url', loginRedirect);
const token = await fetch(
'https://login.pearson.com/v1/piapi/login/webcredentials?ts=' + new Date().valueOf(),
{
method: 'POST',
redirect: 'manual',
body: loginForm,
},
).then((res) => {
if (res.status != 200) {
throw new Error('Login failed');
}
return res.json();
});
const credentails = await fetch(
`https://login.pearson.com/v1/piapi/login/oidctoken?client_id=${clientId}&redirect_uri=undefined&grant_type=authorization_code&iesCode=${iesCode}`,
{
method: 'POST',
},
).then((res) => {
if (res.status != 200) throw new Error('Login failed');
return res.json();
});
const timestamp = await fetch('https://reader-prod.gls.pearson-intl.com/currentTime').then(
(res) => res.text(),
);
const { header: hawkHeader } = hawk.client.header(
'https://api-prod.gls.pearson-intl.com/user/session/token',
'POST',
{
credentials: {
id: 'd5b6fa06-c726-4897-8527-dec9bf2be9ae',
key: 'b753bcff-005f-4f74-86f5-882a1ce1b812',
algorithm: 'sha256',
},
ext: '',
timestamp,
},
);
const secuirtyToken = await fetch('https://api-prod.gls.pearson-intl.com/user/session/token', {
method: 'POST',
headers: {
Authorization: hawkHeader,
'Content-Type': 'application/json',
},
body: JSON.stringify({
firstname: token.data.firstName,
username: email,
}),
})
.then((res) => {
if (res.status != 200) throw new Error('Login failed');
return res.json();
})
.then((res) => res.token);
const user = await fetch('https://api-prod.gls.pearson-intl.com/user', {
headers: {
appid: '56dd9b6c1d40e68e859eedb1',
token: secuirtyToken,
},
}).then((res) => res.json());
const booklist = [];
const bookshelf = await fetch(`https://api-prod.gls.pearson-intl.com/user/${user.id}/product`, {
headers: {
appid: '56dd9b6c1d40e68e859eedb1',
token: secuirtyToken,
},
}).then((res) => res.json());
bookshelf.forEach((book) => {
booklist.push({
type: 'pdf',
title: book.title,
url: book.epubURL,
encpwd: book.encpwd,
});
});
const eTextBookshelf = await fetch(
'https://stpaperapi.prd-prsn.com/etext/v2/courseboot/convergedreader/compositeBookShelf/',
{
headers: {
'x-authorization': credentails.data.access_token,
},
},
).then((res) => res.json());
eTextBookshelf.entries.forEach((book) => {
booklist.push({
type: 'etext',
title: book.title,
url: book.uPdfUrl,
id: book.bookId,
});
});
if (booklist.length == 0) {
console.log('No books found');
process.exit(0);
}
console.log('Bookshelf:');
booklist.forEach((book, i) => {
console.log(`[${book.type.toUpperCase()}]\t ${i}) ${book.title}`);
});
const { bookIndex } = await prompt.get({
properties: {
bookIndex: {
description: 'Book ID [e to exit]: ',
required: true,
pattern: /^((\d+)|e)$/,
message: 'Book ID must be a number',
conform: (value) => {
return value == 'e' || -1 < parseInt(value) < booklist.length;
},
},
},
});
if (bookIndex == 'e') {
console.log('Good bye!');
console.log('Consider starring on github: https://github.com/Leone25/reader-plus-downloader');
process.exit(0);
}
if (booklist[bookIndex].type == 'pdf') {
const cipher = forge.cipher.createDecipher('AES-CBC', 'sDkjhfkj8yhn8gig');
cipher.start({
iv: forge.util.createBuffer(
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
'binary',
),
});
cipher.update(
forge.util.createBuffer(Buffer.from(bookshelf[bookIndex].encpwd, 'base64'), 'binary'),
);
cipher.finish();
console.log('Downloading book...');
const book = await fetch(bookshelf[bookIndex].epubURL).then((res) => res.arrayBuffer());
const bookZip = new zip(Buffer.from(book));
bookZip.list().forEach((file, i, arr) => {
const pdf = bookZip.extract(file.filepath, {
password: cipher.output.bytes(),
});
fs.writeFileSync(
`${bookshelf[bookIndex].title}${arr.length > 1 ? i : ''}.pdf`,
pdf,
'binary',
);
});
} else if (booklist[bookIndex].type == 'etext') {
console.log('Downloading book...');
const cdnToken = await fetch(
`https://etext-ise.pearson.com/marin/api/1.0/products/` + booklist[bookIndex].id + `/token`,
{
headers: {
authorization: credentails.data.token_type + ' ' + credentails.data.access_token,
},
},
).then((res) => res.json());
const book = await fetch(booklist[bookIndex].url, {
headers: {
[cdnToken.name]: cdnToken.value,
},
}).then((res) => res.arrayBuffer());
fs.writeFileSync(`${booklist[bookIndex].title}.pdf`, Buffer.from(book), 'binary');
} else {
console.log("Welp, this wasn't supposed to happen :L");
}
console.log('Done!');
console.log('Consider starring on github: https://github.com/Leone25/reader-plus-downloader');
console.log('Good bye!');
})();