Add pagination

This commit is contained in:
metalune 2021-01-23 15:20:10 +01:00
parent 051272a874
commit 2a8ee4988a
14 changed files with 198 additions and 78 deletions

135
main.py
View file

@ -1,5 +1,6 @@
from quart import Quart, request, render_template, redirect
from datetime import datetime
from math import ceil
import peertube
commit = "not found"
@ -115,13 +116,21 @@ async def simpleer_search_redirect():
query = (await request.form)["query"]
return redirect("/search/" + query)
@app.route("/search/<string:query>")
async def simpleer_search(query):
@app.route("/search/<string:query>", defaults = {"page": 1})
@app.route("/search/<string:query>/<int:page>")
async def simpleer_search(query, page):
results = peertube.sepia_search(query, (page - 1) * 10)
return await render_template(
"simpleer_search_results.html",
commit=commit,
results=peertube.sepia_search(query)
results = results,
query = query,
# details for pagination
page=page,
pages_total=ceil(results["total"] / 10),
)
@ -130,71 +139,106 @@ async def simpleer_search(query):
async def instance(domain):
return redirect("/" + domain + "/videos/trending")
@app.route("/<string:domain>/videos/local")
async def instance_videos_local(domain):
@app.route("/<string:domain>/videos/local", defaults = {"page": 1})
@app.route("/<string:domain>/videos/local/<int:page>")
async def instance_videos_local(domain, page):
vids = peertube.get_videos_local(domain, (page - 1) * 10)
return await render_template(
"instance/local.html",
domain=domain,
instance_name=get_instance_name(domain),
commit=commit,
videos = peertube.get_videos_local(domain),
videos = vids,
# details for pagination
page=page,
pagination_url="/" + domain + "/videos/local/",
pages_total=ceil(vids["total"] / 10),
)
@app.route("/<string:domain>/videos/trending")
async def instance_videos_trending(domain):
@app.route("/<string:domain>/videos/trending", defaults = {"page": 1})
@app.route("/<string:domain>/videos/trending/<int:page>")
async def instance_videos_trending(domain, page):
vids = peertube.get_videos_trending(domain, (page - 1) * 10)
return await render_template(
"instance/trending.html",
domain=domain,
instance_name=get_instance_name(domain),
commit=commit,
videos = peertube.get_videos_trending(domain),
videos = vids,
# details for pagination
page=page,
pagination_url="/" + domain + "/videos/trending/",
pages_total=ceil(vids["total"] / 10),
)
@app.route("/<string:domain>/videos/most-liked")
async def instance_videos_most_liked(domain):
@app.route("/<string:domain>/videos/most-liked", defaults = {"page": 1})
@app.route("/<string:domain>/videos/most-liked/<int:page>")
async def instance_videos_most_liked(domain, page):
vids = peertube.get_videos_most_liked(domain, (page - 1) * 10)
return await render_template(
"instance/most-liked.html",
domain=domain,
instance_name=get_instance_name(domain),
commit=commit,
videos = peertube.get_videos_most_liked(domain),
videos = vids,
# details for pagination
page=page,
pagination_url="/" + domain + "/videos/most-liked/",
pages_total=ceil(vids["total"] / 10),
)
@app.route("/<string:domain>/videos/recently-added")
async def instance_videos_recently_added(domain):
@app.route("/<string:domain>/videos/recently-added", defaults = {"page": 1})
@app.route("/<string:domain>/videos/recently-added/<int:page>")
async def instance_videos_recently_added(domain, page):
vids = peertube.get_videos_recently_added(domain, (page - 1) * 10)
return await render_template(
"instance/recently-added.html",
domain=domain,
instance_name=get_instance_name(domain),
commit=commit,
videos = peertube.get_videos_recently_added(domain),
videos = vids,
# details for pagination
page=page,
pagination_url="/" + domain + "/videos/recently-added/",
pages_total=ceil(vids["total"] / 10),
)
@app.route("/<string:domain>/search", methods=["POST"])
async def search_redirect(domain):
query = (await request.form)["query"]
return redirect("/" + domain + "/search/" + query)
@app.route("/<string:domain>/search/<string:term>")
async def search(domain, term):
amount, results = peertube.search(domain, term)
@app.route("/<string:domain>/search/<string:term>", defaults = {"page": 1})
@app.route("/<string:domain>/search/<string:term>/<int:page>")
async def search(domain, term, page):
results = peertube.search(domain, term, (page - 1) * 10)
return await render_template(
"search_results.html",
domain=domain,
instance_name=get_instance_name(domain),
commit=commit,
amount=amount,
results=results,
search_term=term,
# details for pagination
page=page,
pagination_url="/" + domain + "/search/" + term + "/",
pages_total=(results["total"] / 10)
)
@ -233,11 +277,13 @@ def build_channel_or_account_name(domain, name):
# --- Accounts ---
@app.route("/<string:domain>/accounts/<string:name>")
async def accounts(domain, name):
async def accounts_redirect(domain, name):
return redirect("/" + domain + "/accounts/" + name + "/video-channels")
@app.route("/<string:domain>/accounts/<string:name>/video-channels")
async def account__video_channels(domain, name):
@app.route("/<string:domain>/accounts/<string:name>/video-channels", defaults = {"page": 1})
@app.route("/<string:domain>/accounts/<string:name>/video-channels/<int:page>")
async def account__video_channels(domain, name, page):
video_channels = peertube.account_video_channels(domain, name, (page - 1) * 10)
return await render_template(
"accounts/video_channels.html",
domain=domain,
@ -246,11 +292,18 @@ async def account__video_channels(domain, name):
name = name,
account = get_account_info(build_channel_or_account_name(domain, name)),
video_channels = peertube.account_video_channels(domain, name)
video_channels = video_channels,
# details for pagination
page=page,
pagination_url="/" + domain + "/accounts/" + name + "/video-channels/",
pages_total=ceil(video_channels["total"] / 10)
)
@app.route("/<string:domain>/accounts/<string:name>/videos")
async def account__videos(domain, name):
@app.route("/<string:domain>/accounts/<string:name>/videos", defaults = {"page": 1})
@app.route("/<string:domain>/accounts/<string:name>/videos/<int:page>")
async def account__videos(domain, name, page):
vids = peertube.account_videos(domain, name, (page - 1) * 10)
return await render_template(
"accounts/videos.html",
domain=domain,
@ -259,7 +312,12 @@ async def account__videos(domain, name):
name = name,
account = get_account_info(build_channel_or_account_name(domain, name)),
videos = peertube.account_videos(domain, name)
videos = vids,
# details for pagination
page=page,
pagination_url="/" + domain + "/accounts/" + name + "/videos/",
pages_total=ceil(vids["total"] / 10)
)
@app.route("/<string:domain>/accounts/<string:name>/about")
@ -278,11 +336,13 @@ async def account__about(domain, name):
# --- Video-Channels ---
@app.route("/<string:domain>/video-channels/<string:name>")
async def video_channels(domain, name):
async def video_channels_redirect(domain, name):
return redirect("/" + domain + "/video-channels/" + name + "/videos")
@app.route("/<string:domain>/video-channels/<string:name>/videos")
async def video_channels__videos(domain, name):
@app.route("/<string:domain>/video-channels/<string:name>/videos", defaults = {"page": 1})
@app.route("/<string:domain>/video-channels/<string:name>/videos/<int:page>")
async def video_channels__videos(domain, name, page):
vids = peertube.video_channel_videos(domain, name, (page - 1) * 10)
return await render_template(
"video_channels/videos.html",
domain=domain,
@ -291,11 +351,16 @@ async def video_channels__videos(domain, name):
name = name,
video_channel = get_video_channel_info(build_channel_or_account_name(domain, name)),
videos = peertube.video_channel_videos(domain, name)
page=page,
pagination_url="/" + domain + "/video-channels/" + name + "/videos/",
pages_total=ceil(vids["total"] / 10),
videos = vids,
)
@app.route("/<string:domain>/video-channels/<string:name>/video-playlists")
async def video_channels__video_playlists(domain, name):
@app.route("/<string:domain>/video-channels/<string:name>/video-playlists", defaults = {"page": 1})
@app.route("/<string:domain>/video-channels/<string:name>/video-playlists/<int:page>")
async def video_channels__video_playlists(domain, name, page):
video_playlists = peertube.video_channel_video_playlists(domain, name, (page - 1) * 10)
return await render_template(
"video_channels/video_playlists.html",
domain=domain,
@ -304,7 +369,11 @@ async def video_channels__video_playlists(domain, name):
name = name,
video_channel = get_video_channel_info(build_channel_or_account_name(domain, name)),
video_playlists = peertube.video_channel_video_playlists(domain, name)
video_playlists = video_playlists,
page=page,
pagination_url="/" + domain + "/video-channels/" + name + "/video-playlists/",
pages_total=ceil(video_playlists["total"] / 10)
)
@app.route("/<string:domain>/video-channels/<string:name>/about")

