Convert playlist page to flask framework
This commit is contained in:
parent
1c724f4f28
commit
d105d4520f
@ -6,7 +6,7 @@ from youtube import yt_app
|
|||||||
from youtube import util
|
from youtube import util
|
||||||
|
|
||||||
# these are just so the files get run - they import yt_app and add routes to it
|
# these are just so the files get run - they import yt_app and add routes to it
|
||||||
from youtube import watch, search
|
from youtube import watch, search, playlist
|
||||||
|
|
||||||
import settings
|
import settings
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from youtube import util, yt_data_extract, html_common, template, proto
|
from youtube import util, yt_data_extract, proto
|
||||||
|
from youtube import yt_app
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
import urllib
|
import urllib
|
||||||
@ -6,10 +7,8 @@ import json
|
|||||||
import string
|
import string
|
||||||
import gevent
|
import gevent
|
||||||
import math
|
import math
|
||||||
|
from flask import request
|
||||||
with open("yt_playlist_template.html", "r") as file:
|
import flask
|
||||||
yt_playlist_template = template.Template(file.read())
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -76,14 +75,15 @@ def get_videos(playlist_id, page):
|
|||||||
return info
|
return info
|
||||||
|
|
||||||
|
|
||||||
playlist_stat_template = string.Template('''
|
@yt_app.route('/playlist')
|
||||||
<div>$stat</div>''')
|
def get_playlist_page():
|
||||||
def get_playlist_page(env, start_response):
|
if 'list' not in request.args:
|
||||||
start_response('200 OK', [('Content-type','text/html'),])
|
abort(400)
|
||||||
parameters = env['parameters']
|
|
||||||
playlist_id = parameters['list'][0]
|
playlist_id = request.args.get('list')
|
||||||
page = parameters.get("page", "1")[0]
|
page = request.args.get('page', '1')
|
||||||
if page == "1":
|
|
||||||
|
if page == '1':
|
||||||
first_page_json = playlist_first_page(playlist_id)
|
first_page_json = playlist_first_page(playlist_id)
|
||||||
this_page_json = first_page_json
|
this_page_json = first_page_json
|
||||||
else:
|
else:
|
||||||
@ -98,26 +98,20 @@ def get_playlist_page(env, start_response):
|
|||||||
video_list = this_page_json['response']['contents']['singleColumnBrowseResultsRenderer']['tabs'][0]['tabRenderer']['content']['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0]['playlistVideoListRenderer']['contents']
|
video_list = this_page_json['response']['contents']['singleColumnBrowseResultsRenderer']['tabs'][0]['tabRenderer']['content']['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0]['playlistVideoListRenderer']['contents']
|
||||||
except KeyError: # other pages
|
except KeyError: # other pages
|
||||||
video_list = this_page_json['response']['continuationContents']['playlistVideoListContinuation']['contents']
|
video_list = this_page_json['response']['continuationContents']['playlistVideoListContinuation']['contents']
|
||||||
videos_html = ''
|
|
||||||
for video_json in video_list:
|
parsed_video_list = [yt_data_extract.parse_info_prepare_for_html(video_json) for video_json in video_list]
|
||||||
info = yt_data_extract.renderer_info(video_json['playlistVideoRenderer'])
|
|
||||||
videos_html += html_common.video_item_html(info, html_common.small_video_item_template)
|
|
||||||
|
|
||||||
|
|
||||||
metadata = yt_data_extract.renderer_info(first_page_json['response']['header']['playlistHeaderRenderer'])
|
metadata = yt_data_extract.renderer_info(first_page_json['response']['header'])
|
||||||
|
yt_data_extract.prefix_urls(metadata)
|
||||||
|
|
||||||
video_count = int(metadata['size'].replace(',', ''))
|
video_count = int(metadata['size'].replace(',', ''))
|
||||||
page_buttons = html_common.page_buttons_html(int(page), math.ceil(video_count/20), util.URL_ORIGIN + "/playlist", env['QUERY_STRING'])
|
metadata['size'] += ' videos'
|
||||||
|
|
||||||
html_ready = html_common.get_html_ready(metadata)
|
return flask.render_template('playlist.html',
|
||||||
html_ready['page_title'] = html_ready['title'] + ' - Page ' + str(page)
|
video_list = parsed_video_list,
|
||||||
|
num_pages = math.ceil(video_count/20),
|
||||||
|
parameters_dictionary = request.args,
|
||||||
|
|
||||||
stats = ''
|
**metadata
|
||||||
stats += playlist_stat_template.substitute(stat=html_ready['size'] + ' videos')
|
|
||||||
stats += playlist_stat_template.substitute(stat=html_ready['views'])
|
|
||||||
return yt_playlist_template.substitute(
|
|
||||||
header = html_common.get_header(),
|
|
||||||
videos = videos_html,
|
|
||||||
page_buttons = page_buttons,
|
|
||||||
stats = stats,
|
|
||||||
**html_ready
|
|
||||||
).encode('utf-8')
|
).encode('utf-8')
|
||||||
|
106
youtube/templates/playlist.html
Normal file
106
youtube/templates/playlist.html
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
{% block page_title %}{{ title + ' - Page ' + parameters_dictionary.get('page', '1') }}{% 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;
|
||||||
|
grid-template-rows: 0fr 1fr 0fr;
|
||||||
|
}
|
||||||
|
.playlist-metadata{
|
||||||
|
grid-column:2;
|
||||||
|
grid-row:1;
|
||||||
|
|
||||||
|
display:grid;
|
||||||
|
grid-template-columns: 0fr 1fr;
|
||||||
|
}
|
||||||
|
.playlist-thumbnail{
|
||||||
|
grid-row: 1 / span 5;
|
||||||
|
grid-column:1;
|
||||||
|
justify-self:start;
|
||||||
|
width:250px;
|
||||||
|
margin-right: 10px;
|
||||||
|
}
|
||||||
|
.playlist-title{
|
||||||
|
grid-row: 1;
|
||||||
|
grid-column:2;
|
||||||
|
}
|
||||||
|
.playlist-author{
|
||||||
|
grid-row:2;
|
||||||
|
grid-column:2;
|
||||||
|
}
|
||||||
|
.playlist-stats{
|
||||||
|
grid-row:3;
|
||||||
|
grid-column:2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.playlist-description{
|
||||||
|
grid-row:4;
|
||||||
|
grid-column:2;
|
||||||
|
min-width:0px;
|
||||||
|
white-space: pre-line;
|
||||||
|
}
|
||||||
|
.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;
|
||||||
|
margin-top:10px;
|
||||||
|
|
||||||
|
display: grid;
|
||||||
|
grid-auto-rows: 0fr;
|
||||||
|
grid-row-gap: 10px;
|
||||||
|
|
||||||
|
}
|
||||||
|
{% endblock style %}
|
||||||
|
|
||||||
|
{% block main %}
|
||||||
|
<div id="left">
|
||||||
|
<div class="playlist-metadata">
|
||||||
|
<img class="playlist-thumbnail" src="{{ thumbnail }}">
|
||||||
|
<h2 class="playlist-title">{{ title }}</h2>
|
||||||
|
<a class="playlist-author" href="{{ author_url }}">{{ author }}</a>
|
||||||
|
<div class="playlist-stats">
|
||||||
|
<div>{{ views }}</div>
|
||||||
|
<div>{{ size }}</div>
|
||||||
|
</div>
|
||||||
|
<div class="playlist-description">{{ description }}</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="results">
|
||||||
|
{% for info in video_list %}
|
||||||
|
{{ common_elements.item(info) }}
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
<nav class="page-button-row">
|
||||||
|
{{ common_elements.page_buttons(num_pages, '/https://www.youtube.com/playlist', parameters_dictionary) }}
|
||||||
|
</nav>
|
||||||
|
</div>
|
||||||
|
{% endblock main %}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -200,12 +200,12 @@ def renderer_info(renderer, additional_info={}):
|
|||||||
|
|
||||||
info.update(additional_info)
|
info.update(additional_info)
|
||||||
|
|
||||||
if type.startswith('compact'):
|
if type.startswith('compact') or type.startswith('playlist') or type.startswith('grid'):
|
||||||
info['item_size'] = 'small'
|
info['item_size'] = 'small'
|
||||||
else:
|
else:
|
||||||
info['item_size'] = 'medium'
|
info['item_size'] = 'medium'
|
||||||
|
|
||||||
if type in ('compactVideoRenderer', 'videoRenderer', 'gridVideoRenderer'):
|
if type in ('compactVideoRenderer', 'videoRenderer', 'playlistVideoRenderer', 'gridVideoRenderer'):
|
||||||
info['type'] = 'video'
|
info['type'] = 'video'
|
||||||
elif type in ('playlistRenderer', 'compactPlaylistRenderer', 'gridPlaylistRenderer',
|
elif type in ('playlistRenderer', 'compactPlaylistRenderer', 'gridPlaylistRenderer',
|
||||||
'radioRenderer', 'compactRadioRenderer', 'gridRadioRenderer',
|
'radioRenderer', 'compactRadioRenderer', 'gridRadioRenderer',
|
||||||
@ -213,6 +213,8 @@ def renderer_info(renderer, additional_info={}):
|
|||||||
info['type'] = 'playlist'
|
info['type'] = 'playlist'
|
||||||
elif type == 'channelRenderer':
|
elif type == 'channelRenderer':
|
||||||
info['type'] = 'channel'
|
info['type'] = 'channel'
|
||||||
|
elif type == 'playlistHeaderRenderer':
|
||||||
|
info['type'] = 'playlist_metadata'
|
||||||
else:
|
else:
|
||||||
info['type'] = 'unsupported'
|
info['type'] = 'unsupported'
|
||||||
return info
|
return info
|
||||||
|
@ -1,115 +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;
|
|
||||||
grid-template-rows: 0fr 1fr 0fr;
|
|
||||||
}
|
|
||||||
.playlist-metadata{
|
|
||||||
grid-column:2;
|
|
||||||
grid-row:1;
|
|
||||||
|
|
||||||
display:grid;
|
|
||||||
grid-template-columns: 0fr 1fr;
|
|
||||||
}
|
|
||||||
.playlist-thumbnail{
|
|
||||||
grid-row: 1 / span 5;
|
|
||||||
grid-column:1;
|
|
||||||
justify-self:start;
|
|
||||||
width:250px;
|
|
||||||
margin-right: 10px;
|
|
||||||
}
|
|
||||||
.playlist-title{
|
|
||||||
grid-row: 1;
|
|
||||||
grid-column:2;
|
|
||||||
}
|
|
||||||
.playlist-author{
|
|
||||||
grid-row:2;
|
|
||||||
grid-column:2;
|
|
||||||
}
|
|
||||||
.playlist-stats{
|
|
||||||
grid-row:3;
|
|
||||||
grid-column:2;
|
|
||||||
}
|
|
||||||
|
|
||||||
.playlist-description{
|
|
||||||
grid-row:4;
|
|
||||||
grid-column:2;
|
|
||||||
min-width:0px;
|
|
||||||
white-space: pre-line;
|
|
||||||
}
|
|
||||||
.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;
|
|
||||||
margin-top:10px;
|
|
||||||
|
|
||||||
display: grid;
|
|
||||||
grid-auto-rows: 0fr;
|
|
||||||
grid-row-gap: 10px;
|
|
||||||
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
$header
|
|
||||||
<main>
|
|
||||||
<div id="left">
|
|
||||||
<div class="playlist-metadata">
|
|
||||||
<img class="playlist-thumbnail" src="$thumbnail">
|
|
||||||
<h2 class="playlist-title">$title</h2>
|
|
||||||
<a class="playlist-author" href="$author_url">$author</a>
|
|
||||||
<div class="playlist-stats">
|
|
||||||
$stats
|
|
||||||
</div>
|
|
||||||
<div class="playlist-description">$description</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<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