Skip to content

Commit

Permalink
fix(route): douyin live (#13225)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyRL authored Sep 6, 2023
1 parent 02491ad commit bb524fb
Showing 1 changed file with 44 additions and 15 deletions.
59 changes: 44 additions & 15 deletions lib/v2/douyin/live.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const config = require('@/config').value;
const { getOriginAvatar, universalGet } = require('./utils');
const { getOriginAvatar } = require('./utils');
const logger = require('@/utils/logger');

module.exports = async (ctx) => {
const { rid } = ctx.params;
Expand All @@ -9,31 +10,59 @@ module.exports = async (ctx) => {

const pageUrl = `https://live.douyin.com/${rid}`;

const renderData = await ctx.cache.tryGet(`douyin:live:${rid}`, () => universalGet(pageUrl, 'live', ctx), config.cache.routeExpire, false);
const renderData = await ctx.cache.tryGet(
`douyin:live:${rid}`,
async () => {
let roomInfo;
const browser = await require('@/utils/puppeteer')();
const page = await browser.newPage();
await page.setRequestInterception(true);

if (renderData[Object.keys(renderData).find((k) => renderData[k].status_code)].status_code !== '0') {
throw `Status code ${renderData[Object.keys(renderData).find((k) => renderData[k].status_code)].status_code}`;
page.on('request', (request) => {
request.resourceType() === 'document' || request.resourceType() === 'script' || request.resourceType() === 'xhr' ? request.continue() : request.abort();
});
page.on('response', async (response) => {
const request = response.request();
if (request.url().includes('/webcast/room/web/enter')) {
roomInfo = await response.json();
}
});
logger.debug(`Requesting ${pageUrl}`);
await page.goto(pageUrl, {
waitUntil: 'networkidle2',
});
browser.close();

return roomInfo;
},
config.cache.routeExpire,
false
);

if (renderData.status_code !== 0) {
throw Error(`Status code ${renderData.status_code}`);
}

const roomInfo = renderData.app.initialState.roomStore.roomInfo;
const nickname = roomInfo.anchor.nickname;
const userAvatar = roomInfo.anchor.avatar_thumb.url_list[0];
const roomInfo = renderData.data.data[0];
const nickname = roomInfo.owner.nickname;
const userAvatar = roomInfo.owner.avatar_thumb.url_list[0];

const items = [];
if (roomInfo.roomId) {
if (roomInfo.room.status === 2) {
if (roomInfo.id_str) {
if (roomInfo.status === 2) {
items.push({
title: `开播:${roomInfo.room.title}`,
title: `开播:${roomInfo.title}`,
description: `<img src="${roomInfo.cover.url_list[0]}">`,
link: pageUrl,
author: nickname,
guid: roomInfo.roomId, // roomId is unique for each live event
guid: roomInfo.id_str, // roomId is unique for each live event
});
} else if (roomInfo.room.status === 4) {
} else if (roomInfo.status === 4) {
items.push({
title: `当前直播已结束,期待下一场:${roomInfo.room.title}`,
link: `https://www.douyin.com/user/${roomInfo.anchor.sec_uid}`,
title: `当前直播已结束,期待下一场:${roomInfo.title}`,
link: `https://www.douyin.com/user/${roomInfo.owner.sec_uid}`,
author: nickname,
guid: roomInfo.roomId,
guid: roomInfo.id_str,
});
}
}
Expand Down

0 comments on commit bb524fb

Please sign in to comment.