~metalune/public-inbox

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
1

[PATCH 1/2] Cache accepts arbitrary (lambda) condition for refreshing

southerntofu
Details
Message ID
<20210729150628.892905-1-southerntofu@thunix.net>
DKIM signature
missing
Download raw message
Patch: +3 -2
---
 main.py | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/main.py b/main.py
index 764a640..20b769d 100644
--- a/main.py
+++ b/main.py
@@ -66,15 +66,16 @@ class VideoWrapper:

# Helper Class for using caches
class Cache:
    def __init__(self):
    def __init__(self, criteria = lambda diff: diff.days > 0):
        self.dict = {}
        self.criteria = criteria

    def get(self, arg, func):
        if arg in self.dict:
            last_time_updated = (self.dict[arg])[1]
            time_diff = datetime.now() - last_time_updated

            if time_diff.days > 0:
            if self.criteria(time_diff):
                self.dict[arg] = [
                    func(arg),
                    datetime.now()
-- 
2.30.2

[PATCH 2/2] Display latest videos from account subs in accounts.list on homepage, if any

southerntofu
Details
Message ID
<20210729150628.892905-2-southerntofu@thunix.net>
In-Reply-To
<20210729150628.892905-1-southerntofu@thunix.net> (view parent)
DKIM signature
missing
Download raw message
Patch: +69 -2
---
 main.py              | 37 +++++++++++++++++++++++++++++++++++++
 templates/index.html | 34 ++++++++++++++++++++++++++++++++--
 2 files changed, 69 insertions(+), 2 deletions(-)

diff --git a/main.py b/main.py
index 20b769d..1e21c0a 100644
--- a/main.py
+++ b/main.py
@@ -1,5 +1,6 @@
from quart import Quart, request, render_template, redirect
from datetime import datetime
from dateutil import parser as dateutil
from math import ceil
import peertube
import html2text
@@ -93,6 +94,9 @@ cached_instance_names = Cache()
cached_account_infos = Cache()
cached_video_channel_infos = Cache()

cached_subscriptions_accounts = Cache(criteria = lambda diff: diff.total_seconds() > 60)
cached_account_videos = Cache(criteria = lambda diff: diff.total_seconds() > 1800)

# cache the instance names so we don't have to send a request to the domain every time someone
# loads any site 
def get_instance_name(domain):
@@ -116,8 +120,39 @@ def get_video_channel(info):
def get_video_channel_info(name):
    return cached_video_channel_infos.get(name, get_video_channel)

# Get latest remote videos from name
def get_latest_account_videos(name):
    return cached_account_videos.get(name, latest_account_videos)

# Refresh latest remote videos from name
def latest_account_videos(name):
    print("[CACHE] Refreshing acount videos for %s" % name)
    (name, domain) = name.split('@')
    return peertube.account_videos(domain, name, 0)

# Get local accounts subscriptions, as specified in accounts.list
def get_subscriptions_accounts():
    return cached_subscriptions_accounts.get("accounts", load_subscriptions_accounts)

# Refresh local accounts subscriptions
def load_subscriptions_accounts(_):
    print("[CACHE] Refreshing subscriptions accounts from accounts.list")
    try:
        with open('accounts.list', 'r') as f:
            subscriptions = f.read().splitlines()
    except Exception as e:
        print("No `accounts.list` file to load for local subscriptions")
        subscriptions = []
    return subscriptions

# Get the latest videos from local accounts subscriptions, ordered by most recent and with ; only return `limit` number of videos
def get_subscriptions_accounts_videos(limit=12):
    latest  = []
    for sub in get_subscriptions_accounts():
        account_latest = get_latest_account_videos(sub)["data"]
        latest.extend(account_latest)
    latest.sort(key = lambda vid: dateutil.isoparse(vid["createdAt"]), reverse=True)
    return latest[0:limit]

app = Quart(__name__)

@@ -125,6 +160,8 @@ app = Quart(__name__)
async def main():
    return await render_template(
        "index.html",
        videos = get_subscriptions_accounts_videos(),
        subscriptions = map(lambda sub: get_account_info(sub), get_subscriptions_accounts())
    )

@app.route("/search", methods = ["POST"])
diff --git a/templates/index.html b/templates/index.html
index 8a64d92..7ece231 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -3,7 +3,7 @@
    <head>
        <title>SimpleerTube - Search</title>
        <link rel="stylesheet" href="/static/style.css">
	<link rel="search" type="application/opensearchdescription+xml" href="/opensearch.xml" title="SimpleerTube"/>
        <link rel="search" type="application/opensearchdescription+xml" href="/opensearch.xml" title="SimpleerTube"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    </head>
    <body>
@@ -14,7 +14,37 @@
            <form action="/search" method="POST">
                <input size="45" style="max-width: 100%" type="text" name="query" id="query" placeholder="SepiaSearch"/>
                <button type="submit">Search</button>
            </form>
            </form>{% if videos|length > 0 %}
            <hr>
            <h2>{{ videos|length }} latest videos from local subscriptions</h2>
            <p>{% for sub in subscriptions %}{% if not loop.first %}, {% endif %}<a href="/{{ sub.host }}/accounts/{{ sub.name }}">{{ sub.displayName }} (@{{ sub.name }}@{{ sub.host }})</a>{% endfor %}</p>
            <hr>
            <div id="wrap">
                {#{% for vid in videos %}
                    <pre>{{ vid|pprint }}</pre>
                {% endfor %}#}
                {% for vid in videos %}
                <div class="result-wrapper">
                    <a href="/{{ vid.account.host }}/videos/watch/{{ vid.uuid }}">
                        <img src="https://{{ vid.account.host }}{{ vid.thumbnailPath }}" height="150"/>
                    </a>

                    <div class="result-info">
                        <a href="/{{ vid.account.host }}/videos/watch/{{ vid.uuid }}">{{ vid.name }}</a>
                        <br>
                        {{ vid.views }} Views
                        <br>
                        <a href="/{{ vid.account.host }}/video-channels/{{ vid.channel.name }}@{{ vid.channel.host }}">
                            <b>{{ vid.channel.displayName }}</b>
                        </a>
                        <br>
                        <a href="/{{ vid.account.host }}/accounts/{{ vid.account.name }}@{{ vid.account.host }}">
                            {{ vid.account.name }}@{{ vid.account.host }}
                        </a>
                    </div>
                </div>
                {% endfor %}
            </div>{% endif %}
        </center>
    </body>
</html>
-- 
2.30.2
Reply to thread Export thread (mbox)