-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhelper.js
104 lines (91 loc) · 2.59 KB
/
helper.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
/**
* 各类帮助工具
*/
const fs = require('fs');
const _ = require('lodash');
const ArgumentError = require('./error/ArgumentError');
const playFileList = require('./config/playFileList');
const logger = require('./logger');
var helper = {
/**
* 得到要下载的文件名
* @param {object} chapter 下载章节信息
* @return {String} 文件名
*/
getChapterFileName : function(chapter){
let fileName = '';
if (typeof(chapter.section) === 'string'){
fileName = chapter.section;
}else if (typeof(chapter.section) === 'object'){
fileName = chapter.section.fileName;
}
return fileName;
},
/**
* 得到最后播放章节
* @return {JSON} 最后章节JSON,如无此文件或数据返回null
*/
getLastChapter : function () {
let file = null;
try{
if(fs.existsSync('./config/lastChapter.json'))
file = fs.readFileSync('./config/lastChapter.json');
file = JSON.parse(file);
}catch(e){
logger.error(e);
}
return file;
},
/**
* 得到下一次播放章节
* @return {JSON} 最后章节JSON,如无此文件或数据返回null
*/
getNextChapter : function (title,section) {
let playFileListConf = playFileList();
let currentPlaylistIndex = 0;
let nowPlayList = playFileListConf.find((value, index, arr) => {
if(value.title === title){
currentPlaylistIndex = index;
return true;
}
});
if(_.isEmpty(nowPlayList)){
nowPlayList = playFileListConf[0];
}
let currentSection = 0;
let flag = false;
let nowSection = nowPlayList.section.find((value, index, arr) => {
let mySection = '';
if (typeof(value) === 'string'){
mySection = value;
}else if (typeof(value) === 'object'){
mySection = value.fileName;
}
if(mySection === section) {
currentSection = index;
flag = true;
}
if(flag && index === (currentSection+1)) return true;
})
if(typeof nowSection === 'undefined'){
//换盘了
if(typeof playFileListConf[currentPlaylistIndex+1] === 'undefined') nowPlayList = playFileListConf[0];
else nowPlayList = playFileListConf[currentPlaylistIndex +1];
nowSection = nowPlayList.section[0];
}
var chapter = nowPlayList;
chapter.chapter = nowPlayList.title;
chapter.section = nowSection;
return chapter;
},
/**
* 设置最后章节文件
* @param {Sring} chapter 章节文件字符串
*/
setLastChapter : function(chapter){
if(_.isEmpty(chapter) || _.isEmpty(chapter.chapter) || _.isEmpty(chapter.section))
return new ArgumentError('参数不能为空');
fs.writeFileSync('./config/lastChapter.json',JSON.stringify(chapter));
}
};
module.exports = helper;