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.
 

64 lines
2.1 KiB

import { fetch } from "bun";
import * as cheerio from "cheerio"
import fs from "node:fs";
import path from "node:path";
const output = path.resolve(__dirname, "../dist/wallhaven")
async function downImageByPage(page: number) {
const html = await (await fetch("https://wallhaven.cc/search?categories=110&purity=100&topRange=1M&sorting=toplist&order=desc&ai_art_filter=1&page=" + page, {
method: "GET"
})).text()
const $ = cheerio.load(html);
const previewList: string[] = []
$("#thumbs > section > ul > li > figure > a").each((index, el) => {
previewList[index] = $(el).attr("href") as string
});
for (let i = 0; i < previewList.length; i++) {
const previewURL = previewList[i];
console.log(`正在下载第${i + 1}张图片`);
await (() => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(1)
}, Math.random() * 2000 + 500);
})
})();
const html = await (await fetch(previewURL, {
method: "GET"
})).text()
const $ = cheerio.load(html);
const imageURL = $("#wallpaper").attr("src") as string
const p = path.resolve(output, "page-" + page + "/" + imageURL.split('/').slice(-1).join(''))
console.log("图片地址:", imageURL);
console.log("图片保存地址:", p);
if (!await checkExist(path.resolve(output, "page-" + page))) {
fs.mkdirSync(path.resolve(output, "page-" + page), { recursive: true })
}
const isExist = await checkExist(p)
if (!isExist) {
const stream = await (await fetch(imageURL)).arrayBuffer()
fs.writeFileSync(p, Buffer.from(stream!), { flag: "w" })
} else {
console.log("文件已存在,跳过下载", imageURL);
}
}
}
function checkExist(path: string) {
return new Promise((resolve, reject) => {
fs.access(path, fs.constants.F_OK, (err) => {
if (err) {
resolve(false)
} else {
resolve(true)
}
})
})
}
downImageByPage(1)