View file

@ -3,8 +3,8 @@ import requests
import json
# --- Sepiasearch ---
def sepia_search(query):
url = "https://search.joinpeertube.org/api/v1/search/videos?search=" + query
def sepia_search(query, start=0, count=10):
url = "https://search.joinpeertube.org/api/v1/search/videos?search=" + query + "&start=" + str(start) + "&count=" + str(count)
return json.loads(requests.get(url).text)
# --- ----
@ -19,47 +19,41 @@ def get_instance_name(domain):
return "PeerTube Instance"
def video(domain, id):
video_url = "https://" + domain + "/api/v1/videos/" + id
video_object = json.loads(requests.get(video_url).text)
return video_object
url = "https://" + domain + "/api/v1/videos/" + id
return json.loads(requests.get(url).text)
def search(domain, term, start=0, count=10):
search_url = "https://" + domain + "/api/v1/search/videos?start=" + str(start) + "&count=" + str(count) + "&search=" + term + "&sort=-match&searchTarget=local"
search_object = json.loads(requests.get(search_url).text)
amount = search_object["total"]
results = search_object["data"]
return amount, results
url = "https://" + domain + "/api/v1/search/videos?start=" + str(start) + "&count=" + str(count) + "&search=" + term + "&sort=-match&searchTarget=local"
return json.loads(requests.get(url).text)
def get_comments(domain, id):
url = "https://" + domain + "/api/v1/videos/" + id + "/comment-threads"
return json.loads(requests.get(url).text)
def get_videos_trending(domain):
url = "https://" + domain + "/api/v1/videos?sort=-trending"
def get_videos_trending(domain, start=0, count=10):
url = "https://" + domain + "/api/v1/videos?sort=-trending&start=" + str(start) + "&count=" + str(count)
return json.loads(requests.get(url).text)
def get_videos_most_liked(domain):
url = "https://" + domain + "/api/v1/videos?sort=-likes"
def get_videos_most_liked(domain, start=0, count=10):
url = "https://" + domain + "/api/v1/videos?sort=-likes&start=" + str(start) + "&count=" + str(count)
return json.loads(requests.get(url).text)
def get_videos_recently_added(domain):
url = "https://" + domain + "/api/v1/videos?sort=-publishedAt"
def get_videos_recently_added(domain, start=0, count=10):
url = "https://" + domain + "/api/v1/videos?sort=-publishedAt&start=" + str(start) + "&count=" + str(count)
return json.loads(requests.get(url).text)
def get_videos_local(domain):
url = "https://" + domain + "/api/v1/videos?sort=-publishedAt&filter=local"
def get_videos_local(domain, start=0, count=10):
url = "https://" + domain + "/api/v1/videos?sort=-publishedAt&filter=local&start=" + str(start) + "&count=" + str(count)
return json.loads(requests.get(url).text)
# --- Accounts ---
def account_video_channels(domain, name):
url = "https://" + domain + "/api/v1/accounts/" + name + "/video-channels"
def account_video_channels(domain, name, start=0, count=10):
url = "https://" + domain + "/api/v1/accounts/" + name + "/video-channels?start=" + str(start) + "&count=" + str(count)
return json.loads(requests.get(url).text)
def account_videos(domain, name):
url = "https://" + domain + "/api/v1/accounts/" + name + "/videos"
def account_videos(domain, name, start=0, count=10):
url = "https://" + domain + "/api/v1/accounts/" + name + "/videos?start=" + str(start) + "&count=" + str(count)
return json.loads(requests.get(url).text)
def account(domain, name):
@ -68,12 +62,12 @@ def account(domain, name):
# --- Video Channels ---
def video_channel_videos(domain, name):
url = "https://" + domain + "/api/v1/video-channels/" + name + "/videos"
def video_channel_videos(domain, name, start=0, count=10):
url = "https://" + domain + "/api/v1/video-channels/" + name + "/videos?start=" + str(start) + "&count=" + str(count)
return json.loads(requests.get(url).text)
def video_channel_video_playlists(domain, name):
url = "https://" + domain + "/api/v1/video-channels/" + name + "/video-playlists"
def video_channel_video_playlists(domain, name, start=0, count=10):
url = "https://" + domain + "/api/v1/video-channels/" + name + "/video-playlists?start=" + str(start) + "&count=" + str(count)
return json.loads(requests.get(url).text)
def video_channel(domain, name):

