fix(route): youtube playlistId (#11739)

* fix youtube playlistId

* Update lib/v2/youtube/user.js

* Update lib/v2/youtube/user.js

* Update user.js

* Update user.js

---------

Co-authored-by: hanzongwen <hanzongwen@baidu.com>
This commit is contained in:
sohow 2023-02-01 16:57:47 +08:00 committed by GitHub
parent 3a4739338d
commit 4e8a3bb3d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 3 deletions

View File

@ -1,6 +1,8 @@
const utils = require('./utils');
const config = require('@/config').value;
const { parseDate } = require('@/utils/parse-date');
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
if (!config.youtube || !config.youtube.key) {
@ -9,13 +11,23 @@ module.exports = async (ctx) => {
const username = ctx.params.username;
const embed = !ctx.params.embed;
const playlistId = (await utils.getChannelWithUsername(username, 'contentDetails', ctx.cache)).data.items[0].contentDetails.relatedPlaylists.uploads;
let playlistId;
let channelName;
if (username.startsWith('@')) {
const link = `https://www.youtube.com/${username}`;
const response = await got(link);
const $ = cheerio.load(response.data);
const channelId = $('meta[itemprop="channelId"]').attr('content');
channelName = $('meta[itemprop="name"]').attr('content');
playlistId = (await utils.getChannelWithId(channelId, 'contentDetails', ctx.cache)).data.items[0].contentDetails.relatedPlaylists.uploads;
}
playlistId = playlistId || (await utils.getChannelWithUsername(username, 'contentDetails', ctx.cache)).data.items[0].contentDetails.relatedPlaylists.uploads;
const data = (await utils.getPlaylistItems(playlistId, 'snippet', ctx.cache)).data.items;
ctx.state.data = {
title: `${username} - YouTube`,
link: `https://www.youtube.com/user/${username}`,
title: `${channelName || username} - YouTube`,
link: username.startsWith('@') ? `https://www.youtube.com/${username}` : `https://www.youtube.com/user/${username}`,
description: `YouTube user ${username}`,
item: data
.filter((d) => d.snippet.title !== 'Private video' && d.snippet.title !== 'Deleted video')