-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
121 lines (99 loc) · 2.93 KB
/
index.js
File metadata and controls
121 lines (99 loc) · 2.93 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const fs = require("fs");
const axios = require("axios");
const { exec } = require("child_process");
const urls = [];
const directory = "";
const writePathFile = async (names) => {
try {
await fs.writeFile(`path/${directory}.txt`, names.join("\n"), (err) => {
if (err) {
console.error("err writing file", err);
} else {
console.log(`Array written to path/${directory}.txt successfully.`);
}
});
if (urls.length === 1) {
copyFileToMediaFolder();
fs.rm(`./${directory}`, { recursive: true, force: true }, (error) => {
if (error) {
console.error(error);
return;
}
console.log(`${directory} is deleted`);
return;
});
}
concatIntoOneFile(directory);
return;
} catch (error) {
console.error("error ocurred while writing path file", error);
}
};
const concatIntoOneFile = (fileName) => {
const ffmpegCommand = `ffmpeg.exe -f concat -safe 0 -i .\\path\\${fileName}.txt .\\media\\${fileName}.mp3`;
console.log("run ffmpeg", ffmpegCommand);
exec(ffmpegCommand, (error) => {
if (error) {
console.error(`Error executing FFmpeg: ${error.message}`);
return;
}
console.log("FFmpeg command executed successfully.");
try {
fs.rm(`./${directory}`, { recursive: true, force: true }, (error) => {
if (error) {
console.error(error);
return;
}
console.log(`${directory} is deleted`);
return;
});
} catch (err) {
console.error("Error occurred:", err);
}
});
};
const copyFileToMediaFolder = () => {
console.log("copy file start");
const command = `copy '.\\${directory}\\${directory}.mp3' C:\\Users\\iluha\\temp\\Ilya\\bookDownloader\\media`;
exec(command, (error) => {
if (error) {
console.log("error copy file to media folder", error);
return;
}
console.log("file copied to media", directory);
return;
});
};
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory);
}
const names = [];
console.log("star downloading", directory);
const bookPromises = urls.map((url) => {
return axios.get(url, {
responseType: "arraybuffer",
});
});
// Wait for all requests to complete
Promise.all(bookPromises)
.then((responses) => {
responses.forEach(({ data }, index) => {
const filename = decodeURIComponent(urls[index])
.split("/")
.at(-1)
.split("?")[0]
.replaceAll(" ", "_")
.replaceAll(",", "")
.replace("!", "");
// depending on audio file provider we need to add .mp3
const path = `${directory}/${filename}`;
fs.writeFileSync(path, data);
console.log(`Downloaded ${filename} successfully.`);
names.push(`file ../${path}`);
});
// Now that all requests are done, call writePathFile
writePathFile(names);
})
.catch((err) => {
console.error("An error occurred while downloading files:", err);
});