Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aandrew-me
GitHub Repository: aandrew-me/ytDownloader
Path: blob/main/src/playlist_new.js
448 views
1
const { clipboard, shell, ipcRenderer } = require("electron");
2
const { default: YTDlpWrap } = require("yt-dlp-wrap-plus");
3
const path = require("path");
4
const os = require("os");
5
const fs = require("fs");
6
const { execSync, exec, spawnSync } = require("child_process");
7
let url;
8
const ytDlp = localStorage.getItem("ytdlp");
9
const ytdlp = new YTDlpWrap(ytDlp);
10
const downloadDir = localStorage.getItem("downloadPath");
11
const i18n = new (require("../translations/i18n"))();
12
let cookieArg = "";
13
let browser = "";
14
const formats = {
15
144: 160,
16
240: 133,
17
360: 134,
18
480: 135,
19
720: 136,
20
1080: 137,
21
1440: 400,
22
2160: 401,
23
4320: 571,
24
};
25
let originalCount = 0;
26
let ffmpeg;
27
let ffmpegPath;
28
if (os.platform() === "win32") {
29
ffmpeg = `"${__dirname}\\..\\ffmpeg.exe"`;
30
ffmpegPath = `${__dirname}\\..\\ffmpeg.exe`;
31
} else {
32
ffmpeg = `"${__dirname}/../ffmpeg"`;
33
ffmpegPath = `${__dirname}/../ffmpeg`;
34
}
35
36
if (!fs.existsSync(ffmpegPath)) {
37
try {
38
ffmpeg = execSync("which ffmpeg", { encoding: "utf8" });
39
ffmpeg = `"${ffmpeg.trimEnd()}"`;
40
} catch (error) {
41
console.log(error);
42
}
43
}
44
console.log("ffmpeg:", ffmpeg);
45
46
let foldernameFormat = "%(playlist_title)s";
47
let filenameFormat = "%(playlist_index)s.%(title)s.%(ext)s";
48
let playlistIndex = 1;
49
let playlistEnd = "";
50
51
function getId(id) {
52
return document.getElementById(id);
53
}
54
55
function pasteLink() {
56
const clipboardText = clipboard.readText();
57
getId("loadingWrapper").style.display = "flex";
58
getId("incorrectMsg").textContent = "";
59
getId("errorBtn").style.display = "none";
60
getId("errorDetails").style.display = "none";
61
getId("errorDetails").textContent = "";
62
exec(
63
`${ytDlp} --yes-playlist --no-warnings -J --flat-playlist "${clipboardText}"`,
64
(error, stdout, stderr) => {
65
if (error) {
66
getId("loadingWrapper").style.display = "none";
67
getId("incorrectMsg").textContent = i18n.__(
68
"Some error has occurred. Check your network and use correct URL"
69
);
70
getId("errorDetails").innerHTML = `
71
<strong>URL: ${clipboardText}</strong>
72
<br><br>
73
${error}
74
`;
75
getId("errorDetails").title = i18n.__("Click to copy");
76
getId("errorBtn").style.display = "inline-block";
77
} else {
78
const parsed = JSON.parse(stdout);
79
console.log(parsed);
80
let items = "";
81
// If correct playlist url is used
82
if (parsed.entries) {
83
parsed.entries.forEach((entry) => {
84
console.log(entry)
85
const randId = Math.random()
86
.toFixed(10)
87
.toString()
88
.slice(2);
89
90
if (entry.channel) {
91
items += `
92
<div class="item" id="${randId}">
93
<img src="${
94
entry.thumbnails[3].url
95
}" alt="No thumbnail" class="itemIcon" crossorigin="anonymous">
96
97
<div class="itemBody">
98
<div class="itemTitle">${entry.title}</div>
99
<div>${formatTime(entry.duration)}</div>
100
<input type="checkbox" class="playlistCheck" id="c${randId}">
101
<input type="hidden" id="link${randId}" value="${entry.url}">
102
</div>
103
</div>
104
`;
105
}
106
});
107
getId("data").innerHTML = items;
108
getId("loadingWrapper").style.display = "none";
109
}
110
// If correct playlist url is not used
111
else {
112
getId("loadingWrapper").style.display = "none";
113
getId("incorrectMsg").textContent = i18n.__(
114
"Incompatible URL. Please provide a playlist URL"
115
);
116
getId("errorDetails").innerHTML = `
117
<strong>URL: ${clipboardText}</strong>
118
<br><br>
119
${error}
120
`;
121
getId("errorDetails").title = i18n.__("Click to copy");
122
getId("errorBtn").style.display = "inline-block";
123
}
124
}
125
}
126
);
127
}
128
129
getId("pasteLink").addEventListener("click", (e) => {
130
pasteLink();
131
});
132
133
document.addEventListener("keydown", (event) => {
134
if (event.ctrlKey && event.key == "v") {
135
pasteLink();
136
}
137
});
138
139
function formatTime(seconds) {
140
let hours = Math.floor(seconds / 3600);
141
let minutes = Math.floor((seconds - hours * 3600) / 60);
142
seconds = seconds - hours * 3600 - minutes * 60;
143
let formattedTime = "";
144
145
if (hours > 0) {
146
formattedTime += hours + ":";
147
}
148
if (minutes < 10 && hours > 0) {
149
formattedTime += "0";
150
}
151
formattedTime += minutes + ":";
152
if (seconds < 10) {
153
formattedTime += "0";
154
}
155
formattedTime += seconds;
156
return formattedTime;
157
}
158
function closeMenu() {
159
getId("menuIcon").style.transform = "rotate(0deg)";
160
menuIsOpen = false;
161
let count = 0;
162
let opacity = 1;
163
const fade = setInterval(() => {
164
if (count >= 10) {
165
clearInterval(fade);
166
} else {
167
opacity -= 0.1;
168
getId("menu").style.opacity = opacity;
169
count++;
170
}
171
}, 50);
172
}
173
174
getId("preferenceWin").addEventListener("click", () => {
175
closeMenu();
176
ipcRenderer.send("load-page", __dirname + "/preferences.html");
177
});
178
179
getId("aboutWin").addEventListener("click", () => {
180
closeMenu();
181
ipcRenderer.send("load-page", __dirname + "/about.html");
182
});
183
184
getId("historyWin").addEventListener("click", () => {
185
closeMenu();
186
ipcRenderer.send("load-page", __dirname + "/history.html");
187
});
188
189
getId("homeWin").addEventListener("click", () => {
190
closeMenu();
191
ipcRenderer.send("load-win", __dirname + "/index.html");
192
});
193
194