@lzwme/m3u8-dl
Version:
A free, open-source, and powerful m3u8 video batch downloader with multi-threaded downloading, play-while-downloading, WebUI management, video parsing, and more.
81 lines (80 loc) • 2.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommSearchApi = void 0;
const fe_utils_1 = require("@lzwme/fe-utils");
const req = new fe_utils_1.Request(null, {
'content-type': 'application/json; charset=UTF-8',
});
/**
* 基于采集站点 API 的通用搜索
* @example
* ```ts
* const v = new CommSearchApi({ api: 'https://api.xinlangapi.com/xinlangapi.php/provide/vod/' });
* v.search('三体')
* .then(d => {
* console.log(d.total, d.list);
* return v.getVideoList(d.list[0].vod_id);
* })
* .then(d => {
* console.log('detail:', d.total, d.list[0]);
* });
* ```
*/
class CommSearchApi {
options;
currentUrl;
apiMap = new Map();
get desc() {
return this.options.desc || this.options.api;
}
get key() {
return this.options.api;
}
get enable() {
return this.options.api && this.options.enable !== false;
}
constructor(options = {}) {
this.options = options;
if (options.api)
options.api = this.formatUrl(options.api)[0];
this.options = options;
}
async search(wd, api = this.options.api) {
let { data } = await req.get(api, { wd }, null, { rejectUnauthorized: false });
if (typeof data === 'string')
data = JSON.parse(data);
return data;
}
async detail(id, api = this.options.api) {
return this.getVideoList(id, api);
}
/** 按 id 取列表(每一项中包含了更为详细的内容) */
async getVideoList(ids, api = this.options.api) {
let { data } = await req.get(api, {
ac: 'videolist',
ids: Array.isArray(ids) ? ids.join(',') : ids,
}, null, { rejectUnauthorized: false });
if (typeof data === 'string')
data = JSON.parse(data);
return data;
}
formatUrl(url) {
const urls = [];
if (!url)
return urls;
if (typeof url === 'string')
url = [url];
for (let u of url) {
u = String(u || '').trim();
if (u.startsWith('http')) {
if (u.endsWith('provide/'))
u += 'vod/';
if (u.endsWith('provide/vod'))
u += '/';
urls.push(u.replace('/at/xml/', '/'));
}
}
return [...new Set(urls)];
}
}
exports.CommSearchApi = CommSearchApi;