Skip to content

Commit

Permalink
Create logger utility + open logs button
Browse files Browse the repository at this point in the history
  • Loading branch information
xylish7 committed Nov 1, 2019
1 parent d7527a1 commit 9433ce2
Show file tree
Hide file tree
Showing 8 changed files with 748 additions and 665 deletions.
4 changes: 4 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const updater = require("./main/update/updater");
const { killAllProcesses } = require("./main/kill-processes/app-notifications");
const vcredistInstall = require("./main/vcredist_x86/install");
const ytdlUpdater = require("./main/update/ytdl-updater");
const { keepLogs } = require("./utils/logger");

if (process.argv[2] == "dev") {
require("electron-reload")(__dirname);
Expand Down Expand Up @@ -43,6 +44,9 @@ app.on("ready", () => {
// Send window object to download video
dlPlaylist.staticInfo.win = windows.mainWindow;

// Clear old logs
keepLogs(7);

// Show notification if processes are in progress
windows.mainWindow.on("close", e => {
e.preventDefault();
Expand Down
18 changes: 16 additions & 2 deletions main/update/ytdl-updater.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var fs = require("fs");
var path = require("path");
var spawn = require("cross-spawn-async");
const { logger } = require("../../utils/logger");

const ytdlProcessMetadata = {
ytdl_path: path.resolve(
Expand All @@ -24,11 +24,25 @@ const checkForUpdates = event => {
const ytdlProcess = spawn(ytdl_path, args, options);

ytdlProcess.stdout.on("data", ytdlOutput => {
logger(ytdlOutput);

if (ytdlOutput.toString().includes("Updating"))
event.sender.send("update-ytdl");
if (ytdlOutput.toString().includes("Updated"))
event.sender.send("ytdl-update-finished");
if (ytdlOutput.toString().includes("Waiting for file"))
event.sender.send("ytdl-update-finished");
});

ytdlProcess.stderr.on("data", ytdlError => {
logger(ytdlError);

event.sender.send("ytdl-update-finished");
});

ytdlProcess.on("exit", () => {
ytdlProcess.on("exit", code => {
logger(`yt-dl updater exit code: ${code}`);

event.sender.send("ytdl-update-finished");
});
};
Expand Down
156 changes: 73 additions & 83 deletions main/ytdl/download-playlist.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// Modules
const ytdl = require('youtube-dl')
const fs = require('fs')
const {ipcMain, app} = require('electron')
const ytdl = require("youtube-dl");
const fs = require("fs");
const { ipcMain, app } = require("electron");

// Internam modules
const mp3converter = require('../conversion/mp3Converter')
const {killAllProcesses} = require('../kill-processes/app-notifications')


const mp3converter = require("../conversion/mp3Converter");
const { killAllProcesses } = require("../kill-processes/app-notifications");
const { logger } = require("../../utils/logger");

exports.staticInfo = {
win: false,
Expand All @@ -16,145 +15,136 @@ exports.staticInfo = {
appendColumns: true,
isPlaylist: false,
keepFiles: false
}
};

exports.ipcEvent = {}
exports.ipcEvent = {};

var options = {
maxBuffer: Infinity
}
var video, stream
};
var video, stream;

var getArgs = () => {
var args = [
'-f', `${this.ipcEvent.downloadInfo.video_quality}[ext=${this.ipcEvent.downloadInfo.video_format}]`
]

return args
}
"-f",
`${this.ipcEvent.downloadInfo.video_quality}[ext=${this.ipcEvent.downloadInfo.video_format}]`
];

exports.playlist = (url) => {
return args;
};

var dynamicInfo = {}
video = ytdl(url, getArgs(), options)
exports.playlist = url => {
var dynamicInfo = {};
video = ytdl(url, getArgs(), options);

// Catch error
video.on('error', (err) => {
this.ipcEvent.event.sender.send('ytdl-errors', err)

// Create directory for logs if it doesn't exists
var dir = `${app.getPath('userData')}/logs`
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
video.on("error", err => {
this.ipcEvent.event.sender.send("ytdl-errors", err);

// Create log file
var error_template = `---------------------------\n${new Date().toLocaleString()}\n---------------------------\n\n${err}\n\n`
fs.appendFile(`${app.getPath('userData')}/logs/ytdl-errors.txt`, error_template, (error) => {
if (error) throw error;
})
logger(err);
});

// Get video information
video.on('info', (info) => {

this.staticInfo.keepFiles = this.ipcEvent.downloadInfo.keepFilesCheckbox
this.staticInfo.isPlaylist = info.playlist_index
video.on("info", info => {
this.staticInfo.keepFiles = this.ipcEvent.downloadInfo.keepFilesCheckbox;
this.staticInfo.isPlaylist = info.playlist_index;
if (this.staticInfo.isPlaylist != null) {
if (!this.staticInfo.informationExtracted) {
this.staticInfo.informationExtracted = true
this.staticInfo.informationExtracted = true;
// Get number of videos in playlist
this.staticInfo.n_entries = info.n_entries
this.staticInfo.n_entries = info.n_entries;
}
}

// Get video index in playlist
dynamicInfo = {
dynamicInfo = {
playlist_index: info.playlist_index,
_raw_duration: info._duration_raw,
title: info.title.replace(/[\\/|":*?<>']/g, ""),
size: info.size,
}
size: info.size
};

// Save path
var outputFile = `${this.ipcEvent.downloadInfo.savePath}\\${dynamicInfo.title}.${this.ipcEvent.downloadInfo.video_format}`
var outputFile = `${this.ipcEvent.downloadInfo.savePath}\\${dynamicInfo.title}.${this.ipcEvent.downloadInfo.video_format}`;

// Start writing video in directory
stream = fs.createWriteStream(outputFile)
stream = fs.createWriteStream(outputFile);
video.pipe(stream);
// Send the information gathered until now
this.ipcEvent.event.sender.send('playlist-progress', {
this.ipcEvent.event.sender.send("playlist-progress", {
static: this.staticInfo,
dynamic: dynamicInfo
})

this.staticInfo.appendColumns = false
});

this.staticInfo.appendColumns = false;
});

// Calculate the progress of download
var position = 0
video.on('data', (chunk) =>{
var position = 0;
video.on("data", chunk => {
position += chunk.length;

if (dynamicInfo.size) {
dynamicInfo.percent = (position / dynamicInfo.size * 100).toFixed(2);
dynamicInfo.percent = ((position / dynamicInfo.size) * 100).toFixed(2);

this.ipcEvent.event.sender.send('playlist-progress', {
this.ipcEvent.event.sender.send("playlist-progress", {
static: this.staticInfo,
dynamic: dynamicInfo
})
});
}
});

// Executed when video it's downloaded
video.on('end', () => {
video.on("end", () => {
// Send event if download finished
var staticInfo = this.staticInfo
var staticInfo = this.staticInfo;

// Send data for video
// Send data for video
if (staticInfo.isPlaylist == null) {
staticInfo.downloadFinished = true
this.ipcEvent.event.sender.send('playlist-progress', {
staticInfo.downloadFinished = true;
this.ipcEvent.event.sender.send("playlist-progress", {
static: staticInfo,
dynamic: dynamicInfo
})
});
}

// Send data for playlist
if (staticInfo.n_entries == dynamicInfo.playlist_index) {
staticInfo.downloadFinished = true
this.ipcEvent.event.sender.send('playlist-progress', {
staticInfo.downloadFinished = true;
this.ipcEvent.event.sender.send("playlist-progress", {
static: staticInfo,
dynamic: dynamicInfo
})
});
}
// Parameter used to append new progress bar for every video
staticInfo.appendColumns = true
staticInfo.appendColumns = true;

// Convert video
if (this.ipcEvent.downloadInfo.mp3Conversion == 'true') {
if (this.ipcEvent.downloadInfo.mp3Conversion == "true") {
var static = staticInfo,
dynamic = dynamicInfo
mp3converter.convertVideo({static, dynamic}, this.ipcEvent.downloadInfo)
dynamic = dynamicInfo;
mp3converter.convertVideo(
{ static, dynamic },
this.ipcEvent.downloadInfo
);
}
});

// Download next video
video.on('next', this.playlist);
}

ipcMain.on('stop-download', (event) => {
stream.end()
video.unresolve()
killAllProcesses()
this.staticInfo.appendColumns = true
this.staticInfo.downloadFinished = false
this.staticInfo.informationExtracted = false
event.sender.send('response')
})
video.on("next", this.playlist);
};

ipcMain.on("stop-download", event => {
stream.end();
video.unresolve();
killAllProcesses();
this.staticInfo.appendColumns = true;
this.staticInfo.downloadFinished = false;
this.staticInfo.informationExtracted = false;
event.sender.send("response");
});

exports.stopOnClose = () => {
if (stream) stream.end()
video.unresolve()
}
if (stream) stream.end();
video.unresolve();
};
Loading

0 comments on commit 9433ce2

Please sign in to comment.