fix error handling, null URLs in templates, and Radio playlist support
All checks were successful
git-sync-with-mirror / git-sync (push) Successful in 13s
CI / test (push) Successful in 49s

- Global error handler: friendly messages for 429, 502, 403, 400
  instead of raw tracebacks. Filter FetchError from Flask logger.
- Fix None URLs in templates: protect href/src in common_elements,
  playlist, watch, and comments templates against None values.
- Radio playlists (RD...): redirect /playlist?list=RD... to
  /watch?v=...&list=RD... since YouTube only supports them in player.
- Wrap player client fallbacks (ios, tv_embedded) in try/catch so
  a failed fallback doesn't crash the whole page.
This commit is contained in:
2026-03-27 21:23:03 -05:00
parent 22c72aa842
commit e03f40d728
7 changed files with 85 additions and 45 deletions

View File

@@ -5,6 +5,7 @@ from flask import request
import jinja2 import jinja2
import settings import settings
import traceback import traceback
import logging
import re import re
from sys import exc_info from sys import exc_info
from flask_babel import Babel from flask_babel import Babel
@@ -12,6 +13,15 @@ from flask_babel import Babel
yt_app = flask.Flask(__name__) yt_app = flask.Flask(__name__)
yt_app.config['TEMPLATES_AUTO_RELOAD'] = True yt_app.config['TEMPLATES_AUTO_RELOAD'] = True
yt_app.url_map.strict_slashes = False yt_app.url_map.strict_slashes = False
# Don't log full tracebacks for handled FetchErrors
class FetchErrorFilter(logging.Filter):
def filter(self, record):
if record.exc_info and record.exc_info[0] == util.FetchError:
return False
return True
yt_app.logger.addFilter(FetchErrorFilter())
# yt_app.jinja_env.trim_blocks = True # yt_app.jinja_env.trim_blocks = True
# yt_app.jinja_env.lstrip_blocks = True # yt_app.jinja_env.lstrip_blocks = True
@@ -124,49 +134,54 @@ def timestamps(text):
@yt_app.errorhandler(500) @yt_app.errorhandler(500)
def error_page(e): def error_page(e):
slim = request.args.get('slim', False) # whether it was an ajax request slim = request.args.get('slim', False) # whether it was an ajax request
if (exc_info()[0] == util.FetchError if exc_info()[0] == util.FetchError:
and exc_info()[1].code == '429' fetch_err = exc_info()[1]
and settings.route_tor error_code = fetch_err.code
):
error_message = ('Error: YouTube blocked the request because the Tor' if error_code == '429' and settings.route_tor:
' exit node is overutilized. Try getting a new exit node by' error_message = ('Error: YouTube blocked the request because the Tor'
' using the New Identity button in the Tor Browser.') ' exit node is overutilized. Try getting a new exit node by'
if exc_info()[1].error_message: ' using the New Identity button in the Tor Browser.')
error_message += '\n\n' + exc_info()[1].error_message if fetch_err.error_message:
if exc_info()[1].ip: error_message += '\n\n' + fetch_err.error_message
error_message += '\n\nExit node IP address: ' + exc_info()[1].ip if fetch_err.ip:
return flask.render_template('error.html', error_message=error_message, slim=slim), 502 error_message += '\n\nExit node IP address: ' + fetch_err.ip
elif exc_info()[0] == util.FetchError and exc_info()[1].error_message: return flask.render_template('error.html', error_message=error_message, slim=slim), 502
# Handle specific error codes with user-friendly messages
error_code = exc_info()[1].code elif error_code == '429':
error_msg = exc_info()[1].error_message error_message = ('YouTube is temporarily blocking requests from your IP address (429 Too Many Requests).\n\n'
'Try:\n'
'• Wait a few minutes and refresh\n'
'• Enable Tor routing in Settings for automatic IP rotation\n'
'• Use a VPN to change your IP address')
if fetch_err.ip:
error_message += '\n\nYour IP: ' + fetch_err.ip
return flask.render_template('error.html', error_message=error_message, slim=slim), 429
elif error_code == '502' and ('Failed to resolve' in str(fetch_err) or 'Failed to establish' in str(fetch_err)):
error_message = ('Could not connect to YouTube.\n\n'
'Check your internet connection and try again.')
return flask.render_template('error.html', error_message=error_message, slim=slim), 502
elif error_code == '403':
error_message = ('YouTube blocked this request (403 Forbidden).\n\n'
'Try enabling Tor routing in Settings.')
return flask.render_template('error.html', error_message=error_message, slim=slim), 403
if error_code == '400':
error_message = (f'Error: Bad Request (400)\n\n{error_msg}\n\n'
'This usually means the URL or parameters are invalid. '
'Try going back and trying a different option.')
elif error_code == '404': elif error_code == '404':
error_message = 'Error: The page you are looking for isn\'t here.' error_message = 'Error: The page you are looking for isn\'t here.'
else: return flask.render_template('error.html', error_code=error_code,
error_message = f'Error: {error_code} - {error_msg}' error_message=error_message, slim=slim), 404
else:
# Catch-all for any other FetchError (400, etc.)
error_message = f'Error communicating with YouTube ({error_code}).'
if fetch_err.error_message:
error_message += '\n\n' + fetch_err.error_message
return flask.render_template('error.html', error_message=error_message, slim=slim), 502
return (flask.render_template(
'error.html',
error_message=error_message,
slim=slim
), 502)
elif (exc_info()[0] == util.FetchError
and exc_info()[1].code == '404'
):
error_message = ('Error: The page you are looking for isn\'t here.')
return flask.render_template('error.html',
error_code=exc_info()[1].code,
error_message=error_message,
slim=slim), 404
return flask.render_template('error.html', traceback=traceback.format_exc(), return flask.render_template('error.html', traceback=traceback.format_exc(),
error_code=exc_info()[1].code,
slim=slim), 500 slim=slim), 500
# return flask.render_template('error.html', traceback=traceback.format_exc(), slim=slim), 500
font_choices = { font_choices = {

View File

@@ -78,6 +78,15 @@ def get_playlist_page():
abort(400) abort(400)
playlist_id = request.args.get('list') playlist_id = request.args.get('list')
# Radio/Mix playlists (RD...) only work as watch page, not playlist page
if playlist_id.startswith('RD'):
first_video_id = playlist_id[2:] # video ID after 'RD' prefix
return flask.redirect(
util.URL_ORIGIN + '/watch?v=' + first_video_id + '&list=' + playlist_id,
302
)
page = request.args.get('page', '1') page = request.args.get('page', '1')
if page == '1': if page == '1':

View File

@@ -3,13 +3,13 @@
{% macro render_comment(comment, include_avatar, timestamp_links=False) %} {% macro render_comment(comment, include_avatar, timestamp_links=False) %}
<div class="comment-container"> <div class="comment-container">
<div class="comment"> <div class="comment">
<a class="author-avatar" href="{{ comment['author_url'] }}" title="{{ comment['author'] }}"> <a class="author-avatar" href="{{ comment['author_url'] or '#' }}" title="{{ comment['author'] }}">
{% if include_avatar %} {% if include_avatar %}
<img class="author-avatar-img" alt="{{ comment['author'] }}" src="{{ comment['author_avatar'] }}"> <img class="author-avatar-img" alt="{{ comment['author'] }}" src="{{ comment['author_avatar'] }}">
{% endif %} {% endif %}
</a> </a>
<address class="author-name"> <address class="author-name">
<a class="author" href="{{ comment['author_url'] }}" title="{{ comment['author'] }}">{{ comment['author'] }}</a> <a class="author" href="{{ comment['author_url'] or '#' }}" title="{{ comment['author'] }}">{{ comment['author'] }}</a>
</address> </address>
<a class="permalink" href="{{ comment['permalink'] }}" title="permalink"> <a class="permalink" href="{{ comment['permalink'] }}" title="permalink">
<span>{{ comment['time_published'] }}</span> <span>{{ comment['time_published'] }}</span>

View File

@@ -20,7 +20,7 @@
{{ info['error'] }} {{ info['error'] }}
{% else %} {% else %}
<div class="item-video {{ info['type'] + '-item' }}"> <div class="item-video {{ info['type'] + '-item' }}">
<a class="thumbnail-box" href="{{ info['url'] }}" title="{{ info['title'] }}"> <a class="thumbnail-box" href="{{ info['url'] or '#' }}" title="{{ info['title'] }}">
<div class="thumbnail {% if info['type'] == 'channel' %} channel {% endif %}"> <div class="thumbnail {% if info['type'] == 'channel' %} channel {% endif %}">
{% if lazy_load %} {% if lazy_load %}
<img class="thumbnail-img lazy" alt="&#x20;" data-src="{{ info['thumbnail'] }}" onerror="thumbnail_fallback(this)"> <img class="thumbnail-img lazy" alt="&#x20;" data-src="{{ info['thumbnail'] }}" onerror="thumbnail_fallback(this)">
@@ -35,7 +35,7 @@
{% endif %} {% endif %}
</div> </div>
</a> </a>
<h4 class="title"><a href="{{ info['url'] }}" title="{{ info['title'] }}">{{ info['title'] }}</a></h4> <h4 class="title"><a href="{{ info['url'] or '#' }}" title="{{ info['title'] }}">{{ info['title'] }}</a></h4>
{% if include_author %} {% if include_author %}
{% set author_description = info['author'] %} {% set author_description = info['author'] %}

View File

@@ -10,11 +10,17 @@
<div class="playlist-metadata"> <div class="playlist-metadata">
<div class="author"> <div class="author">
{% if thumbnail %}
<img alt="{{ title }}" src="{{ thumbnail }}"> <img alt="{{ title }}" src="{{ thumbnail }}">
{% endif %}
<h2>{{ title }}</h2> <h2>{{ title }}</h2>
</div> </div>
<div class="summary"> <div class="summary">
{% if author_url %}
<a class="playlist-author" href="{{ author_url }}">{{ author }}</a> <a class="playlist-author" href="{{ author_url }}">{{ author }}</a>
{% else %}
<span class="playlist-author">{{ author }}</span>
{% endif %}
</div> </div>
<div class="playlist-stats"> <div class="playlist-stats">
<div>{{ video_count|commatize }} videos</div> <div>{{ video_count|commatize }} videos</div>

View File

@@ -172,7 +172,11 @@
{% else %} {% else %}
<li>{{ playlist['current_index']+1 }}/{{ playlist['video_count'] }}</li> <li>{{ playlist['current_index']+1 }}/{{ playlist['video_count'] }}</li>
{% endif %} {% endif %}
{% if playlist['author_url'] %}
<li><a href="{{ playlist['author_url'] }}" title="{{ playlist['author'] }}">{{ playlist['author'] }}</a></li> <li><a href="{{ playlist['author_url'] }}" title="{{ playlist['author'] }}">{{ playlist['author'] }}</a></li>
{% elif playlist['author'] %}
<li>{{ playlist['author'] }}</li>
{% endif %}
</ul> </ul>
</div> </div>
<nav class="playlist-videos"> <nav class="playlist-videos">

View File

@@ -431,14 +431,20 @@ def extract_info(video_id, use_invidious, playlist_id=None, index=None):
# Fallback to 'ios' if no valid URLs are found # Fallback to 'ios' if no valid URLs are found
if not info.get('formats') or info.get('player_urls_missing'): if not info.get('formats') or info.get('player_urls_missing'):
print(f"No URLs found in '{primary_client}', attempting with '{fallback_client}'.") print(f"No URLs found in '{primary_client}', attempting with '{fallback_client}'.")
player_response = fetch_player_response(fallback_client, video_id) or {} try:
yt_data_extract.update_with_new_urls(info, player_response) player_response = fetch_player_response(fallback_client, video_id) or {}
yt_data_extract.update_with_new_urls(info, player_response)
except util.FetchError as e:
print(f"Fallback '{fallback_client}' failed: {e}")
# Final attempt with 'tv_embedded' if there are still no URLs # Final attempt with 'tv_embedded' if there are still no URLs
if not info.get('formats') or info.get('player_urls_missing'): if not info.get('formats') or info.get('player_urls_missing'):
print(f"No URLs found in '{fallback_client}', attempting with '{last_resort_client}'") print(f"No URLs found in '{fallback_client}', attempting with '{last_resort_client}'")
player_response = fetch_player_response(last_resort_client, video_id) or {} try:
yt_data_extract.update_with_new_urls(info, player_response) player_response = fetch_player_response(last_resort_client, video_id) or {}
yt_data_extract.update_with_new_urls(info, player_response)
except util.FetchError as e:
print(f"Fallback '{last_resort_client}' failed: {e}")
# signature decryption # signature decryption
if info.get('formats'): if info.get('formats'):