|
|
@@ -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 |