Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Enhance thumbnail retriever to support author thumbnails #85

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions lib/utils/format-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,8 @@ function getAuthorThumbnails(videoDetails, sort=true) {
});
}
const authorThumbnails = TypeUtils.isPlainObject(videoDetails.author)
? videoDetails.authorThumbnails
&& Array.isArray(videoDetails.author.thumbnails)
? videoDetails.author.thumbnails
: [];
return (!sort) ? authorThumbnails : sortThumbnailsByResolution(authorThumbnails);
}
Expand Down Expand Up @@ -467,8 +468,8 @@ function getVideoThumbnails(videoDetails, sort=true) {
expectedType: TypeUtils.getType({})
});
}
const videoThumbnails = TypeUtils.isPlainObject(videoDetails.videoThumbnails)
? videoDetails.videoThumbnails
const videoThumbnails = Array.isArray(videoDetails.thumbnails)
? videoDetails.thumbnails
: [];
return (!sort) ? videoThumbnails : sortThumbnailsByResolution(videoThumbnails);
}
Expand Down Expand Up @@ -546,6 +547,7 @@ function getThumbnailByResolution(thumbnails, resolutionType) {

// Get the sorted thumbnails for easier processing
const sortedThumbnails = sortThumbnailsByResolution(thumbnails);
let thumbnail = null;

// Mapping of resolution keys to thumbnail identifiers
const resolutionMapping = {
Expand All @@ -559,15 +561,27 @@ function getThumbnailByResolution(thumbnails, resolutionType) {
const targetKeys = resolutionMapping[resolutionType];
if (Array.isArray(targetKeys)) {
for (const key of targetKeys) {
const thumb = sortedThumbnails.find((thumb) => thumb.url.includes(key));
if (thumb) return thumb;
if (!thumbnail) {
thumbnail = sortedThumbnails.find((thumb) => thumb.url.includes(key));
}
}
return null; // If no fallback available, return null
}

// Standard resolution lookup
return sortedThumbnails.find((thumb) =>
thumb.url.includes(resolutionMapping[resolutionType])) || null;
thumbnail = thumbnail
|| sortedThumbnails.find((thumb) => thumb.url.includes(targetKeys));

// If the thumbnail is still not found, handle the case where the thumbnail is an author thumbnail
if (!thumbnail &&
Object.values(sortedThumbnails).some(t => t.width === t.height
|| /=s[0-9]+(x[0-9]+)?/.test(t.url))
) {
// The author thumbnail has a square resolution and may contain size parameters in the URL
if (resolutionType === 'max') resolutionType = 'high'; // Change the resolution type to 'high'
const resolutionIndex = { low: 0, medium: 1, high: 2 };
thumbnail = sortedThumbnails[resolutionIndex[resolutionType]] || null;
}

return thumbnail || null;
}
FormatUtils.getThumbnailByResolution = getThumbnailByResolution;

Expand Down
Loading