fix(route): tiktok user route rewrite (#10066)

This commit is contained in:
Tony
2022-06-29 21:28:50 +05:00
committed by GitHub
parent bdd5352c41
commit 3f3f437641
+22 -55
View File
@@ -1,5 +1,3 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const config = require('@/config').value;
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
@@ -10,80 +8,49 @@ const baseUrl = 'https://www.tiktok.com';
module.exports = async (ctx) => {
const { user } = ctx.params;
let items = await ctx.cache.tryGet(
const data = await ctx.cache.tryGet(
`tiktok:user:${user}`,
async () => {
const browser = await require('@/utils/puppeteer')();
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on('request', (request) => {
// blocking script reduces clips from 30 -> 16
request.resourceType() === 'document' || request.resourceType() === 'script' ? request.continue() : request.abort();
});
await page.goto(`${baseUrl}/${user}`, {
waitUntil: 'networkidle2',
waitUntil: 'domcontentloaded',
});
const html = await page.evaluate(() => document.documentElement.innerHTML);
const SIGI_STATE = await page.evaluate(() => window.SIGI_STATE);
browser.close();
const $ = cheerio.load(html);
const lang = SIGI_STATE.AppContext.lang;
const SharingMeta = SIGI_STATE.SharingMeta;
const ItemModule = SIGI_STATE.ItemModule;
return $('[class*=-DivItemContainerV2]')
.toArray()
.map((item) => ({ link: $(item).find('[class*=-DivWrapper] a').attr('href') }));
return { lang, SharingMeta, ItemModule };
},
config.cache.routeExpire,
false
);
let feedTitle;
let feedDescription;
let feedImage;
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
try {
const videoId = item.link.substring(item.link.lastIndexOf('/') + 1);
const api = (
await got(`${baseUrl}/node/share/video/${user}/${videoId}`, {
headers: {
referer: `${baseUrl}/${user}`,
},
})
).data;
if (!feedTitle) {
feedTitle = `${api.itemInfo.itemStruct.author.nickname} (@${api.itemInfo.itemStruct.author.uniqueId}) TikTok | Watch ${api.itemInfo.itemStruct.author.nickname}'s Newest TikTok Videos`;
}
if (!feedDescription) {
feedDescription = api.itemInfo.itemStruct.author.signature;
}
if (!feedImage) {
feedImage = api.itemInfo.itemStruct.author.avatarLarger || api.itemInfo.itemStruct.author.avatarMedium || api.itemInfo.itemStruct.author.avatarThumb;
}
item.title = api.itemInfo.itemStruct.desc;
item.description = art(path.join(__dirname, 'templates/user.art'), {
poster: api.itemInfo.itemStruct.video.cover,
source: api.itemInfo.itemStruct.video.playAddr,
});
item.author = api.itemInfo.itemStruct.author.nickname;
item.pubDate = parseDate(api.itemInfo.itemStruct.createTime * 1000);
} catch (e) {
item.title = 'Deleted clip';
}
return item;
})
)
);
const items = Object.values(data.ItemModule).map((item) => ({
title: item.desc,
description: art(path.join(__dirname, 'templates/user.art'), {
poster: item.video.cover,
source: item.video.playAddr,
}),
author: item.nickname,
pubDate: parseDate(item.createTime, 'X'),
link: `${baseUrl}/@${item.author}/video/${item.id}`,
category: item.textExtra.map((t) => `#${t.hashtagName}`),
}));
ctx.state.data = {
title: feedTitle,
description: feedDescription,
image: feedImage,
title: data.SharingMeta.value['og:title'],
description: data.SharingMeta.value['og:description'],
image: data.SharingMeta.value['og:image'],
link: `${baseUrl}/${user}`,
item: items,
language: data.lang,
};
};