Skip to content

Commit

Permalink
Fix age restricted videos sometimes not falling back to autogenerated…
Browse files Browse the repository at this point in the history
… thumbnails
  • Loading branch information
ajayyy committed Jan 8, 2024
1 parent 4776722 commit ba1b4fd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/thumbnails/thumbnailData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ interface InnerTubeFormat {
mimeType: string;
}


interface InnerTubeMetadataBase {
duration: number | null;
channelID: string | null;
author: string | null;
isLive: boolean | null;
isUpcoming: boolean | null;
playabilityStatus?: string;
}

interface InnerTubeMetadata extends InnerTubeMetadataBase {
Expand Down Expand Up @@ -71,9 +73,12 @@ export async function fetchVideoMetadata(videoID: VideoID, ignoreCache: boolean)
try {
const result = activeRequests[videoID] ?? (async () => {
let metadata = await fetchVideoDataDesktopClient(videoID).catch(() => null);
if (!onMobile() && (!metadata || metadata.formats.length === 0)) metadata = await fetchVideoDataDesktopClient(videoID).catch(() => null);

if (metadata && metadata.formats.length > 0) {
// Don't retry for LOGIN_REQUIRED, they will never have urls
if (!onMobile() && (!metadata
|| (metadata.formats.length === 0 && metadata.playabilityStatus !== "LOGIN_REQUIRED"))) metadata = await fetchVideoDataDesktopClient(videoID).catch(() => null);

if (metadata) {
let formats = metadata.formats;
if (isSafari()) {
formats = formats.filter((format) => format.mimeType.includes("avc"));
Expand Down Expand Up @@ -192,14 +197,16 @@ export async function fetchVideoDataAndroidClient(videoID: VideoID): Promise<Inn
const author = response?.videoDetails?.author ?? null;
const isLive = response?.videoDetails?.isLive ?? null;
const isUpcoming = response?.videoDetails?.isUpcoming ?? null;
const playabilityStatus = response?.playabilityStatus?.status ?? null;
if (formats) {
return {
formats,
duration,
channelID: channelId,
author,
isLive,
isUpcoming
isUpcoming,
playabilityStatus
};
}
}
Expand Down Expand Up @@ -251,22 +258,23 @@ export async function fetchVideoDataDesktopClient(videoID: VideoID): Promise<Inn
};
}

const formats = response?.streamingData?.adaptiveFormats as InnerTubeFormat[];
const formats = response?.streamingData?.adaptiveFormats as InnerTubeFormat[] || [];
const duration = response?.videoDetails?.lengthSeconds ? parseInt(response.videoDetails.lengthSeconds) : null;
const channelId = response?.videoDetails?.channelId ?? null;
const author = response?.videoDetails?.author ?? null;
const isLive = response?.videoDetails?.isLive ?? null;
const isUpcoming = response?.videoDetails?.isUpcoming ?? null;
if (formats) {
return {
formats,
duration,
channelID: channelId,
author,
isLive,
isUpcoming
};
}
const playabilityStatus = response?.playabilityStatus?.status ?? null;

return {
formats,
duration,
channelID: channelId,
author,
isLive,
isUpcoming,
playabilityStatus
};
}

} catch (e) { } //eslint-disable-line no-empty
Expand Down
1 change: 1 addition & 0 deletions src/thumbnails/thumbnailRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ export async function replaceThumbnail(element: HTMLElement, videoID: VideoID, b
&& !await isLiveOrUpcoming(videoID)) {
await resetToShowAutogenerated(videoID, thumbnail!, image, brandingLocation, displayThumbnail);
} else {
// Will only get here if it did not create a random time thumbnail (such as innertube failing)
resetToShowOriginalThumbnail(image, brandingLocation);
}
}
Expand Down

0 comments on commit ba1b4fd

Please sign in to comment.