-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
194 lines (166 loc) · 5.1 KB
/
app.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const fs = require("fs");
const ProgressBar = require("progress");
const axios = require("axios");
const prompt = require("prompt");
const NodeID3 = require("node-id3");
const youtubedl = require("youtube-dl-exec");
const { getPlaylist } = require("./src/getPlaylist");
const { getDownloadLink } = require("./src/getDownloadLink");
let index = -1;
let songList = [];
let totalSongs = 0;
let notFound = [];
const downloadSong = async (
songName,
singerName,
songDownloadUrl,
songTitleFound
) => {
try {
let numb = index + 1;
console.log(`\n(${numb}/${totalSongs}) Starting download: ${songName}`);
const { data, headers } = await axios({
method: "GET",
url: songDownloadUrl,
responseType: "stream",
});
var bar = new ProgressBar("[:bar] :percent :etas ", {
total: 45,
});
var timer = setInterval(function () {
bar.tick();
if (bar.complete) {
console.log(`\n ${songTitleFound} - Downloaded\n`);
clearInterval(timer);
}
}, 100);
data.on("end", async () => {
singerName = singerName.replace(/\s{2,10}/g, "");
console.log("Song Downloaded!");
startNextSong();
});
//for saving in file...
await youtubedl(songDownloadUrl, {
format: "m4a",
output: "./songs/" + songTitleFound + ".mp3",
maxFilesize: "104857600",
preferFreeFormats: true,
});
startNextSong();
} catch (err) {
console.log("Error:", err);
startNextSong();
}
};
// const downloadImage = async (songImageUrl, imageFilePath) => {
// try {
// const response = await axios({
// method: "GET",
// url: songImageUrl,
// responseType: "stream",
// });
// // Create a write stream to save the image
// const writer = fs.createWriteStream(imageFilePath);
// // Pipe the response data into the writer stream
// response.data.pipe(writer);
// // Wait for the image to finish downloading
// await new Promise((resolve, reject) => {
// writer.on("finish", resolve);
// writer.on("error", reject);
// });
// console.log("Image downloaded!");
// } catch (error) {
// console.error("Error downloading image:", error.message);
// }
// };
const startNextSong = async () => {
index += 1;
if (index === totalSongs) {
console.log("\n#### ALL SONGS ARE DOWNLOADED!! ####\n");
console.log("Songs that are not found:-");
let i = 1;
for (let song of notFound) {
console.log(`${i} - ${song}`);
i += 1;
}
if (i === 1) console.log("None!");
return;
}
const { songName, singerName, songDurationSec } = songList[index];
const res = await getDownloadLink(songName, singerName);
if (res) {
const { songDownloadUrl, songTitleFound } = res;
if (fs.existsSync(`./songs/${songTitleFound}.mp3`)) {
console.log(
`\n(${
index + 1
}/${totalSongs}) - [ SONG ALREADY PRESENT ] : ${songName}`
);
startNextSong(); //next song
return;
}
await downloadSong(
songName,
singerName,
songDownloadUrl,
songTitleFound,
songDurationSec
);
} else {
console.log(
`\n(${index + 1}/${totalSongs}) - [ SONG NOT FOUND ] : ${songName}`
);
notFound.push(`${songName} - ${singerName}`);
startNextSong();
}
};
const start = async () => {
try {
let songPlaylistObj;
const platlistFileName = "playlist-info.txt";
console.log("Checking if file playlist-info.txt exist locally!");
if (fs.existsSync(`./${platlistFileName}`)) {
console.log(
"File playlist-info.txt does exist. Reading data.. [if there is any changes in playlist, delete the file playlist-info.txt and execute code again]"
);
const data = fs.readFileSync(platlistFileName, {
encoding: "utf8",
flag: "r",
});
songPlaylistObj = JSON.parse(data);
} else {
console.log("File playlist-info.txt does not exist.");
prompt.start();
const { Playlist_URL } = await prompt.get(["Playlist_URL"]);
// "https://open.spotify.com/playlist/6erqXmUhndc9DmQBMsImyY?si=tdOMvOQdR6KAZy9916kXcg&utm_source=copy-link&dl_branch=1&nd=1";
songPlaylistObj = await getPlaylist(Playlist_URL);
songPlaylistObj.url = Playlist_URL; //saving playlist url also
console.log(
"Saving data in playlist-info.txt file. [Next time, data will be directly read from this file]"
);
fs.writeFileSync(
platlistFileName,
JSON.stringify(songPlaylistObj),
(err) => {
if (err) throw err;
console.log("Saved data in playlist-info.txt file.");
}
);
}
console.log("\nPlaylist URL: ", songPlaylistObj.url);
console.log("Playlist Name: ", songPlaylistObj.playlist);
console.log("User Name: ", songPlaylistObj.user);
console.log("Total songs: ", songPlaylistObj.songs.length);
songList = songPlaylistObj.songs;
totalSongs = songPlaylistObj.songs.length;
//create folder
let dir = "./songs";
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
startNextSong();
} catch (err) {
console.log(err);
}
};
start();