Skip to content

Commit d3e3e9d

Browse files
author
lihang1870719
committed
readme.md
1 parent e146256 commit d3e3e9d

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed

README.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# 豆丁书房文档下载至PC端
2+
3+
标签(空格分隔): 豆丁 nodejs
4+
5+
---
6+
7+
[TOC]
8+
9+
---
10+
最近因为要写一些小论文,有时需要在豆丁网下载文档,但是豆丁上的文档大部分是要钱的,有三种途径,一种是充VIP,但是VIP也是下载的次数也是有限的;另一种是直接充钱(此处土豪直接略过);最后一种是豆丁出的神器--豆丁书房APP(此处是不是有打广告的嫌疑,囧),但是这种方法是有局限的,只能在手机端去查看,有点麻烦,于是就有了这一片blog的由来,我用nodejs写了一个小脚本,直接下载文档到PC端,下面就是详细的过程。
11+
12+
---
13+
## 使用步骤
14+
1. 由于本插件是基于nodejs的,所以需要配置nodejs的环境
15+
2. 访问[我的github](https://github.com/lihang1870719/douding-download)clone到本地
16+
3. 执行npm install
17+
4. 执行node getFile 文档就会自动下载到当前目录
18+
19+
## 详解
20+
---
21+
### Step 1. 获取文档对应ID号
22+
进入豆丁网,找到大家需要下载的文档,例如《42套最成功个人简历模板大集合 求职简历 英文简历》
23+
24+
![snapshot](https://github.com/lihang1870719/douding-download/blob/master/snapshot.png?raw=true)
25+
26+
大家可以看到这篇文档时需要充值才可以下载的。注意图片最上面的地址栏:www.douin.com/p-531956905.html。其中“531956905”就是代表这篇文档的ID号。那么读者可能会问,这个ID有什么用,OK,下面进入正题。
27+
28+
### Step 2. 根据ID号获得文档
29+
上代码:
30+
``` javascript
31+
var superagent = require('superagent');
32+
var async = require('async');
33+
var fs = require('fs');
34+
```
35+
可以看到插件主要使用了3个模块:
36+
37+
- superagent:用来模拟http请求
38+
- async:用来对代码进行控制,避免陷入callback-callback-callback
39+
- fs:文件操作
40+
41+
插件中主要有4个函数
42+
43+
- getFile
44+
- getInvoiceId
45+
- getFileAddr
46+
- downFile
47+
48+
---
49+
getFile函数:
50+
```
51+
var tasks = [getInvoiceId, getFileAddr, downFile];
52+
var getFile = function (documentId) {
53+
var documentId = documentId;
54+
postText = 'value={"body":{"type":"1",'
55+
+ '"document_ids":["' + documentId +'"]},'
56+
+ '"account":"-fkKFxPvVLXOUxYkMY91S",'
57+
+ '"pwd":"1t8o4wWbnARtCij8JSgSUv",'
58+
+ '"type":"collection",'
59+
+ '"device_info":{"screen":"1080x1800","model":"android"},'
60+
+ '"version":"6.30","engine_id":"Android_BookStore"}';
61+
async.waterfall(tasks, function (err) {
62+
if(err) console.error(err.stack);
63+
console.log("download ok");
64+
});
65+
}
66+
getFile(531956905);
67+
```
68+
getFile函数是程序的入口,大家只需要将自己要下载的文档ID换掉就可以欢乐的下载了
69+
70+
---
71+
getInvoiceId函数
72+
```
73+
var getInvoiceId = function (callback) {
74+
console.log("in invoiceid");
75+
superagent.post(url)
76+
.set('Content-Type', 'application/x-www-form-urlencoded')
77+
.set('Connection', 'Keep-Alive')
78+
.set('User-Agent', 'Android')
79+
.send(postText)
80+
.end(function (err, res){
81+
if (err)
82+
{
83+
console.error(err);
84+
}
85+
else
86+
{
87+
var o = JSON.parse(res.text);
88+
var documents = o.body.documents[0];
89+
invoiceid = documents.invoiceid;
90+
var ext = documents.ext;
91+
}
92+
callback(null, invoiceid, ext);
93+
});
94+
}
95+
```
96+
这里需要InvoiceID,是因为在下一步获取下载地址是需要的,推荐大家使用wireshark来抓包分析
97+
98+
---
99+
getFileAddr
100+
```
101+
var getFileAddr = function (arg1, args2, callback) {
102+
console.log('in fileAddr');
103+
var postFileText = 'un=-fkKFxPvVLXOUxYkMY91S&'
104+
+ 'pwd=1t8o4wWbnARtCij8JSgSUv'
105+
+ '&invoiceid=' + arg1
106+
+ '&isfolder=false'
107+
+ '&encrypt=3'
108+
+ '&from=1'
109+
+ '&platform=android'
110+
+ '&version=2.5.9';
111+
superagent.post(urlFile)
112+
.set('Content-Type', 'application/x-www-form-urlencoded')
113+
.set('Connection', 'Keep-Alive')
114+
.set('User-Agent', 'Android')
115+
.send(postFileText)
116+
.end(function (err, res){
117+
if (err)
118+
{
119+
console.error(err);
120+
}
121+
else
122+
{
123+
var o = JSON.parse(res.text);
124+
var downLoadPath = o.result.url;
125+
var documentName = o.result.name;
126+
console.log("downLoadPath: " + downLoadPath);
127+
console.log(o);
128+
}
129+
callback(null, downLoadPath, documentName, args2);
130+
});
131+
}
132+
```
133+
这里利用获得的invoiceId获得文件下载的地址和下载文件名
134+
135+
---
136+
downFile函数
137+
```
138+
var downFile = function (args1, args2, args3, callback) {
139+
var path = process.cwd() + "\\File\\" + args2 + '.' + args3;
140+
if (args1 == "")
141+
{
142+
callback(null);
143+
}
144+
fs.exists(path, function(args){
145+
if (args == true)
146+
{
147+
callback(null);
148+
}
149+
else
150+
{
151+
console.log(args1);
152+
console.log(path);
153+
var stream = fs.createWriteStream(path);
154+
var req = superagent.get(args1)
155+
.end(function (err, res){
156+
console.log(res.type);
157+
});
158+
req.pipe(stream).on('finish', function(err){
159+
if (err)
160+
{
161+
console.error(err);
162+
}
163+
callback(null);
164+
});
165+
}
166+
});
167+
}
168+
```
169+
OK,最后一步,下载到本地就可以啦
170+
171+
172+
173+
174+
175+

0 commit comments

Comments
 (0)