diff options
Diffstat (limited to 'youtube/__init__.py')
-rw-r--r-- | youtube/__init__.py | 49 |
1 files changed, 42 insertions, 7 deletions
diff --git a/youtube/__init__.py b/youtube/__init__.py index 3c85d47..0072f74 100644 --- a/youtube/__init__.py +++ b/youtube/__init__.py @@ -2,6 +2,7 @@ from youtube import util from .get_app_version import app_version import flask from flask import request +import jinja2 import settings import traceback import re @@ -18,14 +19,14 @@ yt_app.add_url_rule('/settings', 'settings_page', settings.settings_page, method @yt_app.route('/') def homepage(): - return flask.render_template('home.html', title="Youtube local") + return flask.render_template('home.html', title="YT Local") @yt_app.route('/licenses') def licensepage(): return flask.render_template( 'licenses.html', - title="Licenses - YouTube Local" + title="Licenses - YT Local" ) @@ -53,7 +54,10 @@ def commatize(num): if num is None: return '' if isinstance(num, str): - num = int(num) + try: + num = int(num) + except ValueError: + return num return '{:,}'.format(num) @@ -100,7 +104,7 @@ def error_page(e): and exc_info()[1].code == '429' and settings.route_tor ): - error_message = ('Error: Youtube blocked the request because the Tor' + error_message = ('Error: YouTube blocked the request because the Tor' ' exit node is overutilized. Try getting a new exit node by' ' using the New Identity button in the Tor Browser.') if exc_info()[1].error_message: @@ -108,13 +112,30 @@ def error_page(e): if exc_info()[1].ip: error_message += '\n\nExit node IP address: ' + exc_info()[1].ip return flask.render_template('error.html', error_message=error_message, slim=slim), 502 - return flask.render_template('error.html', traceback=traceback.format_exc(), slim=slim), 500 + elif exc_info()[0] == util.FetchError and exc_info()[1].error_message: + return (flask.render_template( + 'error.html', + error_message=exc_info()[1].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(), + error_code=exc_info()[1].code, + slim=slim), 500 + # return flask.render_template('error.html', traceback=traceback.format_exc(), slim=slim), 500 font_choices = { 0: 'initial', - 1: 'arial, "liberation sans", sans-serif', - 2: '"liberation serif", "times new roman", calibri, carlito, serif', + 1: '"liberation serif", "times new roman", calibri, carlito, serif', + 2: 'arial, "liberation sans", sans-serif', 3: 'verdana, sans-serif', 4: 'tahoma, sans-serif', } @@ -129,3 +150,17 @@ def get_css(): ), mimetype='text/css', ) + + +# This is okay because the flask urlize function puts the href as the first +# property +YOUTUBE_LINK_RE = re.compile(r'<a href="(' + util.YOUTUBE_URL_RE_STR + ')"') +old_urlize = jinja2.filters.urlize + + +def prefix_urlize(*args, **kwargs): + result = old_urlize(*args, **kwargs) + return YOUTUBE_LINK_RE.sub(r'<a href="/\1"', result) + + +jinja2.filters.urlize = prefix_urlize |