文件的下载
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
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