diff --git a/lib/v2/douyin/live.js b/lib/v2/douyin/live.js index e22ee7e6b967ca..a6cc9cdfa7603b 100644 --- a/lib/v2/douyin/live.js +++ b/lib/v2/douyin/live.js @@ -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; @@ -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: ``, 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, }); } }