@vbarbarosh/node-helpers
Advanced tools
Comparing version 3.36.0 to 3.37.0
@@ -5,3 +5,3 @@ { | ||
"name": "@vbarbarosh/node-helpers", | ||
"version": "3.36.0", | ||
"version": "3.37.0", | ||
"description": "A set of helpers for JavaScript/Node.js", | ||
@@ -8,0 +8,0 @@ "files": [ |
function format_bytes(bytes) | ||
{ | ||
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; | ||
if (Number.isNaN(bytes*1)) { | ||
if (typeof bytes !== 'number' || Number.isNaN(bytes)) { | ||
return 'n/a'; | ||
} | ||
if (bytes == 0) { | ||
if (!bytes) { | ||
return '0KB'; | ||
@@ -14,5 +14,5 @@ } | ||
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10); | ||
return `${(bytes / (1024 ** i)).toFixed(bytes > 1024*1024 ? 1 : 0)}${sizes[i]}`; | ||
return `${(bytes / (1024 ** i)).toFixed(bytes > 1024*1024 ? 2 : 0)}${sizes[i]}`.replace(/\.00/, '.0'); | ||
} | ||
module.exports = format_bytes; |
const child_process = require('child_process'); | ||
const format_seconds = require('./format_seconds'); | ||
const stream = require('stream'); | ||
@@ -6,5 +7,6 @@ const stream_each = require('./stream_each'); | ||
async function shell_ytdlp_progress(args, {progress_fn, ...options}) | ||
async function shell_ytdlp_progress(args, {user_friendly_status, ...options}) | ||
{ | ||
const proc = child_process.spawn('yt-dlp', ['--progress', ...args], {...options, stdio: 'pipe'}); | ||
const time0 = Date.now(); | ||
@@ -20,4 +22,9 @@ const promises = []; | ||
await Promise.all(promises); | ||
function progress_fn(v) { | ||
const duration = format_seconds((Date.now() - time0)/1000); | ||
user_friendly_status(`${v.perc} | [${v.current_part}/${v.total_parts}] ${v.done} of ${v.total} at ${v.speed} ETA ${v.eta} duration=${duration}`); | ||
} | ||
} | ||
module.exports = shell_ytdlp_progress; |
@@ -0,1 +1,4 @@ | ||
const format_bytes = require('./format_bytes'); | ||
const format_percents = require('./format_percents'); | ||
const parse_bytes = require('./parse_bytes'); | ||
const stream = require('stream'); | ||
@@ -8,8 +11,26 @@ const stream_filter = require('./stream_filter'); | ||
{ | ||
const re = /^\[download] (.*) of (.*) at (.*) ETA (.*)$/; | ||
let current_part = 1; | ||
const downloading_formats = []; | ||
return stream.compose( | ||
stream_lines(), | ||
stream_map(function (v) { | ||
const m = re.exec(v); | ||
return !m ? false : {done: m[1].trim(), of: m[2].trim(), speed: m[3].trim(), eta: m[4].trim()}; | ||
stream_map(function (line) { | ||
let m; | ||
if (m = line.match(/^\[info] \S+: Downloading \d+ format\(s\): (.+)$/)) { | ||
downloading_formats.push(...m[1].split('+')); | ||
return false; | ||
} | ||
if (m = line.match(/^\[download] Destination: .*f(\d+)\.[^.]+/)) { | ||
current_part = downloading_formats.indexOf(m[1]) + 1; | ||
return false; | ||
} | ||
if (m = line.match(/^\[download] (.*) of (.*) at (.*) ETA (.*)$/)) { | ||
const percentage = parseFloat(m[1].trim())/100; | ||
const total = parse_bytes(m[2].trim()); | ||
const speed = m[3].trim().replace('i', ''); | ||
const eta = m[4].trim(); | ||
return {current_part, total_parts: downloading_formats.length, | ||
perc: format_percents(percentage), done: format_bytes(percentage*total), | ||
total: format_bytes(total), speed, eta}; | ||
} | ||
return false; | ||
}), | ||
@@ -16,0 +37,0 @@ stream_filter(v => v), |
135368
197
3942