def get_watch_page(): video_id = request.args['v'] if len(video_id) < 11: flask.abort(404) flask.abort( flask.Response('Incomplete video id (too short): ' + video_id)) lc = request.args.get('lc', '') if settings.route_tor: proxy = 'socks5://127.0.0.1:9150/' else: proxy = '' yt_dl_downloader = YoutubeDL(params={ 'youtube_include_dash_manifest': False, 'proxy': proxy }) tasks = (gevent.spawn(comments.video_comments, video_id, int(settings.default_comment_sorting), lc=lc), gevent.spawn(extract_info, yt_dl_downloader, "https://www.youtube.com/watch?v=" + video_id, download=False)) gevent.joinall(tasks) comments_info, info = tasks[0].value, tasks[1].value if isinstance(info, str): # youtube error return flask.render_template('error.html', error_message=info) video_info = { "duration": util.seconds_to_timestamp(info["duration"]), "id": info['id'], "title": info['title'], "author": info['uploader'], } upload_year = info["upload_date"][0:4] upload_month = info["upload_date"][4:6] upload_day = info["upload_date"][6:8] upload_date = upload_month + "/" + upload_day + "/" + upload_year if settings.related_videos_mode: related_videos = get_related_items(info) else: related_videos = [] if settings.gather_googlevideo_domains: with open(os.path.join(settings.data_dir, 'googlevideo-domains.txt'), 'a+', encoding='utf-8') as f: url = info['formats'][0]['url'] subdomain = url[0:url.find(".googlevideo.com")] f.write(subdomain + "\n") download_formats = [] for format in info['formats']: download_formats.append({ 'url': format['url'], 'ext': format['ext'], 'resolution': yt_dl_downloader.format_resolution(format), 'note': yt_dl_downloader._format_note(format), }) return flask.render_template( 'watch.html', header_playlist_names=local_playlist.get_playlist_names(), uploader_channel_url='/' + info['uploader_url'], upload_date=upload_date, views=(lambda x: '{:,}'.format(x) if x is not None else "")(info.get("view_count", None)), likes=(lambda x: '{:,}'.format(x) if x is not None else "")(info.get("like_count", None)), 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), subtitle_sources=get_subtitle_sources(info), related=related_videos, music_list=info['music_list'], music_attributes=get_ordered_music_list_attributes(info['music_list']), comments_info=comments_info, title=info['title'], uploader=info['uploader'], description=info['description'], unlisted=info['unlisted'], )
def get_watch_page(env, start_response): video_id = env['parameters']['v'][0] if len(video_id) < 11: start_response('404 Not Found', [ ('Content-type', 'text/plain'), ]) return b'Incomplete video id (too short): ' + video_id.encode('ascii') start_response('200 OK', [ ('Content-type', 'text/html'), ]) lc = util.default_multi_get(env['parameters'], 'lc', 0, default='') if settings.route_tor: proxy = 'socks5://127.0.0.1:9150/' else: proxy = '' downloader = YoutubeDL(params={ 'youtube_include_dash_manifest': False, 'proxy': proxy }) tasks = (gevent.spawn(comments.video_comments, video_id, int(settings.default_comment_sorting), lc=lc), gevent.spawn(extract_info, downloader, "https://www.youtube.com/watch?v=" + video_id, download=False)) gevent.joinall(tasks) comments_html, info = tasks[0].value, tasks[1].value #comments_html = comments.comments_html(video_id(url)) #info = YoutubeDL().extract_info(url, download=False) #chosen_format = choose_format(info) if isinstance(info, str): # youtube error return html_common.yt_basic_template.substitute( page_title="Error", style="", header=html_common.get_header(), page=html.escape(info), ).encode('utf-8') sorted_formats = sort_formats(info) video_info = { "duration": util.seconds_to_timestamp(info["duration"]), "id": info['id'], "title": info['title'], "author": info['uploader'], } upload_year = info["upload_date"][0:4] upload_month = info["upload_date"][4:6] upload_day = info["upload_date"][6:8] upload_date = upload_month + "/" + upload_day + "/" + upload_year if settings.enable_related_videos: related_videos_html = get_related_items_html(info) else: related_videos_html = '' music_list = info['music_list'] if len(music_list) == 0: music_list_html = '' else: # get the set of attributes which are used by atleast 1 track # so there isn't an empty, extraneous album column which no tracks use, for example used_attributes = set() for track in music_list: used_attributes = used_attributes | track.keys() # now put them in the right order ordered_attributes = [] for attribute in ('Artist', 'Title', 'Album'): if attribute.lower() in used_attributes: ordered_attributes.append(attribute) music_list_html = '''<hr> <table> <caption>Music</caption> <tr> ''' # table headings for attribute in ordered_attributes: music_list_html += "<th>" + attribute + "</th>\n" music_list_html += '''</tr>\n''' for track in music_list: music_list_html += '''<tr>\n''' for attribute in ordered_attributes: try: value = track[attribute.lower()] except KeyError: music_list_html += '''<td></td>''' else: music_list_html += '''<td>''' + html.escape( value) + '''</td>''' music_list_html += '''</tr>\n''' music_list_html += '''</table>\n''' if settings.gather_googlevideo_domains: with open(os.path.join(settings.data_dir, 'googlevideo-domains.txt'), 'a+', encoding='utf-8') as f: url = info['formats'][0]['url'] subdomain = url[0:url.find(".googlevideo.com")] f.write(subdomain + "\n") download_options = '' for format in info['formats']: download_options += download_link_template.substitute( url=html.escape(format['url']), ext=html.escape(format['ext']), resolution=html.escape(downloader.format_resolution(format)), note=html.escape(downloader._format_note(format)), ) page = yt_watch_template.substitute( video_title=html.escape(info["title"]), page_title=html.escape(info["title"]), header=html_common.get_header(), uploader=html.escape(info["uploader"]), uploader_channel_url='/' + info["uploader_url"], upload_date=upload_date, views=(lambda x: '{:,}'.format(x) if x is not None else "")(info.get("view_count", None)), likes=(lambda x: '{:,}'.format(x) if x is not None else "")(info.get("like_count", None)), dislikes=(lambda x: '{:,}'.format(x) if x is not None else "")(info.get("dislike_count", None)), download_options=download_options, video_info=html.escape(json.dumps(video_info)), description=html.escape(info["description"]), video_sources=formats_html(sorted_formats) + subtitles_html(info), related=related_videos_html, comments=comments_html, music_list=music_list_html, is_unlisted='<span class="is-unlisted">Unlisted</span>' if info['unlisted'] else '', ) return page.encode('utf-8')