Better styles I guess
This commit is contained in:
parent
e8c91189c0
commit
86971c39d6
1 changed files with 45 additions and 33 deletions
78
cry/web.py
78
cry/web.py
|
|
@ -5,12 +5,13 @@ import functools
|
|||
import html
|
||||
import http.server
|
||||
import io
|
||||
import pathlib
|
||||
import time
|
||||
import traceback
|
||||
import threading
|
||||
import urllib.parse
|
||||
|
||||
from typing import Any, Callable, Concatenate, ParamSpec, Protocol
|
||||
from typing import Callable, Concatenate, ParamSpec
|
||||
|
||||
from . import database
|
||||
from . import feed
|
||||
|
|
@ -194,7 +195,9 @@ class EventConsumer:
|
|||
return Event(event=None, data=None, id=None)
|
||||
|
||||
|
||||
###############################################################################
|
||||
# Background Tasks
|
||||
###############################################################################
|
||||
|
||||
|
||||
class BackgroundTask:
|
||||
|
|
@ -335,6 +338,8 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
|||
def do_GET(self):
|
||||
if self.path == "/":
|
||||
return self.serve_feeds()
|
||||
elif self.path == "/style.css":
|
||||
return self.serve_style()
|
||||
elif self.path == "/refresh-status":
|
||||
return self.serve_status()
|
||||
elif self.path == "/subscribe-status":
|
||||
|
|
@ -433,26 +438,28 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
|||
self.wfile.flush()
|
||||
|
||||
def serve_status(self):
|
||||
global REFRESH_TASK
|
||||
|
||||
# TODO: FIX STYLES TO BE INLINE FOR SPEED I GUESS
|
||||
buffer = io.StringIO()
|
||||
buffer.write(
|
||||
"""
|
||||
<!doctype html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Status</title>
|
||||
<style>
|
||||
body { margin-left: 4rem; margin-right: 4rem; }
|
||||
li.entry { display: inline; padding-right: 1rem; }
|
||||
li.entry:before { content: '\\2022'; padding-right: 0.5rem; }
|
||||
h1 { margin-bottom: 0.25rem; }
|
||||
</style>
|
||||
<link rel="stylesheet" href="/style.css" type="text/css" />
|
||||
</head>
|
||||
<div style="display: flex; flex-flow: column; height: 100vh">
|
||||
<h1 style="flex: 0 1 auto;">Status</h1>
|
||||
<h2 style="flex: 0 1 auto;">Status: <span id="status">Starting...</span></h2>
|
||||
<pre style="flex: 1 1 auto; overflow-y: scroll;" id="log"></pre>
|
||||
<div style="flex: 0 1 auto;"><a href="/">Back to feeds</a></div>
|
||||
<body>
|
||||
<header>
|
||||
<h1>Status</h1>
|
||||
<h2>Status: <span id="status">Starting...</span></h2>
|
||||
</header>
|
||||
|
||||
<pre class="content" id="log"></pre>
|
||||
|
||||
<footer>
|
||||
<a href="/">Back to feeds</a>
|
||||
</footer>
|
||||
</div>
|
||||
<script>
|
||||
function append_log(txt) {
|
||||
|
|
@ -472,9 +479,10 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
|||
});
|
||||
events.addEventListener("redirect", (e) => {
|
||||
console.log(e);
|
||||
window.location = e.data;
|
||||
// window.location = e.data;
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
"""
|
||||
)
|
||||
|
||||
|
|
@ -494,12 +502,7 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
|||
<head>
|
||||
<meta charset="utf8">
|
||||
<title>Subscribed Feeds</title>
|
||||
<style>
|
||||
body { margin-left: 4rem; margin-right: 4rem; }
|
||||
li.entry { display: inline; padding-right: 1rem; }
|
||||
li.entry:before { content: '\\2022'; padding-right: 0.5rem; }
|
||||
h1 { margin-bottom: 0.25rem; }
|
||||
</style>
|
||||
<link rel="stylesheet" href="/style.css" type="text/css" />
|
||||
</head>
|
||||
<h1>Feeds</h1>
|
||||
<div>
|
||||
|
|
@ -516,18 +519,16 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
|||
)
|
||||
for f in feeds:
|
||||
feed_title = html.escape(f.title)
|
||||
if len(f.entries) > 0:
|
||||
ago = f" ({f.entries[0].time_ago()})"
|
||||
else:
|
||||
ago = ""
|
||||
buffer.write(f"<div class='feed'>")
|
||||
buffer.write(f'<h2><a href="{f.link}">{feed_title}</a>{ago}</h2>')
|
||||
buffer.write(
|
||||
f'<h2><a href="{f.link}" target="_blank">{feed_title}</a></h2>'
|
||||
)
|
||||
if len(f.entries) > 0:
|
||||
buffer.write(f"<ul>")
|
||||
for entry in f.entries:
|
||||
title = html.escape(entry.title)
|
||||
buffer.write(
|
||||
f'<li class="entry"><a href="{entry.link}">{title}</a> ({entry.time_ago()})</li>'
|
||||
f'<li class="entry"><a href="{entry.link}" target="_blank">{title}</a> ({entry.time_ago()})</li>'
|
||||
)
|
||||
buffer.write(f"</ul>")
|
||||
else:
|
||||
|
|
@ -555,12 +556,7 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
|||
<head>
|
||||
<meta charset="utf8">
|
||||
<title>Choose Feed</title>
|
||||
<style>
|
||||
body { margin-left: 4rem; margin-right: 4rem; }
|
||||
li.entry { display: inline; padding-right: 1rem; }
|
||||
li.entry:before { content: '\\2022'; padding-right: 0.5rem; }
|
||||
h1 { margin-bottom: 0.25rem; }
|
||||
</style>
|
||||
<link rel="stylesheet" href="/style.css" type="text/css" />
|
||||
</head>
|
||||
<h1>Choose Feed</h1>
|
||||
<p>More than one feed was found. Choose the feed to subscribe to.</p>
|
||||
|
|
@ -603,6 +599,22 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
|||
self.end_headers()
|
||||
self.wfile.write(response)
|
||||
|
||||
def serve_style(self):
|
||||
self.write_file(
|
||||
pathlib.Path(__file__).parent / "static" / "style.css",
|
||||
content_type="text/css",
|
||||
)
|
||||
|
||||
def write_file(self, path: pathlib.Path, content_type: str):
|
||||
with open(path, "rb") as file:
|
||||
content = file.read()
|
||||
|
||||
self.send_response(200)
|
||||
self.send_header("content-type", content_type)
|
||||
self.send_header("content-length", str(len(content)))
|
||||
self.end_headers()
|
||||
self.wfile.write(content)
|
||||
|
||||
|
||||
def serve():
|
||||
with http.server.ThreadingHTTPServer(("", 8000), Handler) as server:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue