@hyrious/blivec
Advanced tools
Comparing version 0.3.20 to 0.3.21
@@ -14,2 +14,3 @@ #!/usr/bin/env node | ||
getRoomPlayInfo, | ||
getUid, | ||
searchRoom, | ||
@@ -90,2 +91,4 @@ sendDanmaku, | ||
function listen(id2, { json = false } = {}) { | ||
const env = get_cookie(true); | ||
const uid = env && getUid(env).catch((err) => (log.catch_error(err), 0)); | ||
let count = 0; | ||
@@ -148,3 +151,3 @@ let con; | ||
}; | ||
con = new Connection(id2, events); | ||
con = new Connection(id2, events, uid); | ||
return con; | ||
@@ -186,5 +189,7 @@ } | ||
} | ||
function get_cookie() { | ||
function get_cookie(silent = false) { | ||
const path = cookiePath(); | ||
if (!path) { | ||
if (silent) | ||
return null; | ||
log.error('Please create a file "cookie.txt" in current directory.'); | ||
@@ -207,2 +212,4 @@ example(); | ||
} else { | ||
if (silent) | ||
return null; | ||
log.error("Invalid cookie.txt"); | ||
@@ -209,0 +216,0 @@ example(); |
@@ -24,2 +24,3 @@ import { Socket } from 'net'; | ||
declare function getRoomInfo(id: number): Promise<RoomInfo>; | ||
declare function getUid(env: Env): Promise<number>; | ||
type TYPE = "heartbeat" | "message" | "welcome" | "unknown" | "join"; | ||
@@ -36,8 +37,10 @@ type ConnectionInfo = RoomInfo & DanmuInfo; | ||
readonly events: Events; | ||
readonly getUid: Promise<number> | null; | ||
socket: Socket | null; | ||
buffer: Buffer; | ||
uid: number; | ||
info: ConnectionInfo | null; | ||
timer_reconnect: NodeJS.Timeout; | ||
timer_heartbeat: NodeJS.Timeout; | ||
constructor(roomId: number, events?: Events); | ||
constructor(roomId: number, events: Events, getUid: Promise<number> | null); | ||
_closed: boolean; | ||
@@ -124,2 +127,2 @@ _connect_index: number; | ||
export { Connection, ConnectionInfo, DanmuInfo, Env, Events, RoomInfo, danmakuHistory, dm_v2_face, getDanmuInfo, getFeedList, getRoomInfo, getRoomPlayInfo, searchRoom, sendDanmaku, stripTags, testUrl }; | ||
export { Connection, ConnectionInfo, DanmuInfo, Env, Events, RoomInfo, danmakuHistory, dm_v2_face, getDanmuInfo, getFeedList, getRoomInfo, getRoomPlayInfo, getUid, searchRoom, sendDanmaku, stripTags, testUrl }; |
@@ -20,2 +20,3 @@ "use strict"; | ||
const api_index = "https://api.live.bilibili.com/xlive/web-room/v1/index"; | ||
const User_Agent = "Mozilla/5.0 (X11; Linux x86_64; rv:60.1) Gecko/20100101 Firefox/60.1"; | ||
export async function getDanmuInfo(id) { | ||
@@ -35,2 +36,15 @@ const info = await get(`${api_index}/getDanmuInfo?id=${id}`); | ||
} | ||
export async function getUid(env) { | ||
const { SESSDATA, bili_jct } = env; | ||
const headers = { | ||
"User-Agent": User_Agent, | ||
"Referer": "https://www.bilibili.com/", | ||
"Cookie": `SESSDATA=${SESSDATA}; bili_jct=${bili_jct}` | ||
}; | ||
const info = await get("https://api.bilibili.com/x/web-interface/nav", { headers }); | ||
const { code, message, data } = JSON.parse(info); | ||
if (code != 0) | ||
throw new Error(message); | ||
return data.mid; | ||
} | ||
const OP_TYPE_MAP = { | ||
@@ -48,5 +62,6 @@ 3: "heartbeat", | ||
export class Connection { | ||
constructor(roomId, events = {}) { | ||
constructor(roomId, events = {}, getUid2) { | ||
this.roomId = roomId; | ||
this.events = events; | ||
this.getUid = getUid2; | ||
this.reconnect(); | ||
@@ -56,2 +71,3 @@ } | ||
buffer = EMPTY_BUFFER; | ||
uid = 0; | ||
info = null; | ||
@@ -63,2 +79,4 @@ timer_reconnect = /* @__PURE__ */ setTimeout(noop); | ||
async connect() { | ||
if (this.getUid) | ||
this.uid = await this.getUid; | ||
const { room_id, title, ...rest } = await getRoomInfo(this.roomId); | ||
@@ -100,7 +118,7 @@ const { host_list, token } = await getDanmuInfo(room_id); | ||
this._encode("join", { | ||
uid: this.info.uid, | ||
uid: this.uid || this.info.uid, | ||
roomid: this.info.room_id, | ||
key: this.info.token, | ||
protover: 2, | ||
platform: "web", | ||
protover: 3, | ||
platform: "danmuji", | ||
type: 2 | ||
@@ -227,3 +245,3 @@ }) | ||
const headers = { | ||
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:60.1) Gecko/20100101 Firefox/60.1", | ||
"User-Agent": User_Agent, | ||
"Referer": "https://www.bilibili.com/", | ||
@@ -317,3 +335,3 @@ "Cookie": `SESSDATA=${SESSDATA}; bili_jct=${bili_jct}` | ||
headers: { | ||
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:60.1) Gecko/20100101 Firefox/60.1", | ||
"User-Agent": User_Agent, | ||
"Referer": `https://search.bilibili.com/live?keyword=${keyword}${params}&search_type=live`, | ||
@@ -330,3 +348,3 @@ "Cookie": `buvid3=${crypto.randomUUID()}infoc;` | ||
const headers = { | ||
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:60.1) Gecko/20100101 Firefox/60.1", | ||
"User-Agent": User_Agent, | ||
"Referer": `https://live.bilibili.com/${roomid}` | ||
@@ -333,0 +351,0 @@ }; |
{ | ||
"name": "@hyrious/blivec", | ||
"version": "0.3.20", | ||
"version": "0.3.21", | ||
"description": "bilibili live cli", | ||
@@ -37,5 +37,5 @@ "type": "module", | ||
"@hyrious/rimraf": "^0.1.0", | ||
"@types/node": "^18.16.19", | ||
"esbuild": "^0.18.11" | ||
"@types/node": "^18.17.15", | ||
"esbuild": "^0.19.2" | ||
} | ||
} |
@@ -10,7 +10,8 @@ #!/usr/bin/env node | ||
Connection, | ||
Env, | ||
Events, | ||
danmakuHistory, | ||
dm_v2_face, | ||
getFeedList, | ||
getRoomPlayInfo, | ||
getUid, | ||
searchRoom, | ||
@@ -104,2 +105,5 @@ sendDanmaku, | ||
function listen(id: number, { json = false } = {}) { | ||
const env = get_cookie(true); | ||
const uid = env && getUid(env).catch((err) => (log.catch_error(err), 0)); | ||
let count = 0; | ||
@@ -170,3 +174,3 @@ let con: Connection; | ||
con = new Connection(id, events); | ||
con = new Connection(id, events, uid); | ||
@@ -207,5 +211,8 @@ return con; | ||
function get_cookie() { | ||
function get_cookie(): Env; | ||
function get_cookie(silent: true): Env | null; | ||
function get_cookie(silent = false) { | ||
const path = cookiePath(); | ||
if (!path) { | ||
if (silent) return null; | ||
log.error('Please create a file "cookie.txt" in current directory.'); | ||
@@ -230,2 +237,3 @@ example(); | ||
} else { | ||
if (silent) return null; | ||
log.error("Invalid cookie.txt"); | ||
@@ -232,0 +240,0 @@ example(); |
@@ -25,2 +25,3 @@ import https from "https"; | ||
const api_index = "https://api.live.bilibili.com/xlive/web-room/v1/index"; | ||
const User_Agent = "Mozilla/5.0 (X11; Linux x86_64; rv:60.1) Gecko/20100101 Firefox/60.1"; | ||
@@ -60,2 +61,15 @@ export interface DanmuInfo { | ||
export async function getUid(env: Env) { | ||
const { SESSDATA, bili_jct } = env; | ||
const headers = { | ||
"User-Agent": User_Agent, | ||
"Referer": "https://www.bilibili.com/", | ||
"Cookie": `SESSDATA=${SESSDATA}; bili_jct=${bili_jct}`, | ||
}; | ||
const info = await get("https://api.bilibili.com/x/web-interface/nav", { headers }); | ||
const { code, message, data } = JSON.parse(info); | ||
if (code != 0) throw new Error(message); | ||
return (data as { mid: number }).mid; | ||
} | ||
type TYPE = "heartbeat" | "message" | "welcome" | "unknown" | "join"; | ||
@@ -85,2 +99,3 @@ const OP_TYPE_MAP: Record<number, TYPE> = { | ||
buffer = EMPTY_BUFFER; | ||
uid = 0; | ||
info: ConnectionInfo | null = null; | ||
@@ -91,3 +106,7 @@ | ||
constructor(readonly roomId: number, readonly events: Events = {}) { | ||
constructor( | ||
readonly roomId: number, | ||
readonly events: Events = {}, | ||
readonly getUid: Promise<number> | null, | ||
) { | ||
this.reconnect(); | ||
@@ -99,2 +118,4 @@ } | ||
async connect() { | ||
if (this.getUid) this.uid = await this.getUid; | ||
const { room_id, title, ...rest } = await getRoomInfo(this.roomId); | ||
@@ -144,7 +165,7 @@ const { host_list, token } = await getDanmuInfo(room_id); | ||
this._encode("join", { | ||
uid: this.info.uid, | ||
uid: this.uid || this.info.uid, | ||
roomid: this.info.room_id, | ||
key: this.info.token, | ||
protover: 2, | ||
platform: "web", | ||
protover: 3, | ||
platform: "danmuji", | ||
type: 2, | ||
@@ -329,3 +350,3 @@ }), | ||
const headers = { | ||
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:60.1) Gecko/20100101 Firefox/60.1", | ||
"User-Agent": User_Agent, | ||
"Referer": "https://www.bilibili.com/", | ||
@@ -471,3 +492,3 @@ "Cookie": `SESSDATA=${SESSDATA}; bili_jct=${bili_jct}`, | ||
headers: { | ||
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:60.1) Gecko/20100101 Firefox/60.1", | ||
"User-Agent": User_Agent, | ||
"Referer": `https://search.bilibili.com/live?keyword=${keyword}${params}&search_type=live`, | ||
@@ -493,3 +514,3 @@ "Cookie": `buvid3=${crypto.randomUUID()}infoc;`, | ||
const headers = { | ||
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:60.1) Gecko/20100101 Firefox/60.1", | ||
"User-Agent": User_Agent, | ||
"Referer": `https://live.bilibili.com/${roomid}`, | ||
@@ -496,0 +517,0 @@ }; |
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
74027
2209