drive-mediafire-downloader
Version:
Librería para descargar archivos de Google Drive y Mediafire
150 lines (133 loc) • 5.03 kB
JavaScript
const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
const fs = require('fs');
const { URL } = require('url');
let mainWindow;
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
},
show: false
});
mainWindow.loadFile('index.html');
return mainWindow;
}
app.whenReady().then(() => {
mainWindow = createWindow();
const args = process.argv.slice(2);
if (args.length > 0) {
const downloadLink = args [0];
console.log('Enlace proporcionado:', downloadLink);
if (downloadLink.includes('drive.google.com')) {
handleGoogleDriveWarning(downloadLink);
} else if (downloadLink.includes('mediafire.com')) {
handleMediafireDownload(downloadLink);
} else {
console.log('El enlace proporcionado no es de Google Drive ni Mediafire.');
if (mainWindow && mainWindow.webContents) {
mainWindow.webContents.send('download-status', 'El enlace proporcionado no es compatible.');
}
}
} else {
console.log('No se proporcionó ningún enlace.');
if (mainWindow && mainWindow.webContents) {
mainWindow.webContents.send('download-status', 'No se proporcionó ningún enlace.');
}
}
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
async function handleGoogleDriveWarning(driveLink) {
mainWindow.loadURL(driveLink);
mainWindow.webContents.on('did-finish-load', async () => {
console.log('Página de Google Drive cargada.');
const script = `
const buttons = document.querySelectorAll('input.jfk-button-action');
for (const button of buttons) {
if (button.value === 'Descargar de todos modos') {
button.click();
break;
}
}
`;
try {
await mainWindow.webContents.executeJavaScript(script);
console.log('Se intentó hacer clic en el botón de descarga de Google Drive.');
} catch (error) {
console.error('Error al intentar hacer clic en el botón de Google Drive:', error);
if (mainWindow && mainWindow.webContents) {
mainWindow.webContents.send('download-status', `Error al interactuar con la página de descarga de Google Drive.`);
}
}
});
}
async function handleMediafireDownload(mediafireLink) {
mainWindow.loadURL(mediafireLink);
mainWindow.webContents.on('did-finish-load', async () => {
console.log('Página de Mediafire cargada.');
const script = `
const downloadButton = document.querySelector('a#downloadButton');
if (downloadButton) {
downloadButton.click();
} else {
console.log('No se encontró el botón de descarga en Mediafire.');
if (mainWindow && mainWindow.webContents) {
mainWindow.webContents.send('download-status', 'No se encontró el botón de descarga en Mediafire.');
}
}
`;
try {
await mainWindow.webContents.executeJavaScript(script);
console.log('Se intentó hacer clic en el botón de descarga de Mediafire.');
} catch (error) {
console.error('Error al intentar hacer clic en el botón de Mediafire:', error);
if (mainWindow && mainWindow.webContents) {
mainWindow.webContents.send('download-status', `Error al interactuar con la página de descarga de Mediafire.`);
}
}
});
}
// Escuchar el evento 'will-download' para ambos tipos de descarga
app.on('will-download', (event, item, webContents) => {
const suggestedFilename = item.getFilename();
const downloadPath = path.join(app.getPath('downloads'), suggestedFilename);
event.preventDefault();
item.setSavePath(downloadPath);
item.on('updated', (event, state) => {
if (state === 'progressing') {
const received = item.getReceivedBytes();
const total = item.getTotalBytes();
const progress = total > 0 ? (received / total) * 100 : 0;
console.log(`Descargando: ${progress.toFixed(2)}%`);
if (mainWindow && mainWindow.webContents) {
mainWindow.webContents.send('download-status', `Descargando: ${progress.toFixed(2)}%`);
}
}
});
item.once('done', (event, state) => {
if (state === 'completed') {
console.log('Descarga completada en:', downloadPath);
if (mainWindow && mainWindow.webContents) {
mainWindow.webContents.send('download-status', `Descarga completada en: ${downloadPath}`);
}
} else {
console.log(`Descarga fallida: ${state}`);
if (mainWindow && mainWindow.webContents) {
mainWindow.webContents.send('download-status', `Descarga fallida: ${state}`);
}
}
});
item.resume();
});