Browse Source

Added path to show articles on page and calculate time since last article.

master
Trey Del Bonis 2 years ago
parent
commit
54a442c06b
4 changed files with 100 additions and 4 deletions
  1. 61
    2
      app.py
  2. 4
    1
      sbenv.py
  3. 4
    0
      static/style.css
  4. 31
    1
      templates/main.htm

+ 61
- 2
app.py View File

@@ -1,5 +1,9 @@

from datetime import datetime
import json
import traceback

import aiofiles
import fastapi
from fastapi import Cookie, File, Form, Request, UploadFile, WebSocket, WebSocketDisconnect
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse, RedirectResponse, PlainTextResponse
@@ -8,6 +12,7 @@ from fastapi.templating import Jinja2Templates
from fastapi.middleware.cors import CORSMiddleware

import reporthook
import searchlib

################################
# Core configuration
@@ -46,10 +51,64 @@ async def handle_exception(req: Request, exc: Exception):
return PlainTextResponse('error', status_code=500)

@app.get('/')
def render_main(req: Request):
async def render_main(req: Request):
raw_articles = await load_days_from_file('testresults.json')
converted = convert_days_from_articles(raw_articles)
num_days = calc_num_days(converted)

p = {
'num_days': 5,
'sb': {
'num_days': num_days,
'days': converted
},
'request': req,
}

return tmplts.TemplateResponse('main.htm', p)

################################
# Utilities
################################

async def load_days_from_file(path):
async with aiofiles.open(path, mode='r') as f:
contents = await f.read()
return json.loads(contents)

def convert_days_from_articles(rarts):
processed = searchlib.process_results(rarts)
output = []

for dstr, arts in processed.items():
day = {
'date': dstr,
'links': [convert_article(a) for a in arts['pass']],
'maybe_links': [convert_article(a) for a in arts['maybe']]
}

if len(day['links']) > 0:
output.append(day)

return output

def convert_article(a):
return {
'url': a['url'],
'title': a['gtitle'],
'slug': a['slug'],
}

DATE_FORMAT = "%Y-%m-%d"

def calc_num_days(dayslist):
today = datetime.now()
lowest = -1

for d in dayslist:
pd = datetime.strptime(d['date'], DATE_FORMAT)
diff = today - pd
ndays = diff.days
if ndays < lowest or lowest == -1:
lowest = ndays

return lowest

+ 4
- 1
sbenv.py View File

@@ -10,7 +10,10 @@ def load_cookiejar():
return cj

def get_google_abuse_token():
return os.getenv('SB_GOOGABUSE').trim()
ga = os.getenv('SB_GOOGABUSE')
if ga is not None:
ga = ga.strip()
return ga

def get_report_webhook():
hu = os.getenv('SB_REPORT_WH')

+ 4
- 0
static/style.css View File

@@ -29,6 +29,10 @@ body {
background-color: #f66711; /* uhaul orange */
}

.uhaul_days {
font-size: 36pt;
}

@media screen and (max-width: 52em) {
.uhaul_cont {
font-size: 24pt;

+ 31
- 1
templates/main.htm View File

@@ -1,5 +1,9 @@
{% extends 'base.htm' %}

{% macro render_article_link(desc) %}
<li><a href="{{ desc.url }}">{{ desc.title }}</a></li>
{% endmacro %}

{% block content %}

<div class="uhaul_cont">
@@ -8,7 +12,7 @@
</div>
<div class="uhaul_main">
<div class="uhaul_num">
{{ num_days }}
{{ sb.num_days }}
</div>
<div class="uhaul_days">
days
@@ -19,4 +23,30 @@
</div>
</div>

<div id="main" class="container">
<h1>Articles</h1>
<div id="days">
{% for d in sb.days %}
<div class="day mb-3">
<h3>{{ d.date }}</h3>
<ul>
{% for l in d.links %}
{{ render_article_link(l) }}
{% endfor %}
</ul>
{% if d.maybe_links|length > 0 %}
<details>
<summary><em>and {{ d.maybe_links|length }} weak match(es)</em></summary>
<ul>
{% for l in d.maybe_links %}
{{ render_article_link(l) }}
{% endfor %}
</ul>
</details>
{% endif %}
</div>
{% endfor %}
</div>
</div>

{% endblock %}

Loading…
Cancel
Save