Code refactoring
This commit is contained in:
@@ -4,6 +4,7 @@ import { Readable } from 'stream';
|
||||
import { ISettings } from '../interfaces/ISettings';
|
||||
import { availableParams } from '../utils/params';
|
||||
import Logger from '../utils/BetterLogger';
|
||||
import { IDownloadFormat, IDownloadMetadata } from '../interfaces/IDownloadMetadata';
|
||||
|
||||
const log = Logger.instance;
|
||||
|
||||
@@ -20,7 +21,7 @@ class Process {
|
||||
private settings: ISettings;
|
||||
private stdout: Readable;
|
||||
private pid: number;
|
||||
private metadata?: IDownloadInfo;
|
||||
private metadata?: IDownloadMetadata;
|
||||
private exePath = join(__dirname, 'yt-dlp');
|
||||
|
||||
constructor(url: string, params: Array<string>, settings: any) {
|
||||
@@ -59,7 +60,7 @@ class Process {
|
||||
* function used internally by the download process to fetch information, usually thumbnail and title
|
||||
* @returns Promise to the lock
|
||||
*/
|
||||
public getInfo(): Promise<IDownloadInfo> {
|
||||
public getMetadata(): Promise<IDownloadMetadata> {
|
||||
if (!this.metadata) {
|
||||
let stdoutChunks = [];
|
||||
const ytdlpInfo = spawn(this.exePath, ['-j', this.url]);
|
||||
@@ -74,7 +75,7 @@ class Process {
|
||||
const buffer = Buffer.concat(stdoutChunks);
|
||||
const json = JSON.parse(buffer.toString());
|
||||
const info = {
|
||||
formats: json.formats.map((format: IDownloadInfoSection) => {
|
||||
formats: json.formats.map((format: IDownloadFormat) => {
|
||||
return {
|
||||
format_id: format.format_id ?? '',
|
||||
format_note: format.format_note ?? '',
|
||||
@@ -83,7 +84,7 @@ class Process {
|
||||
vcodec: format.vcodec ?? '',
|
||||
acodec: format.acodec ?? '',
|
||||
}
|
||||
}).filter((format: IDownloadInfoSection) => format.format_note !== 'storyboard'),
|
||||
}).filter((format: IDownloadFormat) => format.format_note !== 'storyboard'),
|
||||
best: {
|
||||
format_id: json.format_id ?? '',
|
||||
format_note: json.format_note ?? '',
|
||||
|
||||
@@ -27,9 +27,9 @@ catch (e) {
|
||||
* @param socket
|
||||
* @param url
|
||||
*/
|
||||
export async function getFormatsAndInfo(socket: Socket, url: string) {
|
||||
export async function getFormatsAndMetadata(socket: Socket, url: string) {
|
||||
let p = new Process(url, [], settings);
|
||||
const formats = await p.getInfo();
|
||||
const formats = await p.getMetadata();
|
||||
socket.emit('available-formats', formats)
|
||||
p = null;
|
||||
}
|
||||
@@ -56,7 +56,7 @@ export async function download(socket: Socket, payload: IPayload) {
|
||||
|
||||
p.start().then(downloader => {
|
||||
mem_db.add(downloader)
|
||||
displayDownloadInfo(downloader, socket);
|
||||
displayDownloadMetadata(downloader, socket);
|
||||
streamProcess(downloader, socket);
|
||||
});
|
||||
}
|
||||
@@ -66,11 +66,11 @@ export async function download(socket: Socket, payload: IPayload) {
|
||||
* @param process
|
||||
* @param socket
|
||||
*/
|
||||
function displayDownloadInfo(process: Process, socket: Socket) {
|
||||
process.getInfo().then(info => {
|
||||
socket.emit('info', {
|
||||
function displayDownloadMetadata(process: Process, socket: Socket) {
|
||||
process.getMetadata().then(metadata => {
|
||||
socket.emit('metadata', {
|
||||
pid: process.getPid(),
|
||||
info: info
|
||||
metadata: metadata,
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -87,11 +87,8 @@ function streamProcess(process: Process, socket: Socket) {
|
||||
pid: process.getPid(),
|
||||
});
|
||||
}
|
||||
const stdout = process.getStdout()
|
||||
|
||||
stdout.removeAllListeners()
|
||||
|
||||
from(stdout) // stdout as observable
|
||||
from(process.getStdout().removeAllListeners()) // stdout as observable
|
||||
.pipe(
|
||||
throttle(() => interval(500)), // discard events closer than 500ms
|
||||
map(stdout => formatter(String(stdout), process.getPid()))
|
||||
@@ -148,7 +145,7 @@ export async function retrieveDownload(socket: Socket) {
|
||||
// resume the jobs
|
||||
for (const entry of it) {
|
||||
const [, process] = entry
|
||||
displayDownloadInfo(process, socket);
|
||||
displayDownloadMetadata(process, socket);
|
||||
streamProcess(process, socket);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
interface IDownloadInfo {
|
||||
formats: Array<IDownloadInfoSection>,
|
||||
best: IDownloadInfoSection,
|
||||
export interface IDownloadMetadata {
|
||||
formats: Array<IDownloadFormat>,
|
||||
best: IDownloadFormat,
|
||||
thumbnail: string,
|
||||
title: string,
|
||||
}
|
||||
|
||||
interface IDownloadInfoSection {
|
||||
export interface IDownloadFormat {
|
||||
format_id: string,
|
||||
format_note: string,
|
||||
fps: number,
|
||||
@@ -1,4 +1,5 @@
|
||||
export interface ISettings {
|
||||
download_path: string,
|
||||
cliArgs?: string[],
|
||||
port?: number,
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import { splash } from './utils/logger';
|
||||
import { join } from 'path';
|
||||
import { Server } from 'socket.io';
|
||||
import { ytdlpUpdater } from './utils/updater';
|
||||
import { download, abortDownload, retrieveDownload, abortAllDownloads, getFormatsAndInfo } from './core/downloader';
|
||||
import { download, abortDownload, retrieveDownload, abortAllDownloads, getFormatsAndMetadata } from './core/downloader';
|
||||
import { getFreeDiskSpace } from './utils/procUtils';
|
||||
import { listDownloaded } from './core/downloadArchive';
|
||||
import { createServer } from 'http';
|
||||
@@ -12,6 +12,7 @@ import * as Router from 'koa-router';
|
||||
import * as serve from 'koa-static';
|
||||
import * as cors from '@koa/cors';
|
||||
import Logger from './utils/BetterLogger';
|
||||
import { ISettings } from './interfaces/ISettings';
|
||||
|
||||
const app = new Koa();
|
||||
const server = createServer(app.callback());
|
||||
@@ -24,6 +25,14 @@ const io = new Server(server, {
|
||||
}
|
||||
});
|
||||
|
||||
let settings: ISettings;
|
||||
|
||||
try {
|
||||
settings = require('../settings.json');
|
||||
} catch (e) {
|
||||
log.warn('settings', 'file not found, ignore if using Docker');
|
||||
}
|
||||
|
||||
// Koa routing
|
||||
router.get('/settings', (ctx, next) => {
|
||||
ctx.redirect('/')
|
||||
@@ -49,7 +58,6 @@ router.get('/stream/:filepath', (ctx, next) => {
|
||||
})
|
||||
|
||||
// WebSocket listeners
|
||||
|
||||
io.on('connection', socket => {
|
||||
log.info('ws', `${socket.handshake.address} connected!`)
|
||||
|
||||
@@ -59,7 +67,7 @@ io.on('connection', socket => {
|
||||
})
|
||||
socket.on('send-url-format-selection', (args) => {
|
||||
log.info('ws', `Formats ${args?.url}`)
|
||||
if (args.url) getFormatsAndInfo(socket, args?.url)
|
||||
if (args.url) getFormatsAndMetadata(socket, args?.url)
|
||||
})
|
||||
socket.on('abort', (args) => {
|
||||
abortDownload(socket, args)
|
||||
@@ -86,10 +94,10 @@ app.use(serve(join(__dirname, 'frontend')))
|
||||
app.use(cors())
|
||||
app.use(router.routes())
|
||||
|
||||
server.listen(process.env.PORT || 3022)
|
||||
server.listen(process.env.PORT || settings.port || 3022)
|
||||
|
||||
splash()
|
||||
log.info('http', `Server started on port ${process.env.PORT || 3022}`)
|
||||
log.info('http', `Server started on port ${process.env.PORT || settings.port || 3022}`)
|
||||
|
||||
/**
|
||||
* Cleanup handler
|
||||
|
||||
Reference in New Issue
Block a user