Skip to content

Commit

Permalink
local API: Fix playlists and podcasts not appearing in search results (
Browse files Browse the repository at this point in the history
  • Loading branch information
absidue authored Nov 20, 2024
1 parent be02c9f commit 80b4036
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions src/renderer/helpers/api/local.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ClientType, Endpoints, Innertube, Misc, Parser, UniversalCache, Utils, YT } from 'youtubei.js'
import { ClientType, Endpoints, Innertube, Misc, Parser, UniversalCache, Utils, YT, YTNodes } from 'youtubei.js'
import Autolinker from 'autolinker'
import { SEARCH_CHAR_LIMIT } from '../../../constants'

Expand Down Expand Up @@ -770,23 +770,7 @@ export function parseLocalChannelShorts(shorts, channelId, channelName) {
*/
export function parseLocalListPlaylist(playlist, channelId = undefined, channelName = undefined) {
if (playlist.type === 'LockupView') {
/** @type {import('youtubei.js').YTNodes.LockupView} */
const lockupView = playlist

/** @type {import('youtubei.js').YTNodes.ThumbnailOverlayBadgeView} */
const thumbnailOverlayBadgeView = lockupView.content_image.primary_thumbnail.overlays
.find(overlay => overlay.type === 'ThumbnailOverlayBadgeView')

return {
type: 'playlist',
dataSource: 'local',
title: lockupView.metadata.title.text,
thumbnail: lockupView.content_image.primary_thumbnail.image[0].url,
channelName,
channelId,
playlistId: lockupView.content_id,
videoCount: extractNumberFromString(thumbnailOverlayBadgeView.badges[0].text)
}
return parseLockupView(playlist, channelId, channelName)
} else {
let internalChannelName
let internalChannelId = null
Expand Down Expand Up @@ -858,7 +842,7 @@ function handleSearchResponse(response) {

const results = response.results
.filter((item) => {
return item.type === 'Video' || item.type === 'Channel' || item.type === 'Playlist' || item.type === 'HashtagTile' || item.type === 'Movie'
return item.type === 'Video' || item.type === 'Channel' || item.type === 'Playlist' || item.type === 'HashtagTile' || item.type === 'Movie' || item.type === 'LockupView'
})
.map((item) => parseListItem(item))

Expand Down Expand Up @@ -1050,6 +1034,41 @@ export function parseLocalListVideo(item) {
}
}

/**
* @param {import('youtubei.js').YTNodes.LockupView} lockupView
* @param {string | undefined} channelId
* @param {string | undefined} channelName
*/
function parseLockupView(lockupView, channelId = undefined, channelName = undefined) {
switch (lockupView.content_type) {
case 'PLAYLIST':
case 'PODCAST': {
const thumbnailOverlayBadgeView = lockupView.content_image.primary_thumbnail.overlays
.find(overlay => overlay.is(YTNodes.ThumbnailOverlayBadgeView))

const maybeChannelText = lockupView.metadata?.metadata?.metadata_rows?.[0]?.metadata_parts?.[0]?.text

if (maybeChannelText && maybeChannelText.endpoint?.metadata.page_type === 'WEB_PAGE_TYPE_CHANNEL') {
channelName = maybeChannelText.text
channelId = maybeChannelText.endpoint.payload.browseId
}

return {
type: 'playlist',
dataSource: 'local',
playlistId: lockupView.content_id,
title: lockupView.metadata.title.text,
thumbnail: lockupView.content_image.primary_thumbnail.image[0].url,
channelName,
channelId,
videoCount: extractNumberFromString(thumbnailOverlayBadgeView.badges[0].text)
}
}
default:
console.warn(`Unknown lockup content type: ${lockupView.content_type}`, lockupView)
}
}

/**
* @param {import('youtubei.js').Helpers.YTNode} item
*/
Expand Down Expand Up @@ -1109,6 +1128,8 @@ function parseListItem(item) {
case 'Playlist': {
return parseLocalListPlaylist(item)
}
case 'LockupView':
return parseLockupView(item)
}
}

Expand Down

0 comments on commit 80b4036

Please sign in to comment.