aboutsummaryrefslogtreecommitdiffstats
path: root/youtube/local_playlist.py
blob: 926c93fbc381948dc27dc06d881b490751277bdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
import json
from youtube.template import Template
from youtube import common
import html

playlists_directory = os.path.normpath("data/playlists")
with open('yt_local_playlist_template.html', 'r', encoding='utf-8') as file:
    local_playlist_template = Template(file.read())

def add_to_playlist(name, video_info_list):
    with open(os.path.join(playlists_directory, name + ".txt"), "a", encoding='utf-8') as file:
        for info in video_info_list:
            file.write(info + "\n")
        
        
def get_local_playlist_page(name):
    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)
            info['thumbnail'] = common.get_thumbnail_url(info['id'])
            videos_html += common.video_item_html(info, common.small_video_item_template)
        except json.decoder.JSONDecodeError:
            pass
    return local_playlist_template.substitute(
        page_title = name + ' - Local playlist',
        header = common.get_header(),
        videos = videos_html,
        title = name,
        page_buttons = ''
    )

def get_playlist_names():
    for item in os.listdir(playlists_directory):
        name, ext = os.path.splitext(item)
        if ext == '.txt':
            yield name

'''
            main{
                display:grid;
                grid-template-columns: 3fr 1fr;
            }

            header{
                grid-template-columns: 3fr 1fr;
            }

            #left{
                grid-column: 1;
                grid-row: 1;
                
                display: grid;
                grid-template-columns: 1fr 800px;
                grid-template-rows: 0fr 1fr 0fr;
            }

            #right{
                grid-column: 2;
                grid-row: 1;
            }
'''
def get_playlists_list_page():
    page = '''<ul>\n'''
    list_item_template = Template('''    <li><a href="$url">$name</a></li>\n''')
    for name in get_playlist_names():
        page += list_item_template.substitute(url = html.escape(common.URL_ORIGIN + '/playlists/' + name), name = html.escape(name))
    page += '''</ul>\n'''
    return common.yt_basic_template.substitute(
        page_title = "Local playlists",
        header = common.get_header(),
        style = '',
        page = page,
    )


def get_playlist_page(url, query_string=''):
    url = url.rstrip('/').lstrip('/')
    if url == '':
        return get_playlists_list_page()
    else:
        return get_local_playlist_page(url)