@lzwme/m3u8-dl
Version:
Batch download of m3u8 files and convert to mp4
142 lines (141 loc) • 6.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VideoSerachAndDL = VideoSerachAndDL;
const console_log_colors_1 = require("console-log-colors");
const enquirer_1 = require("enquirer");
const m3u8_batch_download_js_1 = require("../m3u8-batch-download.js");
const ApiManage_1 = require("./search-api/ApiManage");
const storage_js_1 = require("./storage.js");
const utils_js_1 = require("./utils.js");
async function VideoSerachAndDL(keyword, options, baseOpts) {
utils_js_1.logger.debug(options, baseOpts);
const cache = storage_js_1.stor.get();
const doDownload = async (info, urls) => {
const p = await (0, enquirer_1.prompt)({
type: 'confirm',
name: 'play',
initial: baseOpts.play,
message: `【${(0, console_log_colors_1.greenBright)(info.vod_name)}】是否边下边播?`,
});
baseOpts.play = p.play;
try {
cache.latestSearchDL = {
...cache.latestSearchDL,
info,
urls,
dlOptions: { filename: info.vod_name.replaceAll(' ', '_'), ...baseOpts },
};
storage_js_1.stor.save({ latestSearchDL: cache.latestSearchDL });
const r = await (0, m3u8_batch_download_js_1.m3u8BatchDownload)(cache.latestSearchDL.urls, cache.latestSearchDL.dlOptions);
if (r)
storage_js_1.stor.set({ latestSearchDL: null });
}
catch (error) {
utils_js_1.logger.info('cancel download:', error.message);
}
};
if (cache.latestSearchDL?.urls) {
const p = await (0, enquirer_1.prompt)({
type: 'confirm',
name: 'k',
initial: true,
message: `存在上次未完成的下载【${(0, console_log_colors_1.greenBright)(cache.latestSearchDL.info.vod_name)}】,是否继续?`,
});
if (p.k) {
await doDownload(cache.latestSearchDL.info, cache.latestSearchDL.urls);
}
else {
storage_js_1.stor.set({ latestSearchDL: null });
}
}
if (options.apidir && !ApiManage_1.apiManage.current)
ApiManage_1.apiManage.load(options.apidir);
if (options.url) {
options.url.forEach(api => ApiManage_1.apiManage.add({ api, desc: api }));
}
await ApiManage_1.apiManage.select();
await (0, enquirer_1.prompt)({
type: 'input',
name: 'k',
message: '请输入关键字',
validate: value => value.length > 1,
initial: keyword,
}).then(v => {
keyword = v.k;
});
const sRes = await ApiManage_1.apiManage.search(keyword, ApiManage_1.apiManage.current);
utils_js_1.logger.debug(sRes);
if (!sRes.length) {
console.log(console_log_colors_1.color.green(`[${keyword}]`), '没有搜到结果');
return VideoSerachAndDL(keyword, options, baseOpts);
}
const choices = sRes.map((d, idx) => ({
name: d.vod_id,
message: `${idx + 1}. [${d.type_name}] ${d.vod_name}`,
hint: `${d.vod_remarks}(${d.vod_time})`,
}));
const answer1 = await (0, enquirer_1.prompt)({
type: 'select',
name: 'vid',
pointer: '👉',
message: `查找到了 ${console_log_colors_1.color.greenBright(sRes.length)} 条结果,请选择:`,
choices: choices.concat({ name: -1, message: (0, console_log_colors_1.greenBright)('重新搜索'), hint: '' }),
});
if (answer1.vid === -1)
return VideoSerachAndDL(keyword, options, baseOpts);
const vResult = await ApiManage_1.apiManage.detail(sRes.find(d => d.vod_id === answer1.vid));
if (!vResult) {
utils_js_1.logger.error('获取视频信息失败!');
return VideoSerachAndDL(keyword, options, baseOpts);
}
const info = vResult.list[0];
if (!info.vod_play_url) {
utils_js_1.logger.error('未获取到播放地址信息', info);
return VideoSerachAndDL(keyword, options, baseOpts);
}
if (!info.vod_play_note || !String(info.vod_play_url).includes(info.vod_play_note)) {
['#', '$'].some(d => {
if (info.vod_play_url.includes(d)) {
info.vod_play_note = d;
return true;
}
return true;
});
}
const urls = info.vod_play_url
.split(info.vod_play_note || '$')
.find(d => d.includes('.m3u8'))
.split('#');
utils_js_1.logger.debug(info, urls);
const r = (key, desc) => (info[key] ? ` [${desc}] ${(0, console_log_colors_1.greenBright)(info[key])}` : '');
console.log([
`\n [名称] ${(0, console_log_colors_1.cyanBright)(info.vod_name)}`,
r('vod_sub', '别名'),
` [更新] ${(0, console_log_colors_1.greenBright)(info.vod_remarks)}(${(0, console_log_colors_1.gray)(info.vod_time)})`,
r('vod_total', '总集数'),
r('type_name', '分类'),
r('vod_class', '类别'),
r('vod_writer', '作者'),
r('vod_area', '地区'),
r('vod_lang', '语言'),
r('vod_year', '年份'),
r('vod_douban_score', '评分'),
r('vod_pubdate', '上映日期'),
`\n${(0, console_log_colors_1.green)((info.vod_content || info.vod_blurb).replace(/<\/?.+?>/g, ''))}\n`, // 描述
]
.filter(Boolean)
.join('\n'), '\n');
const answer = await (0, enquirer_1.prompt)({
type: 'select',
name: 'url',
choices: [
{ name: '1', message: (0, console_log_colors_1.green)('全部下载') },
{ name: '-1', message: (0, console_log_colors_1.cyanBright)('重新搜索') },
].concat(urls.map((d, i) => ({ name: d, message: `${i + 1}. ${d}` }))),
message: `获取到了 ${console_log_colors_1.color.magentaBright(urls.length)} 条视频下载地址,请选择:`,
});
if (answer.url !== '-1') {
await doDownload(info, answer.url === '1' ? urls : [answer.url]);
}
return VideoSerachAndDL(keyword, options, baseOpts);
}