from youtube import yt_app from youtube import util, html_common, comments import settings from flask import request import flask from youtube_dl.YoutubeDL import YoutubeDL from youtube_dl.extractor.youtube import YoutubeError import json import html import gevent import os def get_related_items_html(info): result = "" for item in info['related_vids']: if 'list' in item: # playlist: item = watch_page_related_playlist_info(item) result += html_common.playlist_item_html(item, html_common.small_playlist_item_template) else: item = watch_page_related_video_info(item) result += html_common.video_item_html(item, html_common.small_video_item_template) return result # json of related items retrieved directly from the watch page has different names for everything # converts these to standard names def watch_page_related_video_info(item): result = {key: item[key] for key in ('id', 'title', 'author')} result['duration'] = util.seconds_to_timestamp(item['length_seconds']) try: result['views'] = item['short_view_count_text'] except KeyError: result['views'] = '' result['thumbnail'] = util.get_thumbnail_url(item['id']) return result def watch_page_related_playlist_info(item): return { 'size': item['playlist_length'] if item['playlist_length'] != "0" else "50+", 'title': item['playlist_title'], 'id': item['list'], 'first_video_id': item['video_id'], 'thumbnail': util.get_thumbnail_url(item['video_id']), } def get_video_sources(info): video_sources = [] for format in info['formats']: if format['acodec'] != 'none' and format['vcodec'] != 'none': video_sources.append({ 'src': format['url'], 'type': 'video/' + format['ext'], }) return video_sources def get_subtitle_sources(info): sources = [] default_found = False default = None for language, formats in info['subtitles'].items(): for format in formats: if format['ext'] == 'vtt': source = { 'url': '/' + format['url'], 'label': language, 'srclang': language, # set as on by default if this is the preferred language and a default-on subtitles mode is in settings 'on': language == settings.subtitles_language and settings.subtitles_mode > 0, } if language == settings.subtitles_language: default_found = True default = source else: result.append(source) break # Put it at the end to avoid browser bug when there are too many languages # (in firefox, it is impossible to select a language near the top of the list because it is cut off) if default_found: sources.append(default) try: formats = info['automatic_captions'][settings.subtitles_language] except KeyError: pass else: for format in formats: if format['ext'] == 'vtt': sources.append({ 'url': '/' + format['url'], 'label': settings.subtitles_language + ' - Automatic', 'srclang': settings.subtitles_language, # set as on by default if this is the preferred language and a default-on subtitles mode is in settings 'on': settings.subtitles_mode == 2 and not default_found, }) return sources def get_music_list_html(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 = '''
" + attribute + " | \n" music_list_html += '''|
---|---|
''' else: music_list_html += ''' | ''' + html.escape(value) + ''' | ''' music_list_html += '''