@hyrious/blivec
Advanced tools
Comparing version 0.3.11 to 0.3.12
@@ -9,3 +9,11 @@ #!/usr/bin/env node | ||
import readline from "readline"; | ||
import { Connection, getRoomPlayInfo, searchRoom, sendDanmaku, stripTags, testUrl } from "./index.js"; | ||
import { | ||
Connection, | ||
getFeedList, | ||
getRoomPlayInfo, | ||
searchRoom, | ||
sendDanmaku, | ||
stripTags, | ||
testUrl | ||
} from "./index.js"; | ||
const help = ` | ||
@@ -16,6 +24,9 @@ Usage: | ||
bl <room_id> <message> # send danmaku | ||
bl <room_id> <message> # send danmaku (requires cookie) | ||
bl get <room_id> # get stream url | ||
--json # print them in json | ||
bl feed # get feed list (requires cookie) | ||
--json # print them in json | ||
@@ -122,23 +133,23 @@ bl d <room_id> [--interval=1] # dd mode | ||
} | ||
async function send(id2, message) { | ||
function example() { | ||
console.error("Example content:"); | ||
console.error(""); | ||
console.error("SESSDATA=..."); | ||
console.error("bili_jct=..."); | ||
console.error(); | ||
} | ||
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; | ||
} | ||
function example() { | ||
console.error("Example content:"); | ||
console.error(""); | ||
console.error("SESSDATA=..."); | ||
console.error("bili_jct=..."); | ||
console.error(); | ||
} | ||
function cookiePath(path) { | ||
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; | ||
} | ||
function get_cookie() { | ||
const path = cookiePath(); | ||
@@ -161,8 +172,33 @@ if (!path) { | ||
if (env.SESSDATA && env.bili_jct) { | ||
await sendDanmaku(id2, message, env).catch(log.catch_error); | ||
return env; | ||
} else { | ||
log.error("Invalid cookie.txt"); | ||
example(); | ||
process.exit(1); | ||
} | ||
} | ||
async function send(id2, message) { | ||
const env = get_cookie(); | ||
await sendDanmaku(id2, message, env).catch(log.catch_error); | ||
} | ||
async function feed({ json = false } = {}) { | ||
const env = get_cookie(); | ||
let res; | ||
try { | ||
res = await getFeedList(env); | ||
} catch (err) { | ||
log.catch_error(err); | ||
process.exitCode = 1; | ||
return; | ||
} | ||
if (json) { | ||
console.log(JSON.stringify(res.list, null, 2)); | ||
return; | ||
} | ||
log.info(`Found ${res.results} rooms:`); | ||
for (let i = 0; i < res.list.length; i++) { | ||
const { roomid, uname, title } = res.list[i]; | ||
log.info(` [${String(i + 1).padStart(2)}] ${String(roomid).padStart(8)}: ${uname} - ${title}`); | ||
} | ||
} | ||
async function get(id2, { json = false } = {}) { | ||
@@ -341,3 +377,3 @@ try { | ||
let id; | ||
if (arg1 === "get" || arg1 === "d" || arg1 === "dd") { | ||
if (arg1 === "get" || arg1 === "d" || arg1 === "dd" || arg1 === "feed") { | ||
action = arg1; | ||
@@ -348,2 +384,6 @@ id_or_keyword = arg2; | ||
} | ||
if (action === "feed") { | ||
await feed({ json: arg2 === "--json" }); | ||
process.exit(); | ||
} | ||
let maybe_id = Number.parseInt(id_or_keyword); | ||
@@ -350,0 +390,0 @@ if (Number.isSafeInteger(maybe_id) && maybe_id > 0) { |
@@ -71,2 +71,28 @@ import { Socket } from 'net'; | ||
declare function sendDanmaku(id: number, message: string, env: Env): Promise<string>; | ||
interface FeedListResult { | ||
results: number; | ||
page: number; | ||
pagesize: number; | ||
list: Array<{ | ||
cover: string; | ||
face: string; | ||
uname: string; | ||
title: string; | ||
roomid: string; | ||
pic: string; | ||
online: number; | ||
link: string; | ||
uid: number; | ||
watched_show: { | ||
switch: boolean; | ||
num: number; | ||
text_small: string; | ||
text_large: string; | ||
icon: string; | ||
icon_location: number; | ||
icon_web: string; | ||
}; | ||
}>; | ||
} | ||
declare function getFeedList(env: Env): Promise<FeedListResult>; | ||
interface PlayInfo { | ||
@@ -92,2 +118,2 @@ container: string; | ||
export { Connection, ConnectionInfo, DanmuInfo, Env, Events, RoomInfo, getDanmuInfo, getRoomInfo, getRoomPlayInfo, searchRoom, sendDanmaku, stripTags, testUrl }; | ||
export { Connection, ConnectionInfo, DanmuInfo, Env, Events, RoomInfo, getDanmuInfo, getFeedList, getRoomInfo, getRoomPlayInfo, searchRoom, sendDanmaku, stripTags, testUrl }; |
@@ -231,2 +231,15 @@ "use strict"; | ||
} | ||
export async function getFeedList(env) { | ||
const { SESSDATA, bili_jct } = env; | ||
const headers = { | ||
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:60.1) Gecko/20100101 Firefox/60.1", | ||
"Referer": "https://www.bilibili.com/", | ||
"Cookie": `SESSDATA=${SESSDATA}; bili_jct=${bili_jct}` | ||
}; | ||
const res = await get(`https://api.live.bilibili.com/relation/v1/feed/feed_list`, { headers }); | ||
const { code, message, data } = JSON.parse(res); | ||
if (code != 0) | ||
throw new Error(message); | ||
return data; | ||
} | ||
export async function getRoomPlayInfo(id) { | ||
@@ -233,0 +246,0 @@ let code, message, data; |
{ | ||
"name": "@hyrious/blivec", | ||
"version": "0.3.11", | ||
"version": "0.3.12", | ||
"description": "bilibili live cli", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -8,3 +8,12 @@ #!/usr/bin/env node | ||
import readline from "readline"; | ||
import { Connection, Events, getRoomPlayInfo, searchRoom, sendDanmaku, stripTags, testUrl } from "./index.js"; | ||
import { | ||
Connection, | ||
Events, | ||
getFeedList, | ||
getRoomPlayInfo, | ||
searchRoom, | ||
sendDanmaku, | ||
stripTags, | ||
testUrl, | ||
} from "./index.js"; | ||
@@ -16,6 +25,9 @@ const help = ` | ||
bl <room_id> <message> # send danmaku | ||
bl <room_id> <message> # send danmaku (requires cookie) | ||
bl get <room_id> # get stream url | ||
--json # print them in json | ||
bl feed # get feed list (requires cookie) | ||
--json # print them in json | ||
@@ -137,21 +149,21 @@ bl d <room_id> [--interval=1] # dd mode | ||
async function send(id: number, message: string) { | ||
function example() { | ||
console.error("Example content:"); | ||
console.error(""); | ||
console.error("SESSDATA=..."); | ||
console.error("bili_jct=..."); | ||
console.error(); | ||
} | ||
function example() { | ||
console.error("Example content:"); | ||
console.error(""); | ||
console.error("SESSDATA=..."); | ||
console.error("bili_jct=..."); | ||
console.error(); | ||
} | ||
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; | ||
} | ||
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; | ||
} | ||
function get_cookie() { | ||
const path = cookiePath(); | ||
@@ -176,9 +188,39 @@ if (!path) { | ||
if (env.SESSDATA && env.bili_jct) { | ||
await sendDanmaku(id, message, env).catch(log.catch_error); | ||
return env; | ||
} else { | ||
log.error("Invalid cookie.txt"); | ||
example(); | ||
process.exit(1); | ||
} | ||
} | ||
async function send(id: number, message: string) { | ||
const env = get_cookie(); | ||
await sendDanmaku(id, message, env).catch(log.catch_error); | ||
} | ||
async function feed({ json = false } = {}) { | ||
const env = get_cookie(); | ||
let res; | ||
try { | ||
res = await getFeedList(env); | ||
} catch (err) { | ||
log.catch_error(err); | ||
process.exitCode = 1; | ||
return; | ||
} | ||
if (json) { | ||
console.log(JSON.stringify(res.list, null, 2)); | ||
return; | ||
} | ||
log.info(`Found ${res.results} rooms:`); | ||
for (let i = 0; i < res.list.length; i++) { | ||
const { roomid, uname, title } = res.list[i]; | ||
log.info(` [${String(i + 1).padStart(2)}] ${String(roomid).padStart(8)}: ${uname} - ${title}`); | ||
} | ||
} | ||
async function get(id: number, { json = false } = {}) { | ||
@@ -373,3 +415,3 @@ try { | ||
if (arg1 === "get" || arg1 === "d" || arg1 === "dd") { | ||
if (arg1 === "get" || arg1 === "d" || arg1 === "dd" || arg1 === "feed") { | ||
action = arg1; | ||
@@ -381,2 +423,8 @@ id_or_keyword = arg2; | ||
// the feed command do not need room id, so handle it here | ||
if (action === "feed") { | ||
await feed({ json: arg2 === "--json" }); | ||
process.exit(); | ||
} | ||
// resolve keyword to room id | ||
@@ -383,0 +431,0 @@ let maybe_id = Number.parseInt(id_or_keyword); |
@@ -303,2 +303,41 @@ import https from "https"; | ||
interface FeedListResult { | ||
results: number; | ||
page: number; | ||
pagesize: number; | ||
list: Array<{ | ||
cover: string; | ||
face: string; | ||
uname: string; | ||
title: string; | ||
roomid: string; | ||
pic: string; | ||
online: number; | ||
link: string; | ||
uid: number; | ||
watched_show: { | ||
switch: boolean; | ||
num: number; | ||
text_small: string; | ||
text_large: string; | ||
icon: string; | ||
icon_location: number; | ||
icon_web: string; | ||
}; | ||
}>; | ||
} | ||
export async function getFeedList(env: Env) { | ||
const { SESSDATA, bili_jct } = env; | ||
const headers = { | ||
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:60.1) Gecko/20100101 Firefox/60.1", | ||
"Referer": "https://www.bilibili.com/", | ||
"Cookie": `SESSDATA=${SESSDATA}; bili_jct=${bili_jct}`, | ||
}; | ||
const res = await get(`https://api.live.bilibili.com/relation/v1/feed/feed_list`, { headers }); | ||
const { code, message, data } = JSON.parse(res); | ||
if (code != 0) throw new Error(message); | ||
return data as FeedListResult; | ||
} | ||
interface PlayUrlInfo { | ||
@@ -305,0 +344,0 @@ playurl_info: { |
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
61620
1793