View file

@ -2,7 +2,7 @@
{% block title %}{{ domain }}{% endblock %}
{% block sub_content %}
{% block content %}
Coming Soon!

View file

@ -1,6 +1,6 @@
{% extends "base.html" %}
{% block content %}
{% block head_content %}
<table>
<tr>
<td>
@ -24,6 +24,4 @@
<br>
<br>
{% block sub_content %}{% endblock %}
{% endblock %}

View file

@ -2,7 +2,7 @@
{% block title %}{{ domain }}{% endblock %}
{% block sub_content %}
{% block content %}
{{ video_channels.total }} Channels
<br>

View file

@ -2,7 +2,7 @@
{% block title %}{{ domain }}{% endblock %}
{% block sub_content %}
{% block content %}
{{ videos.total }} Videos
<br>

View file

@ -24,9 +24,40 @@
<b> | </b>
<a href="/{{ domain }}/videos/recently-added">Recently Added</a>
<br>
<br>
{% block head_content %}{% endblock %}
{% if pagination_url %}
{% if pages_total > 1 %}
{% if page > 1 %}
<a href="{{ pagination_url }}{{ page - 1 }}">Previous</a>
<b> | </b>
{% endif %}
Page {{ page }} of {{ pages_total }}
{% if page < pages_total %}
<b> | </b>
<a href="{{ pagination_url }}{{ page + 1}}">Next</a>
{% endif %}
{% endif %}
{% endif %}
<br>
{% block content %}{% endblock %}
{% if pagination_url %}
{% if pages_total > 1 %}
{% if page > 1 %}
<a href="{{ pagination_url }}{{ page - 1 }}">Previous</a>
<b> | </b>
{% endif %}
Page {{ page }} of {{ pages_total }}
{% if page < pages_total %}
<b> | </b>
<a href="{{ pagination_url }}{{ page + 1}}">Next</a>
{% endif %}
{% endif %}
{% endif %}
<footer>
<a href="https://codeberg.org/simple-web/peertube/src/commit/{{ commit }}">Commit: {{ commit }}</a>
</footer>

