Esempio n. 1
0
def get_local_playlist_videos(name, offset=0, amount=50):
    try:
        thumbnails = set(os.listdir(os.path.join(thumbnails_directory, name)))
    except FileNotFoundError:
        thumbnails = set()
    missing_thumbnails = []

    videos = []
    with open(os.path.join(playlists_directory, name + ".txt"),
              'r',
              encoding='utf-8') as file:
        data = file.read()
    videos_json = data.splitlines()
    for video_json in videos_json:
        try:
            info = json.loads(video_json)
            if info['id'] + ".jpg" in thumbnails:
                info[
                    'thumbnail'] = "/youtube.com/data/playlist_thumbnails/" + name + "/" + info[
                        'id'] + ".jpg"
            else:
                info['thumbnail'] = util.get_thumbnail_url(info['id'])
                missing_thumbnails.append(info['id'])
            info['item_size'] = 'small'
            info['type'] = 'video'
            yt_data_extract.add_extra_html_info(info)
            videos.append(info)
        except json.decoder.JSONDecodeError:
            if not video_json.strip() == '':
                print('Corrupt playlist video entry: ' + video_json)
    gevent.spawn(download_thumbnails, name, missing_thumbnails)
    return videos[offset:offset + amount], len(videos)
Esempio n. 2
0
def get_local_playlist_page(name):
    try:
        thumbnails = set(os.listdir(os.path.join(thumbnails_directory, name)))
    except FileNotFoundError:
        thumbnails = set()
    missing_thumbnails = []

    videos_html = ''
    with open(os.path.join(playlists_directory, name + ".txt"), 'r', encoding='utf-8') as file:
        videos = file.read()
    videos = videos.splitlines()
    for video in videos:
        try:
            info = json.loads(video)
            if info['id'] + ".jpg" in thumbnails:
                info['thumbnail'] = "/youtube.com/data/playlist_thumbnails/" + name + "/" + info['id'] + ".jpg"
            else:
                info['thumbnail'] = util.get_thumbnail_url(info['id'])
                missing_thumbnails.append(info['id'])
            videos_html += html_common.video_item_html(info, html_common.small_video_item_template)
        except json.decoder.JSONDecodeError:
            pass
    gevent.spawn(download_thumbnails, name, missing_thumbnails)
    return local_playlist_template.substitute(
        page_title = name + ' - Local playlist',
        header = html_common.get_header(),
        videos = videos_html,
        title = name,
        page_buttons = ''
    )
Esempio n. 3
0
def add_extra_info_to_videos(videos, playlist_name):
    '''Adds extra information necessary for rendering the video item HTML

    Downloads missing thumbnails'''
    try:
        thumbnails = set(
            os.listdir(os.path.join(thumbnails_directory, playlist_name)))
    except FileNotFoundError:
        thumbnails = set()
    missing_thumbnails = []

    for video in videos:
        video['type'] = 'video'
        util.add_extra_html_info(video)
        if video['id'] + '.jpg' in thumbnails:
            video['thumbnail'] = (
                '/https://youtube.com/data/playlist_thumbnails/' +
                playlist_name + '/' + video['id'] + '.jpg')
        else:
            video['thumbnail'] = util.get_thumbnail_url(video['id'])
            missing_thumbnails.append(video['id'])

    gevent.spawn(util.download_thumbnails,
                 os.path.join(thumbnails_directory, playlist_name),
                 missing_thumbnails)
Esempio n. 4
0
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']),
    }
Esempio n. 5
0
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