You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

111 lines
3.8 KiB

import { fetch } from "bun"
import fs from "node:fs"
import * as cheerio from "cheerio"
// https://bz.zzzmh.cn/index
// const stream = await (await fetch("https://api.zzzmh.cn/v2/bz/v3/getUrl/1b87e2c5880511ebb6edd017c2d2eca211")).arrayBuffer()
// const stream = await (await fetch("https://api.zzzmh.cn/v2/bz/v3/getUrl/393204c88fff4715a4b4aa88aabdb2ed21")).arrayBuffer()
// fs.writeFileSync("./a.png", Buffer.from(stream!))
let totalPage = 1
async function downImageByPage(current: number) {
const res = await (await fetch("https://api.zzzmh.cn/v2/bz/v3/getData", {
method: "POST",
headers: {
'Content-Type': 'application/json;charset=utf-8',
referer: "https://bz.zzzmh.cn/"
},
body: `{ "size": 24, "current": ${current}, "sort": 0, "category": 0, "resolution": 0, "color": 0, "categoryId": 0, "ratio": 0 }`
})).json()
totalPage = res.data.totalPage
for (let i = 0; i < res.data.list.length; i++) {
const item = res.data.list[i];
console.log(`正在下载第${i + 1}张图片`);
await (() => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(1)
}, Math.random() * 2000 + 500);
})
})();
const stream = await (await fetch("https://api.zzzmh.cn/v2/bz/v3/getUrl/" + item.i + (item.t == 2 ? "21" : "11"))).arrayBuffer()
const p = "./dist/" + "page-" + current + "/" + item.i + ".png"
if (!await checkExist("./dist/" + "page-" + current)) {
fs.mkdirSync("./dist/" + "page-" + current, { recursive: true })
}
const isExist = await checkExist(p)
if (!isExist) {
fs.writeFileSync(p, Buffer.from(stream!), { flag: "w" })
} else {
console.log("文件已存在,跳过下载", item.i + ".png");
}
}
}
downImageByPage(2)
function checkExist(path: string) {
return new Promise((resolve, reject) => {
fs.access(path, fs.constants.F_OK, (err) => {
if (err) {
resolve(false)
} else {
resolve(true)
}
})
})
}
// async function readAllChunks(readableStream: any) {
// const reader = readableStream.getReader();
// const chunks = [];
// let done, value;
// while (!done) {
// ({ value, done } = await reader.read());
// if (done) {
// return chunks;
// }
// chunks.push(value);
// }
// }
// const rr = await readAllChunks(stream)
// fs.writeFileSync("./a.png", Buffer.from(rr!))
// const maxNum = 3
// function request(opts: any, cb: any, num = 0) {
// https.get(opts, function (res: IncomingMessage) {
// if (res.statusCode === 302) {
// if (num > maxNum) {
// console.error("重定向次数超过三次,已强制终止");
// return
// }
// let url = new URL(res.headers['location']!)
// if (url) {
// console.log(`重定向:`, url.href);
// num++
// // 重定向
// request({
// ...opts,
// hostname: url.hostname,
// path: url.pathname + url.search,
// }, cb, num)
// }
// } else if (res.statusCode === 200) {
// cb && cb(res)
// } else {
// console.log(`当前${opts.hostname}请求${opts.path}未成功`, res.statusCode);
// // res.on('data', (d) => {
// // process.stdout.write(d);
// // });
// }
// })
// }
// fetch("https://bz.zzzmh.cn/index")
// .then((res) => res.text())
// .then((html) => {
// const $ = cheerio.load(html)
// console.log($("title").text());
// });