-
Notifications
You must be signed in to change notification settings - Fork 29
/
app.js
151 lines (139 loc) · 4.67 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
149
150
151
var fs = require('fs')
, path = require('path')
, fmt = require('util').format
, readline = require('readline')
, request = require('request')
, cheerio = require('cheerio')
, progress = require('request-progress')
, ProgressBar = require('progress')
, open = require('open')
, colors = require('colors');
var bar = new ProgressBar('正在下载::title [:bar] :percent :etas', {
complete: '=',
incomplete: ' ',
width: 20,
clear: true,
total: 100
});
var currFm = '';
var playList = [];
var isDownloading = -1; // the music being downloaded
// make a download dir if not exists
var downloadDir = './downloads'
if (!fs.existsSync(downloadDir)) {
fs.mkdirSync(downloadDir);
}
function getFm(fmUrl) {
console.log('正在获取期刊信息...'.yellow);
request(fmUrl, function (err, res, html) {
// parse playlist
$ = cheerio.load(html);
$('li.track-item').each(function () {
var trackItem = $(this);
var mp3Info = {};
var titleInfo = trackItem.find('a.trackname').text().split('.');
mp3Info.id = titleInfo[0].trim();
mp3Info.title = titleInfo[1].trim();
mp3Info.mp3 = fmt('http://mp3-cdn2.luoo.net/low/luoo/radio%s/%s.mp3', $('span.vol-number').text(), mp3Info.id);
mp3Info.artist = trackItem.find('p.artist').text().split(':')[1].trim();
mp3Info.album = trackItem.find('p.album').text().split(':')[1].trim();
mp3Info.poster = trackItem.find('a.btn-action-share').attr('data-img');
playList.push(mp3Info);
});
// parse fm info and make music dir
var fmTitle = $('span.vol-title').text();
currFm = fmTitle;
var fmIntro = $('div.vol-desc').text().trim();
var fmCover = $('img.vol-cover').attr('src');
var fmPath = path.join(downloadDir, fmTitle);
var introPath = path.join(downloadDir, fmTitle, fmTitle + '.txt');
var coverPath = path.join(downloadDir, fmTitle, fmTitle + '.jpg')
if (!fs.existsSync(fmPath)) {
fs.mkdirSync(fmPath);
fs.writeFile(introPath, fmIntro.replace(/<br>/g, '\r\n').trim());
request(fmCover).pipe(fs.createWriteStream(coverPath));
}
createTermMenu();
});
}
function createTermMenu() {
var List = require('term-list');
var menu = new List({ marker: '>'.red + ' ', markerLength: 2 });
menu.on('keypress', function(key, index) {
if (key.name === 'return') {
if (index == -4) {
open('https://github.com/stanzhai/luoo-down');
}
if (index < 0 || isDownloading != -1) {
return;
}
var mp3Info = playList[index];
downloadMP3(mp3Info);
} else if (key.name === 'q') {
return menu.stop();
}
});
menu.add(-1, '[期刊名]:' + currFm);
menu.add(-2, Array(60).join('-'));
for (var i = 0; i < playList.length; i++) {
var info = playList[i];
menu.add(i, (i + 1) + '. ' + info.title + '[' + (info.artist + '-' + info.album).green + ']');
};
menu.add(-3, Array(60).join('-'));
menu.add(-4, 'Fork me on GitHub: luoo-down by Stan Zhai, 2014-5-24 night'.grey.underline);
menu.add(-5, '仅供技术学习分享,音乐涉及版权,请勿批量下载,下载后请及时删除!'.red);
menu.start();
menu.select(0);
}
function downloadMP3(mp3Info) {
var coverFile = path.join(downloadDir, currFm, mp3Info.title + '.jpg');
request(mp3Info.poster).pipe(fs.createWriteStream(coverFile));
var mp3File = path.join(downloadDir, currFm, mp3Info.title + '.mp3');
if (!fs.existsSync(mp3File)) {
var lastReceived = 0;
progress(request(mp3Info.mp3))
.on('progress', function (state) {
isDownloading = mp3Info.id;
bar.total = state.total;
bar.tick(state.received - lastReceived, {title: mp3Info.title});
lastReceived = state.received;
})
.pipe(fs.createWriteStream(mp3File))
.on('close', function (err) {
// download ended, reset bar state
bar.tick(bar.total - bar.curr);
bar.curr = 0;
isDownloading = -1;
});
} else {
open(mp3File);
}
}
function setError(err) {
console.log(err.red);
}
function main() {
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log('请输入您喜欢的落网期刊地址或期刊号\r\n如:http://www.luoo.net/vol/index/726或726');
var ask = '[default is 726]:';
rl.question(ask, function(answer) {
if (answer.trim().length == 0) {
answer = 726;
}
if (/^\d+$/.test(answer)) {
answer = 'http://www.luoo.net/vol/index/' + answer;
} else {
answer = answer || 'http://www.luoo.net';
}
getFm(answer);
rl.close();
});
}
process.on('uncaughtException', function(err) {
setError(err.message + '\r\n这个错误有可能是您输入了错误的期刊导致');
main();
});
main();