import fs from "node:fs"; import axios from 'axios' import child_process from 'node:child_process' import util from "util"; import { BILI_BVID_TO_CID, BILI_DYNAMIC, BILI_PLAY_STREAM, BILI_SCAN_CODE_DETECT, BILI_SCAN_CODE_GENERATE, BILI_VIDEO_INFO } from "../constants/tools.js"; import { mkdirIfNotExists } from "./file.js"; import qrcode from "qrcode" const biliHeaders = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36', referer: 'https://www.bilibili.com', } /** * 下载单个bili文件 * @param url * @param fullFileName * @param progressCallback * @returns {Promise} */ export async function downloadBFile(url, fullFileName, progressCallback) { return axios .get(url, { responseType: 'stream', headers: { ...biliHeaders }, }) .then(({ data, headers }) => { let currentLen = 0; const totalLen = headers['content-length']; return new Promise((resolve, reject) => { data.on('data', ({ length }) => { currentLen += length; progressCallback?.(currentLen / totalLen); }); data.pipe( fs.createWriteStream(fullFileName).on('finish', () => { resolve({ fullFileName, totalLen, }); }), ); }); }); } /** * 获取下载链接 * @param url * @returns {Promise} */ export async function getDownloadUrl(url) { return axios .get(url, { headers: { ...biliHeaders }, }) .then(({ data }) => { const info = JSON.parse( data.match(/