1 Star 0 Fork 0

海风/node-reptile

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
index.js 2.51 KB
一键复制 编辑 原始数据 按行查看 历史
import { existsSync, mkdirSync, createWriteStream } from "fs";
import { load } from "cheerio";
import { request } from "./utils.js";
const URL = "https://yys.163.com/media/picture.html";
const BASE_PATH = "https://yys.res.netease.com/";
// 试试多个请求下载
const multiple = 5;
// 需要下载的图片集合
let urlDataArr = [];
// 默认下载全部尺寸的图片
const IMG_SIZES = [
"2732x2048",
"2208x1242",
"2048x1536",
"1920x1080",
"1440x900",
"1366x768",
];
/**
* 创建目录
* @param {String} url
* @returns
*/
function depthMkdirsSync(url) {
if (existsSync(url)) return;
let parts = url.split("/").filter((part) => !!part);
let path = "";
for (let i = 0; i < parts.length; i++) {
path += parts[i] + "/";
if (!existsSync(path)) {
mkdirSync(path);
}
}
}
/**
* 下载保存图片
* @param {Array<{url: String; size: String; name: String}>} arr
*/
const savedImg = async (imgs) => {
if (!imgs) return;
const { url, size, name } = imgs;
const _url = `img/${size}/${name}.jpg`;
if (existsSync(_url)) {
if (urlDataArr.length) {
savedImg(urlDataArr.shift());
} else {
console.log("下载完毕");
}
return;
}
console.log(`正在下载[${name}]\n`);
try {
const imgBuf = await request(url);
const len = imgBuf.reduce((t, n) => t + n.length, 0);
const img = Buffer.concat(imgBuf, len);
const stream = createWriteStream(_url);
stream.write(img);
stream.close();
console.log(`[${name}]文件下载完毕,剩余[${urlDataArr.length}]\n`);
} catch (e) {
} finally {
if (urlDataArr.length) {
savedImg(urlDataArr.shift());
} else {
console.log("下载完毕");
}
}
};
const statrtRequest = async (url) => {
// 请求目标地址
const html = await request(url, { encod: "utf-8" });
const $ = load(html.join(""));
let urlDataArr = [];
$(".target").each((_, item) => {
let url = item.attribs["href"];
if (
url.includes(BASE_PATH) &&
IMG_SIZES.some((size) => url.includes(size))
) {
const size = url.split("/").pop().split(".")[0];
const name = url.split("/data/picture/")[1].replace(/[\/\.]/g, "_");
urlDataArr.push({ url, size, name });
}
});
// 创建目录 --根据 图片尺寸 IMG_SIZES 创建目录
IMG_SIZES.map((size) => {
depthMkdirsSync(`img/${size}`);
});
// 下载保存图片
return urlDataArr;
};
urlDataArr = await statrtRequest(URL);
for (let i = 0; i < multiple; i++) {
const _img = urlDataArr.shift();
savedImg(_img);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lhf6623/node-reptile.git
git@gitee.com:lhf6623/node-reptile.git
lhf6623
node-reptile
node-reptile
main

搜索帮助