@bililive-tools/huya-recorder
Version:
bililive-tools huya recorder implemention
98 lines (97 loc) • 3.86 kB
JavaScript
import { requester } from "./requester.js";
import { utils } from "@bililive-tools/manager";
import { assert, getFormatSources } from "./utils.js";
import { initInfo } from "./anticode.js";
export async function getRoomInfo(roomIdOrShortId, opts = {}) {
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,
}));
streams.push({ desc: "真原画", bitRate: -1 });
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) {
let sStreamName = item.sStreamName;
if (opts.quality === -1) {
sStreamName = sStreamName.replace("-imgplus", "");
}
const url = initInfo({
baseUrl: item.sFlvUrl,
sStreamName: sStreamName,
antiCode: item.sFlvAntiCode,
suffix: item.sFlvUrlSuffix,
_sessionId: Date.now(),
});
sources.flv.push({
name: item.sCdnType,
url,
streamName: sStreamName,
presenterUid: item.lPresenterUid,
subChannelId: item.lSubChannelId,
channelId: item.lChannelId,
suffix: item.sFlvUrlSuffix,
});
}
if (item.sHlsAntiCode && item.sHlsAntiCode.length > 0) {
let sStreamName = item.sStreamName;
if (opts.quality === -1) {
sStreamName = sStreamName.replace("-imgplus", "");
}
const url = initInfo({
baseUrl: item.sHlsUrl,
sStreamName: sStreamName,
antiCode: item.sHlsAntiCode,
suffix: item.sHlsUrlSuffix,
_sessionId: Date.now(),
});
sources.hls.push({
name: item.sCdnType,
url,
streamName: sStreamName,
presenterUid: item.lPresenterUid,
subChannelId: item.lSubChannelId,
channelId: item.lChannelId,
suffix: item.sHlsUrlSuffix,
});
}
}
const startTime = new Date(data.gameLiveInfo?.startTime * 1000);
const formatSources = getFormatSources(sources, opts.formatPriorities);
return {
living: vMultiStreamInfo.length > 0 && data.gameStreamInfoList.length > 0,
api: "web",
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: formatSources?.sources ?? [],
startTime,
liveId: utils.md5(`${roomIdOrShortId}-${startTime?.getTime()}`),
gid: data.gameLiveInfo.gid,
};
}