-
Notifications
You must be signed in to change notification settings - Fork 25
/
app.js
178 lines (151 loc) · 4.27 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
const fs = require("fs");
const ProgressBar = require("progress");
const axios = require("axios");
const NodeID3 = require("node-id3");
const prompt = require("prompt");
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,
songImageUrl,
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",
});
const filepath = `./songs/${songTitleFound}.mp3`;
//for progress bar...
const totalLength = headers["content-length"];
const progressBar = new ProgressBar(
"-> downloading [:bar] :percent :etas",
{
width: 40,
complete: "=",
incomplete: " ",
renderThrottle: 1,
total: parseInt(totalLength),
}
);
data.on("data", (chunk) => progressBar.tick(chunk.length));
data.on("end", async () => {
singerName = singerName.replace(/\s{2,10}/g, "");
console.log("Song Downloaded!");
let imageFilePath = `./songs/${songTitleFound}.jpg`;
await downloadImage(songImageUrl, imageFilePath);
const tags = {
title: songName,
artist: singerName,
APIC: imageFilePath,
};
NodeID3.write(tags, filepath);
console.log("WRITTEN TAGS");
try {
fs.unlinkSync(imageFilePath);
} catch (err) {
console.error(err);
}
startNextSong();
});
//for saving in file...
data.pipe(fs.createWriteStream(filepath));
} 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, songImageUrl, songDurationSec } =
songList[index];
const res = await getDownloadLink(songName, singerName, songDurationSec);
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,
songImageUrl,
songDownloadUrl,
songTitleFound
);
} else {
console.log(
`\n(${index + 1}/${totalSongs}) - [ SONG NOT FOUND ] : ${songName}`
);
notFound.push(`${songName} - ${singerName}`);
startNextSong();
}
};
const start = async () => {
prompt.start();
const { Playlist_URL } = await prompt.get(["Playlist_URL"]);
// "https://music.apple.com/fi/playlist/one-direction-essentials/pl.134ef3b46d32414e9b4b5a995a2f3ea7";
try {
const res = await getPlaylist(Playlist_URL);
console.log("Playlist Name: ", res.playlist);
console.log("User Name: ", res.user);
console.log("Total songs: ", res.songs.length);
songList = res.songs;
totalSongs = res.songs.length;
//create folder
let dir = "./songs";
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
startNextSong();
} catch (err) {
console.log(err);
}
};
start();