From 4fc9c9db419d51737f42fac6a20db345fe7154f6 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 12 Sep 2024 19:26:24 +0200 Subject: [PATCH] updates --- .gitignore | 4 ++++ autostart.desktop | 5 ++++ quotes.py | 59 +++++++++++++++++++++++++++++++++++++++-------- start.sh | 4 ++++ 4 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 .gitignore create mode 100644 autostart.desktop create mode 100755 start.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5e424ea --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +venv +last_quotes.json +denylist.py +__pycache__ diff --git a/autostart.desktop b/autostart.desktop new file mode 100644 index 0000000..8d9ee0a --- /dev/null +++ b/autostart.desktop @@ -0,0 +1,5 @@ +[Desktop Entry] +Type=Application +Name=Quotes +Exec=alacritty -e ~/quotes_display/start.sh +Hidden=true diff --git a/quotes.py b/quotes.py index b489a31..2da83f5 100644 --- a/quotes.py +++ b/quotes.py @@ -1,13 +1,46 @@ from rich.align import Align +from rich.console import Console from rich.layout import Layout from rich.live import Live -from rich.text import Text from rich.padding import Padding -import time -import requests +from rich.text import Text +import json import random +import requests +import time -quotes = requests.get('https://mattermore.zeus.gent/quotes.json').json() +from denylist import get_deny_list +denylist = get_deny_list() + + +def refresh_quotes(): + res = None + try: + # Fetch quotes + res = requests.get('https://mattermore.zeus.gent/quotes.json').json() + assert len(res) > 100 + res = res[-100:] + except: + pass + + if res is None: + # Fallback: load last quotes + try: + with open('last_quotes.json') as f: + res = json.load(f) + except: + assert False, "Failed to fetch quotes, and failed to load fallback json" + else: + try: + # Fetched successfully + # Save to disk for the fallback + with open('last_quotes.json', 'w') as f: + json.dump(res, f) + except: + pass + # Only keep non-denied quotes + res = [q for q in res if not any(denied in q['quote'] for denied in denylist)] + return res def render_quote(quote): for i in range(len(quote['quote'])): @@ -19,9 +52,17 @@ def render_quote(quote): ) yield l -with Live(Layout(Align.center(Text("Yeet"), vertical="middle")), refresh_per_second=30) as live: +with Live( + renderable=Layout(Align.center(Text("Yeet"), vertical="middle")), + console=Console(highlighter=None), + refresh_per_second=30 +) as live: + first_it = True while True: - for q in render_quote(random.choice(quotes[-100:])): - live.update(q) - time.sleep(0.05) - time.sleep(30) + quotes = refresh_quotes() + for _ in range(500): + for q in render_quote(random.choice(quotes) if not first_it else quotes[-1]): + live.update(q) + time.sleep(0.05) + time.sleep(30) + first_it = False diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..fd5b966 --- /dev/null +++ b/start.sh @@ -0,0 +1,4 @@ +#!/bin/sh +cd $(dirname $(realpath $0)) +. venv/bin/activate +python quotes.py