-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
148 lines (139 loc) · 4.05 KB
/
app.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
var async = require('async');
var cheerio = require('cheerio');
var mkdirp = require("mkdirp");
var fs = require("fs");
var http = require("http");
var config = require("./config")
var imoocUrl = 'http://www.imooc.com/learn/'+config.lessonId;
var courseData = [];
var mainPath = config.rootPath;
//获取视频id
var getIds = (imoocUrl)=>{
return new Promise((resolve,reject) => {
var req = http.get(imoocUrl,(res)=>{
var datas = "";
res.on("data",(data)=>{
datas+=data
})
res.on("end",()=>{
//视频id
var videoIds = [];
var $ = cheerio.load(datas);
mainPath = mainPath + $(".course-infos h2").text().trim();
$('.mod-chapters .chapter').each(function (idx, element) {
var $element = $(element);
$element.find(".chapter-content").remove();
//章节标题
var courseTitle = $element.find("h3 strong").text().trim();
//创建文件路径
mkdirp(mainPath+"/"+courseTitle,function(err){
if (err) {
return console.error(err);
}
});
//课程id,名称
var courseIds = [],fileNames = [];
$element.find("li a").each(function(idx,element){
var $element = $(element);
$element.children().remove();
var id = $element.attr('href').trim().split('video/')[1];
var name = $element.text().replace(/\(.*\)/,'').replace(/\(.*\)/,'').replace(/\(.*\)/,"").trim();
console.log(name)
videoIds.push(id);courseIds.push(id);fileNames.push(name)
})
courseData.push({
courseTitle:courseTitle,
courseIds:courseIds,
fileNames:fileNames
})
});
resolve(videoIds)
})
});
req.on('error', ()=>{
reject("getIds request error")
});
req.end();
})
}
//获取视频地址
var getUrl = (videoId)=> {
return new Promise((resolve,reject) => {
var videoUrl = 'http://www.imooc.com/course/ajaxmediainfo/?mid=' + videoId + '&mode=flash';
var req = http.get(videoUrl,(res)=>{
var datas = ""
res.on("data",(data)=>{
datas+=data
})
res.on("end",()=>{
var url = JSON.parse(datas).data.result.mpath[0];
var video = {
'url':url,
'id':videoId
}
resolve(video)
})
})
req.on('error', ()=>{
console.log("getUrl request error");
});
req.end();
})
}
//建立下载任务
var startDownload = (videoUrl,videoId)=>{
return new Promise((resolve,reject) =>{
var path;
var fileName;
for(var i in courseData){
var courseIds = courseData[i].courseIds;
for (var j in courseIds) {
if(courseIds[j]==videoId){
path = mainPath + "/" + courseData[i].courseTitle + "/";
fileName = courseData[i].fileNames[j] + ".mp4";
console.log("开始下载:",fileName,"\r\n");
break
}
};
}
var req = http.request(videoUrl,(res)=>{
var fileBuff = [];
res.on("data",(data)=>{
var buffer = new Buffer(data);
fileBuff.push(buffer)
})
var contentLength = res.headers['Content-Length']-0;
res.on('end', function() {
var totalBuff = Buffer.concat(fileBuff);
if (totalBuff.length < contentLength) {
startDownload(videoUrl,videoId);
return console.log(videoUrl + " download error, try again");
}
fs.appendFile(path + fileName, totalBuff, function(){
console.log("下载完毕: ",fileName,"\r\n")
});
resolve(null)
});
})
req.on('error', ()=>{
console.log("startDownload request error,try again");
startDownload(videoUrl,videoId)
});
req.end();
})
}
//建立任务队列
var taskList = (videoIds)=>{
//并发下载控制
async.mapLimit(videoIds,config.taskNumber,function(videoId,callback){
getUrl(videoId).then((video) =>{
startDownload(video.url,video.id).then(callback)
})
}, function (err, result) {})
}
//开始下载
getIds(imoocUrl).then((videoIds) => {
taskList(videoIds)
}).catch((error) => {
console.log(error)
})