rsshub
Version:
Make RSS Great Again!
101 lines (100 loc) • 3.06 kB
JavaScript
import { t as rofetch } from "./ofetch-W1aeTHCo.mjs";
import { t as config } from "./config-Bpyy15zS.mjs";
import { t as parseDate } from "./parse-date-CjhZE69i.mjs";
import { t as cache_default } from "./cache-CiDoUSLo.mjs";
import { s as solveWafChallenge } from "./utils-BImtW9k-.mjs";
import { load } from "cheerio";
//#region lib/routes/tiktok/live.ts
const baseUrl = "https://www.tiktok.com";
const route = {
path: "/live/:user",
categories: ["social-media"],
example: "/tiktok/live/@shinichifuku",
parameters: { user: "User ID, including @" },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: true,
supportBT: false,
supportPodcast: false,
supportScihub: false
},
radar: [{
source: ["www.tiktok.com/:user/live"],
target: "/live/:user"
}],
name: "Live",
maintainers: ["TonyRL"],
handler
};
const apiGetLiveRoom = async (handle) => {
const { data } = await rofetch(`${baseUrl}/api-live/user/room/`, { query: {
aid: "1988",
sourceType: "54",
uniqueId: handle
} });
return data?.user?.uniqueId ? data : null;
};
const htmlGetLiveRoom = async (link) => {
let response = await rofetch(link);
let $ = load(response);
if ($("p#wci").hasClass("_wafchallengeid")) {
response = await rofetch(link, { headers: { cookie: `_wafchallengeid=${solveWafChallenge($("p#cs").attr("class"))};` } });
$ = load(response);
}
return JSON.parse($("script#SIGI_STATE").text()).LiveRoom.liveRoomUserInfo;
};
const checkAlive = async (roomId) => {
const { data } = await rofetch("https://webcast.tiktok.com/webcast/room/check_alive/", { query: {
aid: "1988",
room_ids: roomId
} });
return data?.[0]?.alive ?? null;
};
async function handler(ctx) {
const user = ctx.req.param("user");
const handle = user.startsWith("@") ? user.slice(1) : user;
const link = `${baseUrl}/@${handle}/live`;
const { user: userInfo, liveRoom } = await cache_default.tryGet(`tiktok:live:${handle}`, async () => {
let info = await apiGetLiveRoom(handle);
info ??= await htmlGetLiveRoom(link);
if (!info.user.roomId) return info;
const alive = await checkAlive(info.user.roomId);
return alive === null ? info : {
...info,
liveRoom: {
...info.liveRoom,
status: alive ? 2 : 4
}
};
}, config.cache.routeExpire, false);
const status = liveRoom.status;
let title;
switch (status) {
case 2:
title = liveRoom.title || `${userInfo.nickname}'s going live now!`;
break;
case 4:
title = `${userInfo.nickname} is not live currently.`;
break;
default:
title = `${userInfo.nickname}'s live status is unknown (status ${status}).`;
break;
}
const items = [{
title,
pubDate: parseDate(liveRoom.startTime, "X"),
author: userInfo.nickname,
link,
guid: `${userInfo.roomId}:${liveRoom.streamId}:${liveRoom.status}`
}];
return {
title: `${userInfo.nickname} (@${userInfo.uniqueId})'s Live Stream - TikTok`,
description: userInfo.signature,
image: userInfo.avatarLarger || userInfo.avatarMedium || userInfo.avatarThumb,
link,
item: items
};
}
//#endregion
export { route };