Convert local_playlist to flask framework
This commit is contained in:
parent
c0617670f7
commit
6261add37d
@ -6,7 +6,7 @@ from youtube import yt_app
|
||||
from youtube import util
|
||||
|
||||
# these are just so the files get run - they import yt_app and add routes to it
|
||||
from youtube import watch, search, playlist, channel
|
||||
from youtube import watch, search, playlist, channel, local_playlist
|
||||
|
||||
import settings
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
from youtube.template import Template
|
||||
from youtube import util, html_common
|
||||
from youtube import util, yt_data_extract
|
||||
from youtube import yt_app
|
||||
import settings
|
||||
|
||||
import os
|
||||
@ -8,12 +8,12 @@ import html
|
||||
import gevent
|
||||
import urllib
|
||||
|
||||
import flask
|
||||
from flask import request
|
||||
|
||||
playlists_directory = os.path.join(settings.data_dir, "playlists")
|
||||
thumbnails_directory = os.path.join(settings.data_dir, "playlist_thumbnails")
|
||||
|
||||
with open('yt_local_playlist_template.html', 'r', encoding='utf-8') as file:
|
||||
local_playlist_template = Template(file.read())
|
||||
|
||||
def video_ids_in_playlist(name):
|
||||
try:
|
||||
with open(os.path.join(playlists_directory, name + ".txt"), 'r', encoding='utf-8') as file:
|
||||
@ -62,36 +62,34 @@ def download_thumbnails(playlist_name, ids):
|
||||
|
||||
|
||||
|
||||
def get_local_playlist_page(name):
|
||||
def get_local_playlist_videos(name):
|
||||
try:
|
||||
thumbnails = set(os.listdir(os.path.join(thumbnails_directory, name)))
|
||||
except FileNotFoundError:
|
||||
thumbnails = set()
|
||||
missing_thumbnails = []
|
||||
|
||||
videos_html = ''
|
||||
videos = []
|
||||
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:
|
||||
data = file.read()
|
||||
videos_json = data.splitlines()
|
||||
for video_json in videos_json:
|
||||
try:
|
||||
info = json.loads(video)
|
||||
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'])
|
||||
videos_html += html_common.video_item_html(info, html_common.small_video_item_template)
|
||||
info['item_size'] = 'small'
|
||||
info['type'] = 'video'
|
||||
yt_data_extract.add_extra_html_info(info)
|
||||
videos.append(info)
|
||||
except json.decoder.JSONDecodeError:
|
||||
pass
|
||||
if not video_json.strip() == '':
|
||||
print('Corrupt playlist video entry: ' + video_json)
|
||||
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 = ''
|
||||
)
|
||||
return videos
|
||||
|
||||
def get_playlist_names():
|
||||
try:
|
||||
@ -124,47 +122,39 @@ def remove_from_playlist(name, video_info_list):
|
||||
for file in to_delete:
|
||||
os.remove(os.path.join(thumbnails_directory, name, file))
|
||||
|
||||
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(util.URL_ORIGIN + '/playlists/' + name), name = html.escape(name))
|
||||
page += '''</ul>\n'''
|
||||
return html_common.yt_basic_template.substitute(
|
||||
page_title = "Local playlists",
|
||||
header = html_common.get_header(),
|
||||
style = '',
|
||||
page = page,
|
||||
)
|
||||
|
||||
|
||||
def get_playlist_page(env, start_response):
|
||||
start_response('200 OK', [('Content-type','text/html'),])
|
||||
path_parts = env['path_parts']
|
||||
if len(path_parts) == 1:
|
||||
return get_playlists_list_page().encode('utf-8')
|
||||
@yt_app.route('/playlists', methods=['GET'])
|
||||
@yt_app.route('/playlists/<playlist_name>', methods=['GET'])
|
||||
def get_local_playlist_page(playlist_name=None):
|
||||
if playlist_name is None:
|
||||
playlists = [(name, util.URL_ORIGIN + '/playlists/' + name) for name in get_playlist_names()]
|
||||
return flask.render_template('local_playlists_list.html', playlists=playlists)
|
||||
else:
|
||||
return get_local_playlist_page(path_parts[1]).encode('utf-8')
|
||||
videos = get_local_playlist_videos(playlist_name)
|
||||
return flask.render_template('local_playlist.html',
|
||||
playlist_name = playlist_name,
|
||||
videos = videos,
|
||||
)
|
||||
|
||||
def path_edit_playlist(env, start_response):
|
||||
@yt_app.route('/playlists/<playlist_name>', methods=['POST'])
|
||||
def path_edit_playlist(playlist_name):
|
||||
'''Called when making changes to the playlist from that playlist's page'''
|
||||
parameters = env['parameters']
|
||||
if parameters['action'][0] == 'remove':
|
||||
playlist_name = env['path_parts'][1]
|
||||
remove_from_playlist(playlist_name, parameters['video_info_list'])
|
||||
start_response('303 See Other', [('Location', util.URL_ORIGIN + env['PATH_INFO']),] )
|
||||
return b''
|
||||
|
||||
if request.values['action'] == 'remove':
|
||||
remove_from_playlist(playlist_name, request.values.getlist('video_info_list'))
|
||||
return flask.redirect(util.URL_ORIGIN + request.path)
|
||||
else:
|
||||
start_response('400 Bad Request', [('Content-type', 'text/plain'),])
|
||||
return b'400 Bad Request'
|
||||
flask.abort(400)
|
||||
|
||||
def edit_playlist(env, start_response):
|
||||
@yt_app.route('/edit_playlist', methods=['POST'])
|
||||
def edit_playlist():
|
||||
'''Called when adding videos to a playlist from elsewhere'''
|
||||
parameters = env['parameters']
|
||||
if parameters['action'][0] == 'add':
|
||||
add_to_playlist(parameters['playlist_name'][0], parameters['video_info_list'])
|
||||
start_response('204 No Content', ())
|
||||
if request.values['action'] == 'add':
|
||||
add_to_playlist(request.values['playlist_name'], request.values.getlist('video_info_list'))
|
||||
return '', 204
|
||||
else:
|
||||
start_response('400 Bad Request', [('Content-type', 'text/plain'),])
|
||||
return b'400 Bad Request'
|
||||
flask.abort(400)
|
||||
|
||||
@yt_app.route('/data/playlist_thumbnails/<playlist_name>/<thumbnail>')
|
||||
def serve_thumbnail(playlist_name, thumbnail):
|
||||
# .. is necessary because flask always uses the application directory at ./youtube, not the working directory
|
||||
return flask.send_from_directory(os.path.join('..', thumbnails_directory, playlist_name), thumbnail)
|
||||
|
57
youtube/templates/local_playlist.html
Normal file
57
youtube/templates/local_playlist.html
Normal file
@ -0,0 +1,57 @@
|
||||
{% extends "base.html" %}
|
||||
{% block page_title %}{{ playlist_name + ' - Local playlist' }}{% endblock %}
|
||||
{% import "common_elements.html" as common_elements %}
|
||||
{% block style %}
|
||||
main{
|
||||
display:grid;
|
||||
grid-template-columns: 3fr 1fr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#left{
|
||||
grid-column: 1;
|
||||
grid-row: 1;
|
||||
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 800px auto;
|
||||
grid-template-rows: 0fr 1fr 0fr;
|
||||
}
|
||||
.playlist-title{
|
||||
grid-column:2;
|
||||
}
|
||||
#playlist-remove-button{
|
||||
grid-column:3;
|
||||
align-self: center;
|
||||
white-space: nowrap;
|
||||
}
|
||||
#results{
|
||||
|
||||
grid-row: 2;
|
||||
grid-column: 2 / span 2;
|
||||
|
||||
|
||||
display: grid;
|
||||
grid-auto-rows: 0fr;
|
||||
grid-row-gap: 10px;
|
||||
|
||||
}
|
||||
.page-button-row{
|
||||
grid-row: 3;
|
||||
grid-column: 2;
|
||||
justify-self: center;
|
||||
}
|
||||
{% endblock style %}
|
||||
|
||||
{% block main %}
|
||||
<div id="left">
|
||||
<h2 class="playlist-title">{{ playlist_name }}</h2>
|
||||
<input type="hidden" name="playlist_page" value="{{ playlist_name }}" form="playlist-edit">
|
||||
<button type="submit" id="playlist-remove-button" name="action" value="remove" form="playlist-edit" formaction="">Remove from playlist</button>
|
||||
<div id="results">
|
||||
{% for video_info in videos %}
|
||||
{{ common_elements.item(video_info) }}
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
{% endblock main %}
|
16
youtube/templates/local_playlists_list.html
Normal file
16
youtube/templates/local_playlists_list.html
Normal file
@ -0,0 +1,16 @@
|
||||
{% extends "base.html" %}
|
||||
{% block page_title %}Local playlists{% endblock %}
|
||||
|
||||
{% block main %}
|
||||
<ul>
|
||||
{% for playlist_name, playlist_url in playlists %}
|
||||
<li><a href="{{ playlist_url }}">{{ playlist_name }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endblock main %}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -1,84 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>$page_title</title>
|
||||
<link href="/youtube.com/shared.css" type="text/css" rel="stylesheet">
|
||||
<link href="/youtube.com/favicon.ico" type="image/x-icon" rel="icon">
|
||||
<link title="Youtube local" href="/youtube.com/opensearch.xml" rel="search" type="application/opensearchdescription+xml">
|
||||
<style type="text/css">
|
||||
main{
|
||||
display:grid;
|
||||
grid-template-columns: 3fr 1fr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#left{
|
||||
grid-column: 1;
|
||||
grid-row: 1;
|
||||
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 800px auto;
|
||||
grid-template-rows: 0fr 1fr 0fr;
|
||||
}
|
||||
.playlist-title{
|
||||
grid-column:2;
|
||||
}
|
||||
#playlist-remove-button{
|
||||
grid-column:3;
|
||||
align-self: center;
|
||||
white-space: nowrap;
|
||||
}
|
||||
#results{
|
||||
|
||||
grid-row: 2;
|
||||
grid-column: 2 / span 2;
|
||||
|
||||
|
||||
display: grid;
|
||||
grid-auto-rows: 0fr;
|
||||
grid-row-gap: 10px;
|
||||
|
||||
}
|
||||
.page-button-row{
|
||||
grid-row: 3;
|
||||
grid-column: 2;
|
||||
justify-self: center;
|
||||
}
|
||||
|
||||
|
||||
#right{
|
||||
grid-column: 2;
|
||||
grid-row: 1;
|
||||
|
||||
}
|
||||
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
$header
|
||||
<main>
|
||||
<div id="left">
|
||||
<h2 class="playlist-title">$title</h2>
|
||||
<input type="hidden" name="playlist_page" value="$title" form="playlist-edit">
|
||||
<button type="submit" id="playlist-remove-button" name="action" value="remove" form="playlist-edit" formaction="">Remove from playlist</button>
|
||||
<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