个人学习站点 个人学习站点
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • Nodejs
  • 分享
  • 学习
  • 面试
  • 我的项目
  • 心情杂货
  • 实用技巧
  • 游玩
  • 英语学习
  • 收藏
GitHub (opens new window)

Darren Zhang

前端界的小学生
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • Nodejs
  • 分享
  • 学习
  • 面试
  • 我的项目
  • 心情杂货
  • 实用技巧
  • 游玩
  • 英语学习
  • 收藏
GitHub (opens new window)
  • 技术文档

  • GitHub技巧

  • Nodejs

    • nodejs递归读取所有文件
    • puppeteer的使用
    • 文件的下载
  • 博客搭建

  • 分享

  • 抠图
  • 技术
  • Nodejs
nzjcnjzx
2022-09-01

文件的下载

const axios = require("axios");
const fs = require("fs");
const path = require("path");
const data = require("./1.json");
const errorUrls = {};
async function getFile(uri, m3u8) {
  const fileName = uri.split("/").slice(-1)[0];
  const filePath = m3u8;
  errorUrls[filePath] = errorUrls[filePath] || [];
  try {
    await downloadFile(uri, filePath, fileName);
  } catch (error) {
    console.error("errr", error);
    errorUrls[filePath].push(uri);
  }
  fs.writeFileSync("1.json", JSON.stringify(errorUrls));
}

async function downloadFile(imgUrl, filepath, flieName) {
  if (!fs.existsSync(filepath)) {
    fs.mkdirSync(filepath);
  }
  const mypath = path.resolve(filepath, flieName);
  console.log(mypath);
  const writer = fs.createWriteStream(mypath); // 创建写入对象
  const response = await axios({
    url: imgUrl,
    method: "GET",
    responseType: "stream",
    timeout: 10000,
  }); // 请求图片地址获取二进制数据流
  response.data.pipe(writer); // 写入图片数据到文件中
  return new Promise((resolve, reject) => {
    writer.on("finish", resolve);
    writer.on("error", reject);
  });
}

Object.entries(data).forEach(([m3u8, urls]) => {
  for (const url of urls) {
    getFile(url, m3u8);
  }
});

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
上次更新: 2022/09/01, 23:08:32
puppeteer的使用
解决百度无法收录搭建在GitHub上的个人博客的问题

← puppeteer的使用 解决百度无法收录搭建在GitHub上的个人博客的问题→

最近更新
01
族谱小程序
01-10
02
2024读书
01-03
03
puppeteer的使用
07-17
更多文章>
Theme by Vdoing | Copyright © 2022-2025 darren Zhang | 鄂ICP备2024032161号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式