add support for viewing local playlists
This commit is contained in:
parent
f4024573dc
commit
52835db0a8
@ -42,7 +42,8 @@ URL_ORIGIN = "/https://www.youtube.com"
|
||||
# first_video_id
|
||||
|
||||
|
||||
|
||||
with open('yt_basic_template.html', 'r', encoding='utf-8') as file:
|
||||
yt_basic_template = Template(file.read())
|
||||
|
||||
|
||||
|
||||
|
@ -1,7 +1,12 @@
|
||||
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:
|
||||
@ -9,11 +14,74 @@ def add_to_playlist(name, video_info_list):
|
||||
file.write(info + "\n")
|
||||
|
||||
|
||||
def get_playlist_page(name):
|
||||
pass
|
||||
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
|
||||
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)
|
||||
|
||||
|
@ -40,6 +40,10 @@ def youtube(env, start_response):
|
||||
start_response('200 OK', (('Content-type','text/html'),) )
|
||||
return channel.get_user_page(path[6:], query_string=query_string).encode()
|
||||
|
||||
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()
|
||||
|
||||
else:
|
||||
start_response('404 Not Found', () )
|
||||
return b'404 Not Found'
|
||||
|
17
yt_basic_template.html
Normal file
17
yt_basic_template.html
Normal file
@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>$page_title</title>
|
||||
<link href="/youtube.com/shared.css" type="text/css" rel="stylesheet">
|
||||
<style type="text/css">
|
||||
$style
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
$header
|
||||
<main>
|
||||
$page
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
76
yt_local_playlist_template.html
Normal file
76
yt_local_playlist_template.html
Normal file
@ -0,0 +1,76 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>$page_title</title>
|
||||
<link href="/youtube.com/shared.css" type="text/css" rel="stylesheet">
|
||||
<style type="text/css">
|
||||
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;
|
||||
}
|
||||
.playlist-title{
|
||||
grid-column:2;
|
||||
}
|
||||
.page-button-row{
|
||||
grid-row: 3;
|
||||
grid-column: 2;
|
||||
justify-self: center;
|
||||
}
|
||||
|
||||
|
||||
#right{
|
||||
grid-column: 2;
|
||||
grid-row: 1;
|
||||
|
||||
}
|
||||
#results{
|
||||
|
||||
grid-row: 2;
|
||||
grid-column: 2;
|
||||
|
||||
|
||||
display: grid;
|
||||
grid-auto-rows: 0fr;
|
||||
grid-row-gap: 10px;
|
||||
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
$header
|
||||
<main>
|
||||
<div id="left">
|
||||
<h2 class="playlist-title">$title</h2>
|
||||
<div id="results">
|
||||
$videos
|
||||
</div>
|
||||
<nav class="page-button-row">
|
||||
$page_buttons
|
||||
</nav>
|
||||
</div>
|
||||
|
||||
|
||||
<div id="right">
|
||||
|
||||
</div>
|
||||
</main>
|
||||
|
||||
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
Loading…
x
Reference in New Issue
Block a user