批量下载异步社区的电子书

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


const request = require('request');
const Promise = require("bluebird");
const httpGET = Promise.promisify(request.get);
// const sleep = require('sleep');
const fs = require('fs');

const headers = {
'Accept-Language': 'zh-cn',
Host: "www.epubit.com",
Referer: "https://www.epubit.com/m/center/book",
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
accept: 'application/json, text/plain, */*',
Cookie: '此处填写你自己的cookie',
'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1',
}

async function getPdfList(page) {
var options = {
method: 'GET',
url: 'https://www.epubit.com/order/getBookshelf?page=' + page + '&rows=10&goodsType=0',
headers: headers
};

let ret = await httpGET(options);
let body = JSON.parse(ret.body);
// sum = sum + body.total;
return body.data;
}
/**
*
* @param {*} url
* @param {*} filename
* @param {*} callback
*/
async function downloadFile(url, filename, callback) {
var stream = fs.createWriteStream(filename);
var options = {
method: 'GET',
url: url,
headers: headers
};
request(options).pipe(stream).on('close', callback);
}


/**
*
* @param {*} list
*/
async function pdfListDown(list) {

list.forEach(v => {
let pdfList = v.book.pdfList;
console.log()
if (pdfList.length > 0) {
pdfList.forEach(v2 => {
if (v2.id) {
let name = v2.name;
let downUrl = 'https://www.epubit.com/book/downEbook/' + v2.id
console.log(name, downUrl)
downloadFile(downUrl, name, function(v) {
console.log(name + " download is ok")
})
}
})
}
});

}
(async() => {
let flag = true;
let page = 1;
do {
let s = await getPdfList(page);
console.log(s.rows.length);
if (s.rows.length == 0) {
console.log(s)
flag = false;
} else {
await pdfListDown(s.rows);
page++;
}
// sleep.sleep(10)
} while (flag);
})