View file

@ -8,7 +8,7 @@
{% for video in videos.data %}
<tr>
<td>
<img src="https://{{ domain }}/{{ video.thumbnailPath }}" height="150"/>
<img src="/{{ domain }}/{{ video.thumbnailPath }}" height="150"/>
</td>
<td>
<a href="/{{ domain }}/videos/watch/{{ video.uuid }}">{{ video.name }}</a>

View file

@ -3,10 +3,10 @@
{% block title %}{{ search_term }} - {{ domain }}{% endblock %}
{% block content %}
<p>{{ amount }} results</p>
<p>{{ results.total }} results</p>
<table>
{% for result in results %}
{% for result in results.data %}
<tr>
<td>
<a href="/{{ domain }}/videos/watch/{{ result.uuid }}">

View file

@ -6,11 +6,27 @@
<body>
<form action="/search" method="POST">
<label for="query"><b>SimpleerTube</b></label>
<input type="text" name="query" id="query" placeholder="Search"/>
<input type="text" name="query" id="query" placeholder="Search" value="{{ query }}"/>
<button type="submit">Search</button>
</form>
<br>
<br>
{% if pages_total > 1 %}
{% if page > 1 %}
<a href="/search/{{ query }}/{{ page - 1 }}">Previous</a>
<b> | </b>
{% endif %}
Page {{ page }} of {{ pages_total }}
{% if page < pages_total %}
<b> | </b>
<a href="/search/{{ query }}/{{ page + 1}}">Next</a>
{% endif %}
{% endif %}
<br>
<br>
{{ results.total }} Results
<table>
{% for result in results.data %}
@ -29,6 +45,20 @@
</tr>
{% endfor %}
</table>
{% if pages_total > 1 %}
{% if page > 1 %}
<a href="/search/{{ query }}/{{ page - 1 }}">Previous</a>
<b> | </b>
{% endif %}
Page {{ page }} of {{ pages_total }}
{% if page < pages_total %}
<b> | </b>
<a href="/search/{{ query }}/{{ page + 1}}">Next</a>
{% endif %}
{% endif %}
<footer>
<a href="https://codeberg.org/simple-web/peertube/src/commit/{{ commit }}">Commit: {{ commit }}</a>
</footer>

View file

@ -2,7 +2,7 @@
{% block title %}{{ domain }}{% endblock %}
{% block sub_content %}
{% block content %}
Coming Soon!

View file

@ -1,6 +1,6 @@
{% extends "base.html" %}
{% block content %}
{% block head_content %}
<table>
<tr>
<td>
@ -28,6 +28,4 @@
<br>
<br>
{% block sub_content %}{% endblock %}
{% endblock %}

View file

@ -2,7 +2,7 @@
{% block title %}{{ domain }}{% endblock %}
{% block sub_content %}
{% block content %}
{{ video_playlists.total }} Playlists
<br>

View file

@ -2,7 +2,7 @@
{% block title %}{{ domain }}{% endblock %}
{% block sub_content %}
{% block content %}
{{ videos.total }} Videos
<br>