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 quart import Quart, request, render_template, redirect
from datetime import datetime from datetime import datetime
from math import ceil
import peertube import peertube
commit = "not found" commit = "not found"
@ -115,13 +116,21 @@ async def simpleer_search_redirect():
query = (await request.form)["query"] query = (await request.form)["query"]
return redirect("/search/" + query) return redirect("/search/" + query)
@app.route("/search/<string:query>") @app.route("/search/<string:query>", defaults = {"page": 1})
async def simpleer_search(query): @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( return await render_template(
"simpleer_search_results.html", "simpleer_search_results.html",
commit=commit, 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): async def instance(domain):
return redirect("/" + domain + "/videos/trending") return redirect("/" + domain + "/videos/trending")
@app.route("/<string:domain>/videos/local") @app.route("/<string:domain>/videos/local", defaults = {"page": 1})
async def instance_videos_local(domain): @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( return await render_template(
"instance/local.html", "instance/local.html",
domain=domain, domain=domain,
instance_name=get_instance_name(domain), instance_name=get_instance_name(domain),
commit=commit, 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") @app.route("/<string:domain>/videos/trending", defaults = {"page": 1})
async def instance_videos_trending(domain): @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( return await render_template(
"instance/trending.html", "instance/trending.html",
domain=domain, domain=domain,
instance_name=get_instance_name(domain), instance_name=get_instance_name(domain),
commit=commit, 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") @app.route("/<string:domain>/videos/most-liked", defaults = {"page": 1})
async def instance_videos_most_liked(domain): @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( return await render_template(
"instance/most-liked.html", "instance/most-liked.html",
domain=domain, domain=domain,
instance_name=get_instance_name(domain), instance_name=get_instance_name(domain),
commit=commit, 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") @app.route("/<string:domain>/videos/recently-added", defaults = {"page": 1})
async def instance_videos_recently_added(domain): @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( return await render_template(
"instance/recently-added.html", "instance/recently-added.html",
domain=domain, domain=domain,
instance_name=get_instance_name(domain), instance_name=get_instance_name(domain),
commit=commit, 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"]) @app.route("/<string:domain>/search", methods=["POST"])
async def search_redirect(domain): async def search_redirect(domain):
query = (await request.form)["query"] query = (await request.form)["query"]
return redirect("/" + domain + "/search/" + query) return redirect("/" + domain + "/search/" + query)
@app.route("/<string:domain>/search/<string:term>") @app.route("/<string:domain>/search/<string:term>", defaults = {"page": 1})
async def search(domain, term): @app.route("/<string:domain>/search/<string:term>/<int:page>")
amount, results = peertube.search(domain, term) async def search(domain, term, page):
results = peertube.search(domain, term, (page - 1) * 10)
return await render_template( return await render_template(
"search_results.html", "search_results.html",
domain=domain, domain=domain,
instance_name=get_instance_name(domain), instance_name=get_instance_name(domain),
commit=commit, commit=commit,
amount=amount,
results=results, results=results,
search_term=term, 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 --- # --- Accounts ---
@app.route("/<string:domain>/accounts/<string:name>") @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") return redirect("/" + domain + "/accounts/" + name + "/video-channels")
@app.route("/<string:domain>/accounts/<string:name>/video-channels") @app.route("/<string:domain>/accounts/<string:name>/video-channels", defaults = {"page": 1})
async def account__video_channels(domain, name): @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( return await render_template(
"accounts/video_channels.html", "accounts/video_channels.html",
domain=domain, domain=domain,
@ -246,11 +292,18 @@ async def account__video_channels(domain, name):
name = name, name = name,
account = get_account_info(build_channel_or_account_name(domain, 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") @app.route("/<string:domain>/accounts/<string:name>/videos", defaults = {"page": 1})
async def account__videos(domain, name): @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( return await render_template(
"accounts/videos.html", "accounts/videos.html",
domain=domain, domain=domain,
@ -259,7 +312,12 @@ async def account__videos(domain, name):
name = name, name = name,
account = get_account_info(build_channel_or_account_name(domain, 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") @app.route("/<string:domain>/accounts/<string:name>/about")
@ -278,11 +336,13 @@ async def account__about(domain, name):
# --- Video-Channels --- # --- Video-Channels ---
@app.route("/<string:domain>/video-channels/<string:name>") @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") return redirect("/" + domain + "/video-channels/" + name + "/videos")
@app.route("/<string:domain>/video-channels/<string:name>/videos") @app.route("/<string:domain>/video-channels/<string:name>/videos", defaults = {"page": 1})
async def video_channels__videos(domain, name): @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( return await render_template(
"video_channels/videos.html", "video_channels/videos.html",
domain=domain, domain=domain,
@ -291,11 +351,16 @@ async def video_channels__videos(domain, name):
name = name, name = name,
video_channel = get_video_channel_info(build_channel_or_account_name(domain, 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") @app.route("/<string:domain>/video-channels/<string:name>/video-playlists", defaults = {"page": 1})
async def video_channels__video_playlists(domain, name): @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( return await render_template(
"video_channels/video_playlists.html", "video_channels/video_playlists.html",
domain=domain, domain=domain,
@ -304,7 +369,11 @@ async def video_channels__video_playlists(domain, name):
name = name, name = name,
video_channel = get_video_channel_info(build_channel_or_account_name(domain, 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") @app.route("/<string:domain>/video-channels/<string:name>/about")

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -24,9 +24,40 @@
<b> | </b> <b> | </b>
<a href="/{{ domain }}/videos/recently-added">Recently Added</a> <a href="/{{ domain }}/videos/recently-added">Recently Added</a>
<br> <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> <br>
{% block content %}{% endblock %} {% 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> <footer>
<a href="https://codeberg.org/simple-web/peertube/src/commit/{{ commit }}">Commit: {{ commit }}</a> <a href="https://codeberg.org/simple-web/peertube/src/commit/{{ commit }}">Commit: {{ commit }}</a>
</footer> </footer>

View file

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

View file

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

View file

@ -6,11 +6,27 @@
<body> <body>
<form action="/search" method="POST"> <form action="/search" method="POST">
<label for="query"><b>SimpleerTube</b></label> <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> <button type="submit">Search</button>
</form> </form>
<br> <br>
<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 {{ results.total }} Results
<table> <table>
{% for result in results.data %} {% for result in results.data %}
@ -29,6 +45,20 @@
</tr> </tr>
{% endfor %} {% endfor %}
</table> </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> <footer>
<a href="https://codeberg.org/simple-web/peertube/src/commit/{{ commit }}">Commit: {{ commit }}</a> <a href="https://codeberg.org/simple-web/peertube/src/commit/{{ commit }}">Commit: {{ commit }}</a>
</footer> </footer>

View file

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

View file

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

View file

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

View file

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