From d9fbf82bb23b848726eace7354444f16e17a36f8 Mon Sep 17 00:00:00 2001
From: James Taylor <user234683@users.noreply.github.com>
Date: Sat, 24 Aug 2019 13:35:11 -0700
Subject: Layout: Add option to use 720p resolution

---
 settings.py                  |  6 ++++++
 youtube/templates/watch.html |  2 +-
 youtube/watch.py             | 14 ++++++++++++--
 3 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/settings.py b/settings.py
index 9253d7c..944576d 100644
--- a/settings.py
+++ b/settings.py
@@ -72,6 +72,12 @@ For security reasons, enabling this is not recommended.''',
         'comment': '',
     }),
 
+    ('default_resolution', {
+        'type': int,
+        'default': 720,
+        'comment': '',
+    }),
+
     ('gather_googlevideo_domains', {
         'type': bool,
         'default': False,
diff --git a/youtube/templates/watch.html b/youtube/templates/watch.html
index 3421059..a555882 100644
--- a/youtube/templates/watch.html
+++ b/youtube/templates/watch.html
@@ -7,7 +7,7 @@
         video{
             grid-column: 1 / span 5;
             width: 100%;
-            max-height: 360px;
+            max-height: {{ video_height }}px;
         }
         #related{
             margin-top: 10px;
diff --git a/youtube/watch.py b/youtube/watch.py
index d9b3cff..ea33d0f 100644
--- a/youtube/watch.py
+++ b/youtube/watch.py
@@ -52,12 +52,17 @@ def watch_page_related_playlist_info(item):
 def get_video_sources(info):
     video_sources = []
     for format in info['formats']:
-        if format['acodec'] != 'none' and format['vcodec'] != 'none':
+        if format['acodec'] != 'none' and format['vcodec'] != 'none' and format['height'] <= settings.default_resolution:
             video_sources.append({
                 'src': format['url'],
                 'type': 'video/' + format['ext'],
+                'height': format['height'],
             })
 
+    #### order the videos sources so the preferred resolution is first ###
+
+    video_sources.sort(key=lambda source: source['height'], reverse=True)
+
     return video_sources
 
 def get_subtitle_sources(info):
@@ -191,6 +196,10 @@ def get_watch_page():
             'note': yt_dl_downloader._format_note(format),
         })
 
+    video_sources = get_video_sources(info)
+    video_height = video_sources[0]['height']
+
+
     return flask.render_template('watch.html',
         header_playlist_names   = local_playlist.get_playlist_names(),
         uploader_channel_url    = '/' + info['uploader_url'],
@@ -200,7 +209,7 @@ def get_watch_page():
         dislikes        = (lambda x: '{:,}'.format(x) if x is not None else "")(info.get("dislike_count", None)),
         download_formats        = download_formats,
         video_info              = json.dumps(video_info),
-        video_sources           = get_video_sources(info),
+        video_sources           = video_sources,
         subtitle_sources        = get_subtitle_sources(info),
         related                 = related_videos,
         music_list              = info['music_list'],
@@ -208,6 +217,7 @@ def get_watch_page():
         comments_info           = comments_info,
 
         theater_mode            = settings.theater_mode,
+        video_height            = video_height,
 
         title       = info['title'],
         uploader    = info['uploader'],
-- 
cgit v1.2.3