Skip to content

Commit d99e235

Browse files
committed
friend is an enemy in disguise
1 parent c70b6fb commit d99e235

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

backend/LiSA.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
from multiprocessing import Pipe, Manager, freeze_support
1010
from video.downloader import DownloadManager
1111
from video.library import Library
12-
from pathlib import Path
1312

1413

1514
def run_api_server(port: int = 8000):
1615
ServerConfig.API_SERVER_ADDRESS = f"http://localhost:{port}"
17-
print(f"server started on port: {port} \n You can access API SERVER on {ServerConfig.API_SERVER_ADDRESS}")
16+
logging.info(f"server started on port: {port} \n You can access API SERVER on {ServerConfig.API_SERVER_ADDRESS}")
1817
start_api_server(port=port)
1918

2019

@@ -33,7 +32,7 @@ def get_ports():
3332
if __name__ == "__main__":
3433
freeze_support()
3534
try:
36-
logging.basicConfig(filename=Path(__file__).parent.joinpath("LiSA.log"), filemode="w", level=logging.ERROR, format="%(name)s → %(levelname)s: %(message)s")
35+
logging.basicConfig(level=logging.ERROR)
3736
DB.migrate() # migrate the database
3837
DB() # initialize the highest id
3938

backend/video/downloader/downloader.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import sys
4+
35
import aiohttp
46
import asyncio
57
import m3u8
@@ -393,8 +395,13 @@ def _merge_segments(self, input_file: str | Path, output_file: str | Path = None
393395
if not output_file:
394396
output_file = self._output_file
395397

396-
# check if exe present in backend folder else fallback to default option
397-
ffmpeg_loc = get_path("ffmpeg", Path(__file__).parent.parent.parent.parent.joinpath("./ffmpeg"))
398+
# check if ffmpeg binary is present in system path, else fallback to the binary in backend folder
399+
if getattr(sys, 'frozen', False):
400+
application_path = sys._MEIPASS
401+
else:
402+
application_path = Path(__file__).parent.parent.parent.parent
403+
404+
ffmpeg_loc = get_path("ffmpeg", Path(application_path).joinpath("ffmpeg"))
398405

399406
cmd = [
400407
ffmpeg_loc,

main.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const { spawn } = require('child_process');
22
const { app, BrowserWindow, ipcMain, shell } = require('electron');
33
const isDevMode = require('electron-is-dev');
44
const path = require('path');
5+
const fs = require('fs');
56

67
let pythonServer;
78

@@ -27,21 +28,34 @@ function startPythonServer() {
2728
return;
2829
}
2930

31+
const logPath = path.join(path.dirname(process.execPath), 'LiSA.log');
32+
fs.writeFileSync(logPath, '', { encoding: 'utf8' }); // clear logs
33+
3034
pythonServer = spawn(cmd, {
3135
shell: true,
32-
detached: isDevMode,
36+
detached: false,
37+
stdio: ['ignore', 'pipe', 'pipe']
38+
});
39+
40+
const logStream = fs.createWriteStream(logPath, { flags: 'a' });
41+
pythonServer.stdout.pipe(logStream);
42+
pythonServer.stderr.pipe(logStream);
43+
44+
pythonServer.on('close', (_) => {
45+
logStream.end();
3346
});
3447
}
3548

49+
3650
function killPythonServer() {
3751
if (!pythonServer) return;
3852

3953
if (process.platform === 'win32') {
40-
const killCmd = `taskkill /pid ${pythonServer.pid} /f /t`;
54+
const killCmd = `tskill LiSA`;
4155
spawn('cmd.exe', ['/c', killCmd]);
42-
} else pythonServer.kill('SIGINT');
43-
44-
console.log('Killed python server, PID: ', pythonServer.pid);
56+
} else {
57+
process.kill(-pythonServer.pid, 'SIGINT');
58+
}
4559

4660
pythonServer = null;
4761
}
@@ -191,7 +205,7 @@ app.whenReady().then(async () => {
191205

192206
app.on('activate', () => {
193207
/**
194-
* On macOS it's common to re-create a window in the app when the
208+
* On macOS, it's common to re-create a window in the app when the
195209
* dock icon is clicked and there are no other windows open.
196210
*/
197211
if (BrowserWindow.getAllWindows().length === 0) createWindow();
@@ -206,8 +220,7 @@ app.on('window-all-closed', () => {
206220
if (process.platform !== 'darwin') app.quit();
207221
});
208222

209-
app.on('quit', function () {
210-
// Clean up the Python server when the app quits
223+
app.on('quit', (event) => {
211224
killPythonServer();
212225
});
213226

0 commit comments

Comments
 (0)