-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
71 lines (64 loc) · 2.17 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
/**
* 入口文件
*/
const helper = require('./helper');
const playFileList = require('./config/playFileList')();
const downloadHandle = require('./downloadHandle');
const openLiveHandle = require('./openLiveHandle');
const videoHandle = require('./videoHandle');
const del = require('del');
const fs = require('fs');
const logger = require('./logger');
var globalLiveInfo = null;
logger.info('启动');
//1.读取上次章节播放
var chapter = helper.getLastChapter();
if(!chapter){
//如果没有,设置playFileList中的第一个为播放文件
chapter = playFileList[0];
chapter.chapter = playFileList[0].title;
chapter.section = playFileList[0].section[0];
helper.setLastChapter(chapter);
}
function errorHandle(err) {
del.sync([`${__dirname}/data`]);
logger.error("遇到异常,删除缓存文件,中断进程");
logger.error(err);
process.exit();
}
function loopLogic(currentChapter) {
return openLiveHandle.checkIsLive().then(function(isLive){
if (!isLive){
throw new Error("检测到没有开始直播!!重启服务器!!");
}
return Promise.all([
videoHandle.pushStream(currentChapter,globalLiveInfo),
new Promise((reslove,reject) => {
let nextChapter = helper.getNextChapter(currentChapter.chapter,helper.getChapterFileName(currentChapter));
return downloadHandle.downloadByCS(nextChapter.chapter,helper.getChapterFileName(nextChapter)).then(() =>{
helper.setLastChapter(nextChapter);
reslove(nextChapter);
});
})
])
}) .then(([unuseful,nextChapter]) => {
del([`${__dirname}/data/${helper.getChapterFileName(chapter)}`]);
chapter = nextChapter;
return loopLogic(nextChapter);
}).catch(function(err){
errorHandle(err);
});
}
//2.根据读取到的章节去百度云盘上去数据
openLiveHandle.startLiveAndGetInfo().then((liveInfo) => {
globalLiveInfo = liveInfo;
return fs.existsSync(`./data/${helper.getChapterFileName(chapter)}`) ;
}).then((haveDownLoadFile) =>{
if(!haveDownLoadFile) return downloadHandle.downloadByCS(chapter.chapter,helper.getChapterFileName(chapter));
return;
}).then(() => {
logger.info('开始直播!!!');
return loopLogic(chapter);
}).catch(function (err){
errorHandle(err);
});