@hyrious/blivec
Advanced tools
Comparing version 0.3.14 to 0.3.15
@@ -36,4 +36,4 @@ #!/usr/bin/env node | ||
default # restart player | ||
ask # ask quality again | ||
quit # quit DD mode | ||
ask # ask quality again (alias: --ask) | ||
quit # quit DD mode (alias: --quit) | ||
-- [...player_args] # pass args to ffplay or mpv | ||
@@ -130,2 +130,16 @@ | ||
} | ||
function configPath(path) { | ||
if (fs.existsSync("blivec.json")) | ||
return "blivec.json"; | ||
path = join(os.homedir(), "blivec.json"); | ||
if (fs.existsSync(path)) | ||
return path; | ||
path = join(os.homedir(), ".config", "blivec.json"); | ||
if (fs.existsSync(path)) | ||
return path; | ||
} | ||
function read_config() { | ||
const path = configPath(); | ||
return path ? JSON.parse(fs.readFileSync(path, "utf-8")) : {}; | ||
} | ||
function example() { | ||
@@ -221,4 +235,16 @@ console.error("Example content:"); | ||
} | ||
function format_interval(minutes) { | ||
if (minutes === 1) | ||
return "1 minute"; | ||
if (minutes < 1) { | ||
const seconds = Math.round(minutes * 60); | ||
if (seconds === 1) | ||
return "1 second"; | ||
if (seconds > 1) | ||
return seconds + " seconds"; | ||
} | ||
return minutes + " minutes"; | ||
} | ||
async function D(id2, { interval = 1, mpv = false, on_close = "default", args = [] } = {}) { | ||
log.info(`DD ${id2} ${interval > 0 ? `every ${interval} minutes` : "once"}`); | ||
log.info(`DD ${id2} ${interval > 0 ? `every ${format_interval(interval)}` : "once"}`); | ||
let con; | ||
@@ -358,2 +384,22 @@ let child; | ||
} | ||
function modify_dd_args(cmd, config) { | ||
if (!config) | ||
return; | ||
let i = config.indexOf("--"); | ||
if (i === -1) { | ||
cmd.unshift(...config); | ||
return; | ||
} | ||
let x = config.slice(0, i); | ||
let y = config.slice(i + 1); | ||
let j = cmd.indexOf("--"); | ||
if (j === -1) { | ||
cmd.unshift(...x); | ||
cmd.push("--", ...y); | ||
return; | ||
} | ||
let a = cmd.slice(0, j); | ||
let b = cmd.slice(j + 1); | ||
cmd.splice(0, cmd.length, ...x, ...a, "--", ...y, ...b); | ||
} | ||
function sigint(con, { json = false } = {}) { | ||
@@ -439,2 +485,4 @@ process.on("SIGINT", () => { | ||
} else { | ||
const config = read_config(); | ||
modify_dd_args(rest, config.d || config.dd); | ||
let interval = 1; | ||
@@ -446,3 +494,3 @@ let mpv = false; | ||
if (arg.startsWith("--interval=")) { | ||
const value = Number.parseInt(arg.slice(11)); | ||
const value = +arg.slice(11); | ||
if (Number.isFinite(value)) { | ||
@@ -464,2 +512,6 @@ interval = Math.max(0, value); | ||
mpv = true; | ||
} else if (arg === "--ask") { | ||
on_close = "ask"; | ||
} else if (arg === "--quit" || arg === "--exit") { | ||
on_close = "quit"; | ||
} else if (arg === "--") { | ||
@@ -466,0 +518,0 @@ args = []; |
{ | ||
"name": "@hyrious/blivec", | ||
"version": "0.3.14", | ||
"version": "0.3.15", | ||
"description": "bilibili live cli", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -62,3 +62,13 @@ # <samp>> <ins>b</ins>ilibili-<ins>live</ins>-<ins>c</ins>li</samp> | ||
- `ask`: Search stream URLs and ask you for a new one to play or just quit. This is useful when you want to switch the stream quality quickly. | ||
- `--quit`: Same as `--on-close=quit`. | ||
- `--ask`: Same as `--on-close=ask`. | ||
**Note**: You can have a `~/.config/blivec.json` to config default arguments: | ||
```json | ||
{ | ||
"dd": ["--mpv", "--quit", "--", "--volume=50"] | ||
} | ||
``` | ||
## Develop | ||
@@ -65,0 +75,0 @@ |
@@ -37,4 +37,4 @@ #!/usr/bin/env node | ||
default # restart player | ||
ask # ask quality again | ||
quit # quit DD mode | ||
ask # ask quality again (alias: --ask) | ||
quit # quit DD mode (alias: --quit) | ||
-- [...player_args] # pass args to ffplay or mpv | ||
@@ -146,2 +146,15 @@ | ||
function configPath(path?: string) { | ||
if (fs.existsSync("blivec.json")) return "blivec.json"; | ||
path = join(os.homedir(), "blivec.json"); | ||
if (fs.existsSync(path)) return path; | ||
path = join(os.homedir(), ".config", "blivec.json"); | ||
if (fs.existsSync(path)) return path; | ||
} | ||
function read_config() { | ||
const path = configPath(); | ||
return path ? JSON.parse(fs.readFileSync(path, "utf-8")) : {}; | ||
} | ||
function example() { | ||
@@ -155,3 +168,3 @@ console.error("Example content:"); | ||
function cookiePath(path?: string | undefined) { | ||
function cookiePath(path?: string) { | ||
if (fs.existsSync("cookie.txt")) return "cookie.txt"; | ||
@@ -245,4 +258,14 @@ path = join(os.homedir(), "cookie.txt"); | ||
function format_interval(minutes) { | ||
if (minutes === 1) return "1 minute"; | ||
if (minutes < 1) { | ||
const seconds = Math.round(minutes * 60); | ||
if (seconds === 1) return "1 second"; | ||
if (seconds > 1) return seconds + " seconds"; | ||
} | ||
return minutes + " minutes"; | ||
} | ||
async function D(id: number, { interval = 1, mpv = false, on_close = "default", args = <string[]>[] } = {}) { | ||
log.info(`DD ${id} ${interval > 0 ? `every ${interval} minutes` : "once"}`); | ||
log.info(`DD ${id} ${interval > 0 ? `every ${format_interval(interval)}` : "once"}`); | ||
@@ -395,2 +418,27 @@ let con!: Connection; | ||
function modify_dd_args(cmd: string[], config?: string[]) { | ||
if (!config) return; | ||
// config = [...x, '--', ...y] | ||
// cmd = [...a, '--', ...b] | ||
// return = [...x, ...a, '--', ...y, ...b] | ||
let i = config.indexOf("--"); | ||
if (i === -1) { | ||
cmd.unshift(...config); | ||
return; | ||
} | ||
let x = config.slice(0, i); | ||
let y = config.slice(i + 1); | ||
let j = cmd.indexOf("--"); | ||
if (j === -1) { | ||
cmd.unshift(...x); | ||
cmd.push("--", ...y); | ||
return; | ||
} | ||
let a = cmd.slice(0, j); | ||
let b = cmd.slice(j + 1); | ||
cmd.splice(0, cmd.length, ...x, ...a, "--", ...y, ...b); | ||
} | ||
function sigint(con: Connection, { json = false } = {}) { | ||
@@ -484,2 +532,4 @@ process.on("SIGINT", () => { | ||
} else { | ||
const config = read_config(); | ||
modify_dd_args(rest, config.d || config.dd); | ||
let interval = 1; | ||
@@ -491,3 +541,3 @@ let mpv = false; | ||
if (arg.startsWith("--interval=")) { | ||
const value = Number.parseInt(arg.slice(11)); | ||
const value = +arg.slice(11); | ||
if (Number.isFinite(value)) { | ||
@@ -509,2 +559,6 @@ interval = Math.max(0, value); | ||
mpv = true; | ||
} else if (arg === "--ask") { | ||
on_close = "ask"; | ||
} else if (arg === "--quit" || arg === "--exit") { | ||
on_close = "quit"; | ||
} else if (arg === "--") { | ||
@@ -511,0 +565,0 @@ args = []; |
64410
1874
87