Code refactoring and set up format selection
Set up format selection and archive download for next releases
This commit is contained in:
@@ -39,9 +39,7 @@ class Process {
|
||||
* @param callback not yet implemented
|
||||
* @returns the process instance
|
||||
*/
|
||||
async start(callback?: Function): Promise<this> {
|
||||
await this.internalGetInfo();
|
||||
|
||||
public async start(callback?: Function): Promise<this> {
|
||||
const sanitizedParams = this.params.filter((param: string) => availableParams.includes(param));
|
||||
|
||||
const ytldp = spawn(this.exePath,
|
||||
@@ -59,37 +57,50 @@ class Process {
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* function used internally by the download process to fetch information, usually thumbnail and title
|
||||
* @returns Promise to the lock
|
||||
*/
|
||||
private async internalGetInfo() {
|
||||
this.lock = true;
|
||||
public getInfo(): Promise<IDownloadInfo> {
|
||||
let stdoutChunks = [];
|
||||
const ytdlpInfo = spawn(this.exePath, ['-s', '-j', this.url]);
|
||||
const ytdlpInfo = spawn(this.exePath, ['-j', this.url]);
|
||||
|
||||
ytdlpInfo.stdout.on('data', (data) => {
|
||||
stdoutChunks.push(data);
|
||||
});
|
||||
|
||||
ytdlpInfo.on('exit', () => {
|
||||
try {
|
||||
const buffer = Buffer.concat(stdoutChunks);
|
||||
const json = JSON.parse(buffer.toString());
|
||||
this.info = json;
|
||||
this.lock = false;
|
||||
return new Promise((resolve, reject) => {
|
||||
ytdlpInfo.on('exit', () => {
|
||||
try {
|
||||
const buffer = Buffer.concat(stdoutChunks);
|
||||
const json = JSON.parse(buffer.toString());
|
||||
this.info = json;
|
||||
this.lock = false;
|
||||
resolve({
|
||||
formats: json.formats.map((format: IDownloadInfoSection) => {
|
||||
return {
|
||||
format_id: format.format_id ?? '',
|
||||
format_note: format.format_note ?? '',
|
||||
fps: format.fps ?? '',
|
||||
resolution: format.resolution ?? '',
|
||||
vcodec: format.vcodec ?? '',
|
||||
}
|
||||
}),
|
||||
best: {
|
||||
format_id: json.format_id ?? '',
|
||||
format_note: json.format_note ?? '',
|
||||
fps: json.fps ?? '',
|
||||
resolution: json.resolution ?? '',
|
||||
vcodec: json.vcodec ?? '',
|
||||
},
|
||||
thumbnail: json.thumbnail,
|
||||
title: json.title,
|
||||
});
|
||||
|
||||
} catch (e) {
|
||||
this.info = {
|
||||
title: "",
|
||||
thumbnail: "",
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
if (!this.lock) {
|
||||
return true;
|
||||
}
|
||||
} catch (e) {
|
||||
reject('failed fetching formats, downloading best available');
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,14 +130,6 @@ class Process {
|
||||
getStdout(): Readable {
|
||||
return this.stdout
|
||||
}
|
||||
|
||||
/**
|
||||
* download info getter function
|
||||
* @returns {*}
|
||||
*/
|
||||
getInfo(): any {
|
||||
return this.info
|
||||
}
|
||||
}
|
||||
|
||||
export default Process;
|
||||
15
server/src/core/downloadArchive.ts
Normal file
15
server/src/core/downloadArchive.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { resolve } from "path";
|
||||
|
||||
const archived = [
|
||||
{
|
||||
id: 1,
|
||||
title: 'AleXa (알렉사) – Voting Open in American Song Contest Grand Final!',
|
||||
path: resolve('downloads/AleXa (알렉사) – Voting Open in American Song Contest Grand Final!.webm'),
|
||||
img: 'https://i.ytimg.com/vi/WbBUz7pjUnM/hq720.jpg?sqp=-oaymwEcCNAFEJQDSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLAi5MNtvpgnY9aRpdFlhAfhdV7Zlg',
|
||||
},
|
||||
]
|
||||
|
||||
export function listDownloaded(ctx: any, next: any) {
|
||||
ctx.body = archived
|
||||
next()
|
||||
}
|
||||
@@ -23,6 +23,18 @@ catch (e) {
|
||||
new Promise(resolve => setTimeout(resolve, 500))
|
||||
.then(() => log.warn('dl', 'settings.json not found, ignore if using Docker'));
|
||||
}
|
||||
/**
|
||||
* Get download info such as thumbnail, title, resolution and list all formats
|
||||
* @param socket
|
||||
* @param url
|
||||
*/
|
||||
export async function getFormatsAndInfo(socket: Socket, url: string) {
|
||||
let p = new Process(url, [], settings);
|
||||
const formats = await p.getInfo();
|
||||
console.log(formats)
|
||||
socket.emit('available-formats', formats)
|
||||
p = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke a new download.
|
||||
@@ -42,27 +54,21 @@ export async function download(socket: Socket, payload: IPayload) {
|
||||
payload.params.split(' ') :
|
||||
payload.params;
|
||||
|
||||
const p = new Process(url, params, settings);
|
||||
let p = new Process(url, params, settings);
|
||||
|
||||
p.start().then(downloader => {
|
||||
pool.add(p)
|
||||
let infoLock = true;
|
||||
let pid = downloader.getPid();
|
||||
|
||||
p.getInfo().then(info => {
|
||||
socket.emit('info', { pid: pid, info: info });
|
||||
});
|
||||
|
||||
from(downloader.getStdout()) // stdout as observable
|
||||
.pipe(throttle(() => interval(500))) // discard events closer than 500ms
|
||||
.subscribe({
|
||||
next: (stdout) => {
|
||||
if (infoLock) {
|
||||
if (downloader.getInfo() === null) {
|
||||
return;
|
||||
}
|
||||
socket.emit('info', {
|
||||
pid: pid, info: downloader.getInfo()
|
||||
});
|
||||
infoLock = false;
|
||||
}
|
||||
socket.emit('progress', formatter(String(stdout), pid)) // finally, emit
|
||||
socket.emit('progress', formatter(String(stdout), pid))
|
||||
},
|
||||
complete: () => {
|
||||
downloader.kill().then(() => {
|
||||
@@ -79,11 +85,10 @@ export async function download(socket: Socket, payload: IPayload) {
|
||||
});
|
||||
}
|
||||
});
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* Retrieve all downloads.
|
||||
* If the server has just been launched retrieve the ones saved to the database.
|
||||
* If the server is running fetches them from the process pool.
|
||||
|
||||
39
server/src/core/streamer.ts
Normal file
39
server/src/core/streamer.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { stat, createReadStream } from 'fs';
|
||||
import { lookup } from 'mime-types';
|
||||
|
||||
export function streamer(ctx: any, next: any) {
|
||||
const filepath = '/Users/marco/dev/homebrew/yt-dlp-web-ui/downloads/AleXa (알렉사) – Voting Open in American Song Contest Grand Final!.webm'
|
||||
stat(filepath, (err, stat) => {
|
||||
if (err) {
|
||||
ctx.response.status = 404;
|
||||
ctx.body = { err: 'resource not found' };
|
||||
next();
|
||||
}
|
||||
const fileSize = stat.size;
|
||||
const range = ctx.headers.range;
|
||||
if (range) {
|
||||
const parts = range.replace(/bytes=/, '').split('-');
|
||||
const start = parseInt(parts[0], 10);
|
||||
const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;
|
||||
const chunksize = end - start + 1;
|
||||
const file = createReadStream(filepath, { start, end });
|
||||
const head = {
|
||||
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
|
||||
'Accept-Ranges': 'bytes',
|
||||
'Content-Length': chunksize,
|
||||
'Content-Type': lookup(filepath)
|
||||
};
|
||||
ctx.res.writeHead(206, head);
|
||||
file.pipe(ctx.res);
|
||||
next();
|
||||
} else {
|
||||
const head = {
|
||||
'Content-Length': fileSize,
|
||||
'Content-Type': 'video/mp4'
|
||||
};
|
||||
ctx.res.writeHead(200, head);
|
||||
createReadStream(ctx.params.filepath).pipe(ctx.res);
|
||||
next();
|
||||
}
|
||||
});
|
||||
}
|
||||
0
server/src/db/db.ts
Normal file
0
server/src/db/db.ts
Normal file
14
server/src/interfaces/IDownloadInfo.ts
Normal file
14
server/src/interfaces/IDownloadInfo.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
interface IDownloadInfo {
|
||||
formats: Array<IDownloadInfoSection>,
|
||||
best: IDownloadInfoSection,
|
||||
thumbnail: string,
|
||||
title: string,
|
||||
}
|
||||
|
||||
interface IDownloadInfoSection {
|
||||
format_id: string,
|
||||
format_note: string,
|
||||
fps: number,
|
||||
resolution: string,
|
||||
vcodec: string,
|
||||
}
|
||||
@@ -2,28 +2,53 @@ import { logger, splash } from './utils/logger';
|
||||
import { join } from 'path';
|
||||
import { Server } from 'socket.io';
|
||||
import { ytdlpUpdater } from './utils/updater';
|
||||
import { download, abortDownload, retrieveDownload, abortAllDownloads } from './core/downloader';
|
||||
import { download, abortDownload, retrieveDownload, abortAllDownloads, getFormatsAndInfo } from './core/downloader';
|
||||
import { getFreeDiskSpace } from './utils/procUtils';
|
||||
import Logger from './utils/BetterLogger';
|
||||
import Jean from './core/HTTPServer';
|
||||
import { listDownloaded } from './core/downloadArchive';
|
||||
import { createServer } from 'http';
|
||||
import * as Koa from 'koa';
|
||||
import * as Router from 'koa-router';
|
||||
import * as serve from 'koa-static';
|
||||
import * as cors from '@koa/cors';
|
||||
import { streamer } from './core/streamer';
|
||||
|
||||
const server = new Jean(join(__dirname, 'frontend')).createServer();
|
||||
const app = new Koa();
|
||||
const server = createServer(app.callback());
|
||||
const router = new Router();
|
||||
const log = new Logger();
|
||||
const io = new Server(server, {
|
||||
cors: {
|
||||
origin: "*",
|
||||
methods: ["GET", "POST"]
|
||||
}
|
||||
});
|
||||
|
||||
// Koa routing
|
||||
|
||||
router.get('/settings', (ctx, next) => {
|
||||
ctx.redirect('/')
|
||||
next()
|
||||
})
|
||||
router.get('/downloaded', (ctx, next) => {
|
||||
ctx.redirect('/')
|
||||
next()
|
||||
})
|
||||
router.get('/getAllDownloaded', (ctx, next) => {
|
||||
listDownloaded(ctx, next)
|
||||
})
|
||||
router.get('/stream/:filepath', (ctx, next) => {
|
||||
streamer(ctx, next)
|
||||
})
|
||||
|
||||
/*
|
||||
WebSocket listeners
|
||||
*/
|
||||
// WebSocket listeners
|
||||
|
||||
io.on('connection', socket => {
|
||||
logger('ws', `${socket.handshake.address} connected!`)
|
||||
|
||||
socket.on('send-url', (args) => {
|
||||
logger('ws', args?.url)
|
||||
//if (args.url) getFormatsAndInfo(socket, args?.url)
|
||||
download(socket, args)
|
||||
})
|
||||
socket.on('abort', (args) => {
|
||||
@@ -47,6 +72,10 @@ io.on('disconnect', (socket) => {
|
||||
logger('ws', `${socket.handshake.address} disconnected`)
|
||||
})
|
||||
|
||||
app.use(serve(join(__dirname, 'frontend')))
|
||||
app.use(router.routes())
|
||||
app.use(cors())
|
||||
|
||||
server.listen(process.env.PORT || 3022)
|
||||
|
||||
splash()
|
||||
@@ -63,7 +92,7 @@ const gracefullyStop = () => {
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
/* Intercepts singnals and perform cleanups before shutting down. */
|
||||
// Intercepts singnals and perform cleanups before shutting down.
|
||||
process
|
||||
.on('SIGTERM', () => gracefullyStop())
|
||||
.on('SIGUSR1', () => gracefullyStop())
|
||||
|
||||
Reference in New Issue
Block a user