aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Taylor <user234683@users.noreply.github.com>2018-07-10 01:49:55 -0700
committerJames Taylor <user234683@users.noreply.github.com>2018-07-10 01:49:55 -0700
commitc7edcff93a165a9e0d9a335f95310d7db54bae96 (patch)
tree8b2b5200c5e74a0f009d9c6e162552cc8357fdfe
parent1134efed496e9bf056948212b24ee85d0cbfdf3b (diff)
downloadyt-local-c7edcff93a165a9e0d9a335f95310d7db54bae96.tar.lz
yt-local-c7edcff93a165a9e0d9a335f95310d7db54bae96.tar.xz
yt-local-c7edcff93a165a9e0d9a335f95310d7db54bae96.zip
add subtitles support
-rw-r--r--youtube/watch.py41
-rw-r--r--youtube/youtube.py6
2 files changed, 42 insertions, 5 deletions
diff --git a/youtube/watch.py b/youtube/watch.py
index 7c11380..47e9894 100644
--- a/youtube/watch.py
+++ b/youtube/watch.py
@@ -114,8 +114,7 @@ _formats = {
-source_tag_template = Template('''
-<source src="$src" type="$type">''')
+
with open("yt_watch_template.html", "r") as file:
yt_watch_template = Template(file.read())
@@ -245,7 +244,43 @@ def choose_format(info):
current_best = (format, video_priority_index)
return current_best[0]
+subtitles_tag_template = Template('''
+<track label="$label" src="$src" kind="subtitles" srclang="$srclang" $default>''')
+def subtitles_html(info):
+ result = ''
+ default_found = False
+ for language, formats in info['subtitles'].items():
+ for format in formats:
+ if format['ext'] == 'vtt':
+ if language == "en":
+ default_found = True
+ result += subtitles_tag_template.substitute(
+ src = html.escape('/' + format['url']),
+ label = html.escape(language),
+ srclang = html.escape(language),
+ default = 'default' if language == 'en' else '',
+ )
+ break
+ try:
+ formats = info['automatic_captions']['en']
+ except KeyError:
+ pass
+ else:
+ for format in formats:
+ if format['ext'] == 'vtt':
+ result += subtitles_tag_template.substitute(
+ src = html.escape('/' + format['url']),
+ label = 'en' + ' - Automatic',
+ srclang = 'en',
+ default = '' if default_found else 'default',
+ )
+ return result
+
+
more_comments_template = Template('''<a class="page-button more-comments" href="$url">More comments</a>''')
+source_tag_template = Template('''
+<source src="$src" type="$type">''')
+
def get_watch_page(query_string):
id = urllib.parse.parse_qs(query_string)['v'][0]
tasks = (
@@ -292,7 +327,7 @@ def get_watch_page(query_string):
dislikes = (lambda x: '{:,}'.format(x) if x is not None else "")(info["dislike_count"]),
video_info = html.escape(json.dumps(video_info)),
description = html.escape(info["description"]),
- video_sources = formats_html(info),
+ video_sources = formats_html(info) + subtitles_html(info),
related = related_videos_html,
comments = comments_html,
more_comments_button = more_comments_button,
diff --git a/youtube/youtube.py b/youtube/youtube.py
index 3b0e845..d2bffed 100644
--- a/youtube/youtube.py
+++ b/youtube/youtube.py
@@ -1,6 +1,6 @@
import mimetypes
import urllib.parse
-from youtube import local_playlist, watch, search, playlist, channel, comments
+from youtube import local_playlist, watch, search, playlist, channel, comments, common
YOUTUBE_FILES = (
"/shared.css",
"/opensearch.xml",
@@ -43,7 +43,9 @@ def youtube(env, start_response):
elif path.startswith("/playlists"):
start_response('200 OK', (('Content-type','text/html'),) )
return local_playlist.get_playlist_page(path[10:], query_string=query_string).encode()
-
+ elif path.startswith("/api/"):
+ start_response('200 OK', () )
+ return common.fetch_url('https://www.youtube.com' + path + ('?' + query_string if query_string else ''))
else:
start_response('404 Not Found', () )
return b'404 Not Found'