import fs from "node:fs"; import axios from 'axios' import child_process from 'node:child_process' function downloadBFile (url, fullFileName, progressCallback) { return axios .get(url, { responseType: 'stream', headers: { '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', }, }) .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, }); }), ); }); }); } function getDownloadUrl (url) { return axios .get(url, { headers: { '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', }, }) .then(({ data }) => { const info = JSON.parse( data.match(/