UNPKG

@bililive-tools/huya-recorder

Version:
80 lines (79 loc) 2.98 kB
import axios from "axios"; import { utils } from "@bililive-tools/manager"; import { assert } from "./utils.js"; import { initInfo } from "./anticode.js"; const requester = axios.create({ timeout: 10e3, }); export async function getRoomInfo(roomIdOrShortId, formatName = "auto") { const res = await requester.get(`https://www.huya.com/${roomIdOrShortId}`); const html = res.data; const match = html.match(/var hyPlayerConfig = ({[^]+?};)/); const hyPlayerConfigString = match?.[1]; const hyPlayerConfig = new Function(`return ${hyPlayerConfigString}`)(); assert(hyPlayerConfig, `Unexpected resp, hyPlayerConfig is null`); // console.log(JSON.stringify(hyPlayerConfig, null, 2)); const { vMultiStreamInfo } = hyPlayerConfig.stream; const streams = vMultiStreamInfo.map((info) => ({ desc: info.sDisplayName, bitRate: info.iBitRate, })); const data = hyPlayerConfig.stream.data[0]; assert(data, `Unexpected resp, data is null`); const sources = { flv: [], hls: [], }; // const sources: SourceProfile[] = data.gameStreamInfoList.map((info) => ({ // name: info.sCdnType, // url: initInfo({ // sFlvUrl: info.sFlvUrl, // sStreamName: info.sStreamName, // sFlvAntiCode: info.sFlvAntiCode, // _sessionId: Date.now(), // }), // })); for (const item of data?.gameStreamInfoList ?? []) { if (item.sFlvAntiCode && item.sFlvAntiCode.length > 0) { const url = initInfo({ baseUrl: item.sFlvUrl, sStreamName: item.sStreamName, antiCode: item.sFlvAntiCode, suffix: item.sFlvUrlSuffix, _sessionId: Date.now(), }); sources.flv.push({ name: item.sCdnType, url, }); } if (item.sHlsAntiCode && item.sHlsAntiCode.length > 0) { const url = initInfo({ baseUrl: item.sHlsUrl, sStreamName: item.sStreamName, antiCode: item.sHlsAntiCode, suffix: item.sHlsUrlSuffix, _sessionId: Date.now(), }); sources.hls.push({ name: item.sCdnType, url, }); } } const startTime = new Date(data.gameLiveInfo?.startTime * 1000); return { living: vMultiStreamInfo.length > 0 && data.gameStreamInfoList.length > 0, id: data.gameLiveInfo.profileRoom, owner: data.gameLiveInfo.nick, title: data.gameLiveInfo.introduction, roomId: data.gameLiveInfo.profileRoom, avatar: data.gameLiveInfo.avatar180, cover: data.gameLiveInfo.screenshot, streams, sources: formatName === "hls" ? sources.hls : sources.flv, startTime, liveId: utils.md5(`${roomIdOrShortId}-${startTime?.getTime()}`), gid: data.gameLiveInfo.gid, }; }