-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
231 lines (195 loc) · 7.04 KB
/
index.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const { app, BrowserWindow, ipcMain, shell } = require('electron');
const fs = require('fs');
const path = require('path');
const youtubedl = require('youtube-dl-exec');
const downloadsFolder = path.join(__dirname, 'karaoke'); // Pasta para downloads
// Cria a pasta se não existir
if (!fs.existsSync(downloadsFolder)) {
fs.mkdirSync(downloadsFolder, { recursive: true });
}
let scriptInjected = false;
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
icon: path.join(__dirname, 'mic.jpg'),
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js'),
},
show: false,
});
win.loadFile('index.html');
const executeScript = () => {
if (scriptInjected) return; // Evita reinjetar o script
const commandScript = fs.readFileSync(path.join(__dirname, 'comand.js'), 'utf-8');
win.webContents.executeJavaScript(`${commandScript}`);
scriptInjected = true; // Marca o script como injetado
win.show();
};
win.webContents.on('did-finish-load', executeScript);
win.webContents.on('did-navigate', executeScript);
ipcMain.on('download-video', (event, url) => {
console.log('Iniciando download para URL:', url);
// Use o youtube-dl-exec para pegar os metadados, incluindo o título
youtubedl(url, {
dumpSingleJson: true
}).then(output => {
setTimeout(() => {
const title = output.title;
downloadVideo(url, downloadsFolder, `${title}.mp4`);
event.reply('download-complete', { title, path: downloadsFolder });
}, 2000)
}).catch(err => {
event.reply('download-error', { error: err.message });
});
});
ipcMain.on('downloads-list', (event) => {
// Verifica se a pasta existe
if (fs.existsSync(downloadsFolder)) {
// Lê todos os arquivos da pasta
fs.readdir(downloadsFolder, (err, files) => {
if (err) {
console.error('Erro ao ler a pasta de downloads:', err);
event.reply('downloads-list-error', { error: err.message });
return;
}
// Filtra os arquivos para pegar apenas os .mp4
const videoFiles = files.filter(file => file.endsWith('.mp4')).map(file => {
return {
title: path.parse(file).name, // Extrai o nome do arquivo sem a extensão
path: path.join(downloadsFolder, file)
};
});
// Envia a lista de vídeos para o frontend
event.reply('downloads-list', videoFiles);
});
} else {
console.error('A pasta de downloads não existe!');
event.reply('downloads-list-error', { error: 'Pasta de downloads não encontrada' });
}
});
ipcMain.on('open-downloads', () => {
const downloadsWindow = new BrowserWindow({
width: 800,
height: 600,
icon: path.join(__dirname, 'mic.jpg'),
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});
downloadsWindow.loadFile('downloads.html');
downloadsWindow.webContents.on('did-finish-load', () => {
downloadsWindow.webContents.send('downloads-list', downloads);
});
});
ipcMain.handle('open-file', async (event, filePath) => {
try {
await shell.openPath(filePath);
} catch (error) {
console.error('Erro ao abrir o arquivo:', error);
}
});
ipcMain.on('check-online', (event) => {
const isOnline = require('dns').resolve('www.google.com', (err) => !err);
event.reply('online-status', isOnline);
});
ipcMain.on('openDownloadsPage', () => {
const karaokeFolderPath = path.join(__dirname, 'karaoke');
const downloads = [];
fs.readdir(karaokeFolderPath, (err, files) => {
if (err) {
console.error('Erro ao ler a pasta karaoke:', err);
return;
}
files.forEach(file => {
const fileExtension = path.extname(file).toLowerCase();
if (['.mp4', '.avi', '.mkv', '.mov'].includes(fileExtension)) {
downloads.push({
title: file,
path: path.join(karaokeFolderPath, file)
});
}
});
console.log('Downloads a serem enviados:', downloads); // Verifique o que está sendo enviado
const downloadsWindow = new BrowserWindow({
width: 800,
height: 600,
icon: path.join(__dirname, 'mic.jpg'),
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js') // Caminho para o arquivo preload.js
},
});
downloadsWindow.loadFile('listVideos.html');
downloadsWindow.webContents.on('did-finish-load', () => {
setTimeout(() => {
downloadsWindow.webContents.send('downloads-list', downloads);
}, 100); // Dê um pequeno delay para garantir que a janela foi completamente carregada
});
});
});
ipcMain.on('changeNameVideo', (event, oldName, newName) => {
const oldFilePath = path.join(downloadsFolder, oldName);
const newFilePath = path.join(downloadsFolder, newName);
// Verifica se o novo nome já existe
fs.exists(newFilePath, (exists) => {
if (exists) {
console.log('O arquivo com esse nome já existe!');
event.reply('changeNameVideo-response', { success: false, message: 'Arquivo já existe' });
return;
}
// Renomeia o arquivo
fs.rename(oldFilePath, newFilePath, (err) => {
if (err) {
event.reply('changeNameVideo-response', { success: false, message: 'Erro ao renomear o arquivo' });
return;
}
// Atualiza a lista de downloads (se necessário)
fs.readdir(downloadsFolder, (err, files) => {
if (err) return
const downloads = files.filter(file => ['.mp4', '.avi', '.mkv', '.mov'].includes(path.extname(file).toLowerCase()))
.map(file => ({
title: file,
path: path.join(downloadsFolder, file)
}));
// Envia a lista atualizada para o frontend
event.reply('downloads-list', downloads);
});
event.reply('changeNameVideo-response', { success: true, message: 'Arquivo renomeado com sucesso' });
});
});
});
}
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});
async function downloadVideo(videoUrl, outputFolder, fileName) {
try {
if (!fs.existsSync(outputFolder)) {
fs.mkdirSync(outputFolder, { recursive: true });
console.log(`Pasta "${outputFolder}" criada.`);
}
const outputPath = path.join(outputFolder, fileName);
console.log(`Iniciando o download de: ${videoUrl}`);
await youtubedl(videoUrl, {
output: outputPath,
format: 'mp4',
});
console.log(`Download concluído!`);
} catch (error) {
console.error(`Erro durante o download: ${error.message}`);
}
}