Add high resolution thumbnail (if available)#33
Add high resolution thumbnail (if available)#33timonvanhasselt wants to merge 1 commit intoOpenVoiceOS:devfrom
Conversation
Added logic for Google profile pictures next to YouTube thumbnails. Maybe there's a better way to receive the higher res instead of the 120px profile pictures the api returns.
WalkthroughIntroduces a helper method Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
__init__.py(4 hunks)
🔇 Additional comments (3)
__init__.py (3)
3-3: LGTM!The
reimport is necessary for the regex operations in the new_get_high_res_thumbnailmethod.
137-138: LGTM!Correct usage of the helper method for playlist track thumbnails. The fallback behavior is maintained since
_get_high_res_thumbnailreturns the original URL when no transformation applies.
158-159: LGTM!Correct usage of the helper method for individual result thumbnails, consistent with the playlist track handling.
| # Helper function to try and get max resolution thumbnail | ||
| def _get_high_res_thumbnail(self, url): | ||
| """ | ||
| Replaces a lower resolution thumbnail ID in the URL with 'maxresdefault'. | ||
| If no known low-resolution ID is found, the original URL is returned (fallback). | ||
| Also updates googleusercontent profile picture size to 800x800 for high resolution. | ||
| """ | ||
| if not url: | ||
| return url | ||
|
|
||
| original_url = url | ||
| desired_size_param = "=w800-h800-l90-rj" | ||
|
|
||
| # NEW LOGIC: Adjust googleusercontent profile pictures for high resolution | ||
| if "googleusercontent.com/profile/picture" in original_url: | ||
|
|
||
| # 1. Try to replace an existing size parameter (e.g., =w120-h120-l90-rj) | ||
| # The pattern looks for "=w<digits>-h<digits>-l90-rj" | ||
| new_url = re.sub(r'=w\d+-h\d+-l90-rj', desired_size_param, original_url) | ||
|
|
||
| # 2. If replacement didn't happen and the desired parameter is not already present, | ||
| # append it to the end of the URL. | ||
| if new_url == original_url and desired_size_param not in original_url: | ||
| # Check if the URL ends with a number (e.g., '.../picture/2'), which is common | ||
| if re.search(r'/\d+$', original_url): | ||
| return original_url + desired_size_param | ||
|
|
||
| return new_url | ||
|
|
||
| # Existing logic: Try high resolution standard YouTube thumbnail | ||
| for quality in ["default", "hqdefault", "mqdefault", "sddefault"]: | ||
| if f"/{quality}.jpg" in original_url: | ||
| # Returns the URL with 'maxresdefault.jpg' | ||
| return original_url.replace(f"/{quality}.jpg", "/maxresdefault.jpg") | ||
|
|
||
| # Fallback: Return original URL if no replacement was made | ||
| return original_url |
There was a problem hiding this comment.
🧩 Analysis chain
Verify maxresdefault.jpg availability causes graceful degradation.
The helper method looks well-structured, but there's a significant concern: maxresdefault.jpg doesn't exist for all YouTube videos. When a video lacks this high-resolution thumbnail, the image URL will return a 404, potentially causing broken images in the UI.
Consider:
- Verifying whether the media player/UI gracefully handles missing thumbnails
- Implementing a fallback mechanism or checking thumbnail availability
- Starting with
hqdefaultas a safer default that exists for all videos
For the Google profile picture logic, the assumption that appending =w800-h800-l90-rj after a numeric path segment is valid seems reasonable, but should be validated against actual API behavior.
Run the following script to check if there are any error handling mechanisms for failed image loads:
🏁 Script executed:
#!/bin/bash
# Description: Search for image error handling in the codebase
# Check if MediaEntry or related classes handle image loading errors
rg -n "image.*error|thumbnail.*error|fallback.*image" --type=py
# Look for any error handling around image URLs
rg -n "MediaEntry" -A 10 -B 5 --type=py | rg -i "error|exception|fallback"Length of output: 185
---
🏁 Script executed:
```shell
#!/bin/bash
# Get context around lines 137 and 158 to understand the fallback mechanism
sed -n '130,165p' __init__.py
Length of output: 1727
🏁 Script executed:
#!/bin/bash
# Search for how thumbnail URLs are actually used (in display/rendering)
rg -n "thumbnail|_get_high_res_thumbnail" -A 5 -B 2 --type=pyLength of output: 2252
🏁 Script executed:
#!/bin/bash
# Check for any image onerror handlers or error recovery mechanisms
rg -n "onerror|onload|image.*failed|src.*error" --type=py --type=js --type=html -C 3Length of output: 0
---
🌐 Web query:
```web
YouTube video thumbnail maxresdefault.jpg availability which thumbnails always exist
💡 Result:
Short answer: maxresdefault.jpg is NOT guaranteed to exist. The only thumbnail URLs you can generally rely on are the low/medium/high default images (default.jpg, mqdefault.jpg, hqdefault.jpg) and the frame indexes (0.jpg, 1.jpg, 2.jpg, 3.jpg). sddefault and maxres (maxresdefault.jpg) are optional and may 404 for many videos; the YouTube Data API lists standard and maxres as “available for some videos.” Use a fallback chain (maxres → sd → hq → mq → default/0) to ensure an image. [1][2][5]
Sources:
- YouTube Data API thumbnails docs (maxres is optional). [1]
- Community/FAQ and developer reports showing maxresdefault may 404; recommend fallbacks. [0][2]
Implement proper thumbnail fallback chain to prevent 404 broken images.
maxresdefault.jpg is not guaranteed to exist; YouTube Data API documentation indicates that maxres thumbnails are optional and available for only some videos. The current implementation at line 69 replaces any low-resolution thumbnail identifier with maxresdefault.jpg, which will fail for the majority of videos, causing broken images with no recovery mechanism.
The only thumbnail URLs you can reliably depend on are default.jpg, mqdefault.jpg, and hqdefault.jpg. Revise the logic to use a fallback chain starting with hqdefault instead of maxresdefault, or implement a complete chain (maxres → sd → hq → mq → default) to ensure graceful degradation.
The googleusercontent profile picture logic is sound.
🤖 Prompt for AI Agents
In __init__.py around lines 36 to 72, the current logic bluntly replaces known
low-res YouTube thumbnail filenames with "maxresdefault.jpg" which leads to 404s
because maxres may not exist; change the replacement to use a fallback chain
instead: when a URL contains a known quality (default, mqdefault, hqdefault,
sddefault), generate replacement candidates in order (maxresdefault → sddefault
→ hqdefault → mqdefault → default) and return the first candidate produced by
replacing the found quality segment; alternatively, prefer starting with
"hqdefault" if you want a simpler safer default, but do not return straight to
maxres without attempting the subsequent fallbacks. Ensure existing
googleusercontent profile picture logic remains unchanged.
Added logic for Google profile pictures next to YouTube thumbnails. Maybe there's a better way to receive the higher res instead of the 120px profile pictures the api returns.
Summary by CodeRabbit