@hyrious/blivec
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -184,2 +184,3 @@ "use strict"; | ||
clearTimeout(this.timer_reconnect); | ||
(this.events.quit || noop)(); | ||
if (this.socket) { | ||
@@ -186,0 +187,0 @@ this.socket.end(); |
#!/usr/bin/env node | ||
"use strict"; | ||
import os from "os"; | ||
import fs from "fs"; | ||
import { join } from "path"; | ||
import rl from "readline"; | ||
@@ -30,2 +32,26 @@ import cp from "child_process"; | ||
function listen(id, { json = false } = {}) { | ||
let repl; | ||
function setup_repl() { | ||
if (process.stdout.isTTY) { | ||
console.log('[blivec] type "> message" to send danmaku'); | ||
repl = rl.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
prompt: "" | ||
}); | ||
repl.on("line", (line) => { | ||
line = line.trim(); | ||
if (line.startsWith("> ") && line.length > 2) { | ||
rl.moveCursor(process.stdout, 0, -1); | ||
rl.clearLine(process.stdout, 0); | ||
line = line.slice(2); | ||
send(id, line).catch(console.error); | ||
} else { | ||
console.log( | ||
'[blivec] message needs to start with "> " (space is required)' | ||
); | ||
} | ||
}); | ||
} | ||
} | ||
const events = json ? { | ||
@@ -43,2 +69,3 @@ init: (data) => console.log(JSON.stringify({ cmd: "init", data })), | ||
} | ||
setup_repl(); | ||
}, | ||
@@ -52,3 +79,4 @@ message(a) { | ||
}, | ||
error: console.error | ||
error: console.error, | ||
quit: () => repl && repl.close() | ||
}; | ||
@@ -65,3 +93,17 @@ return new Connection(id, events); | ||
} | ||
if (!fs.existsSync("cookie.txt")) { | ||
function cookiePath(path2) { | ||
if (fs.existsSync("cookie.txt")) | ||
return "cookie.txt"; | ||
path2 = join(os.homedir(), "cookie.txt"); | ||
if (fs.existsSync(path2)) | ||
return path2; | ||
path2 = join(os.homedir(), ".config", "cookie.txt"); | ||
if (fs.existsSync(path2)) | ||
return path2; | ||
path2 = join(os.homedir(), ".config", "blivec", "cookie.txt"); | ||
if (fs.existsSync(path2)) | ||
return path2; | ||
} | ||
const path = cookiePath(); | ||
if (!path) { | ||
console.log('Please create a file "cookie.txt" in current directory.'); | ||
@@ -71,3 +113,3 @@ example(); | ||
} | ||
const cookie = fs.readFileSync("cookie.txt", "utf-8"); | ||
const cookie = fs.readFileSync(path, "utf-8"); | ||
let env = { SESSDATA: "", bili_jct: "" }; | ||
@@ -181,4 +223,6 @@ for (const line of cookie.split("\n")) { | ||
args.push("--title=" + title); | ||
args.push("--geometry=50%"); | ||
args.push(url); | ||
child = cp.spawn("mpv", args, { stdio: "inherit" }); | ||
child = cp.spawn("mpv", args, { stdio: "inherit", detached: true }); | ||
child.unref(); | ||
} else { | ||
@@ -188,2 +232,3 @@ const args = ["-hide_banner", "-loglevel", "error"]; | ||
args.push("-window_title", title); | ||
args.push("-x", "1280", "-y", "720"); | ||
args.push(url); | ||
@@ -196,6 +241,9 @@ child = cp.spawn("ffplay", args, { stdio: "inherit" }); | ||
} | ||
function sigint(con) { | ||
function sigint(con, { json = false } = {}) { | ||
process.on("SIGINT", () => { | ||
if (!con.closed) { | ||
console.log("\n[blivec] closing..."); | ||
if (json) | ||
console.log(JSON.stringify({ cmd: "exit" })); | ||
else | ||
console.log("\n[blivec] closing..."); | ||
con.close(); | ||
@@ -242,3 +290,3 @@ } | ||
const con = listen(id, { json }); | ||
sigint(con); | ||
sigint(con, { json }); | ||
} | ||
@@ -245,0 +293,0 @@ } else { |
@@ -184,2 +184,3 @@ "use strict"; | ||
clearTimeout(this.timer_reconnect); | ||
(this.events.quit || noop)(); | ||
if (this.socket) { | ||
@@ -186,0 +187,0 @@ this.socket.end(); |
{ | ||
"name": "@hyrious/blivec", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "bilibili live cli", | ||
@@ -5,0 +5,0 @@ "type": "module", |
#!/usr/bin/env node | ||
import os from "os"; | ||
import fs from "fs"; | ||
import { join } from "path"; | ||
import rl from "readline"; | ||
@@ -33,2 +35,28 @@ import cp, { ChildProcess } from "child_process"; | ||
function listen(id: number, { json = false } = {}) { | ||
let repl: rl.Interface | undefined; | ||
function setup_repl() { | ||
if (process.stdout.isTTY) { | ||
console.log('[blivec] type "> message" to send danmaku'); | ||
repl = rl.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
prompt: "", | ||
}); | ||
repl.on("line", (line) => { | ||
line = line.trim(); | ||
if (line.startsWith("> ") && line.length > 2) { | ||
rl.moveCursor(process.stdout, 0, -1); // move up | ||
rl.clearLine(process.stdout, 0); // clear the user input | ||
line = line.slice(2); | ||
send(id, line).catch(console.error); | ||
} else { | ||
console.log( | ||
'[blivec] message needs to start with "> " (space is required)' | ||
); | ||
} | ||
}); | ||
} | ||
} | ||
const events: Events = json | ||
@@ -48,2 +76,3 @@ ? { | ||
} | ||
setup_repl(); | ||
}, | ||
@@ -58,2 +87,3 @@ message(a) { | ||
error: console.error, | ||
quit: () => repl && repl.close(), | ||
}; | ||
@@ -73,3 +103,14 @@ | ||
if (!fs.existsSync("cookie.txt")) { | ||
function cookiePath(path?: string | undefined) { | ||
if (fs.existsSync("cookie.txt")) return "cookie.txt"; | ||
path = join(os.homedir(), "cookie.txt"); | ||
if (fs.existsSync(path)) return path; | ||
path = join(os.homedir(), ".config", "cookie.txt"); | ||
if (fs.existsSync(path)) return path; | ||
path = join(os.homedir(), ".config", "blivec", "cookie.txt"); | ||
if (fs.existsSync(path)) return path; | ||
} | ||
const path = cookiePath(); | ||
if (!path) { | ||
console.log('Please create a file "cookie.txt" in current directory.'); | ||
@@ -80,3 +121,3 @@ example(); | ||
const cookie = fs.readFileSync("cookie.txt", "utf-8"); | ||
const cookie = fs.readFileSync(path, "utf-8"); | ||
let env = { SESSDATA: "", bili_jct: "" }; | ||
@@ -197,9 +238,11 @@ for (const line of cookie.split("\n")) { | ||
args.push("--title=" + title); | ||
args.push("--geometry=50%"); | ||
args.push(url); | ||
child = cp.spawn("mpv", args, { stdio: "inherit" }); | ||
child = cp.spawn("mpv", args, { stdio: "inherit", detached: true }); | ||
child.unref(); | ||
} else { | ||
// ffplay | ||
const args = ["-hide_banner", "-loglevel", "error"]; | ||
args.push("-headers", headers.map((e) => e + "\r\n").join("")); | ||
args.push("-window_title", title); | ||
args.push("-x", "1280", "-y", "720"); | ||
args.push(url); | ||
@@ -214,6 +257,7 @@ child = cp.spawn("ffplay", args, { stdio: "inherit" }); | ||
function sigint(con: Connection) { | ||
function sigint(con: Connection, { json = false } = {}) { | ||
process.on("SIGINT", () => { | ||
if (!con.closed) { | ||
console.log("\n[blivec] closing..."); | ||
if (json) console.log(JSON.stringify({ cmd: "exit" })); | ||
else console.log("\n[blivec] closing..."); | ||
con.close(); | ||
@@ -261,3 +305,3 @@ } | ||
const con = listen(id, { json }); | ||
sigint(con); | ||
sigint(con, { json }); | ||
} | ||
@@ -264,0 +308,0 @@ } else { |
@@ -73,2 +73,3 @@ import https from "https"; | ||
error?: (err: any) => void; | ||
quit?: () => void; | ||
} | ||
@@ -241,2 +242,3 @@ | ||
clearTimeout(this.timer_reconnect); | ||
(this.events.quit || noop)(); | ||
if (this.socket) { | ||
@@ -243,0 +245,0 @@ this.socket.end(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50114
1463