Voyage/voyage.py

89 lines
2.3 KiB
Python
Raw Normal View History

2019-12-06 18:13:43 -05:00
#!/usr/bin/env python3
"""
A display interface for archived 4chan quest threads.
"""
from aiohttp import web
import jinja2
import aiohttp_jinja2
from aiohttp_jinja2 import render_template
import asyncpg
import uvloop
import config
2019-12-10 18:19:01 -05:00
import filters
2019-12-06 18:13:43 -05:00
uvloop.install()
routes = web.RouteTableDef()
@routes.get('/', name='index')
async def index(request):
"""The index page."""
async with request.app['pool'].acquire() as conn:
threads = await conn.fetch("SELECT * FROM thread ORDER BY time ASC")
return render_template("index.html", request, locals())
2019-12-10 18:19:01 -05:00
@routes.get(r'/{thread_id:\d+}', name='thread')
async def thread(request):
"""A single thread page."""
async with request.app['pool'].acquire() as conn:
data = await conn.fetch(
"SELECT post.id, post.name, post.trip_code, post.subject, "
"post.time, post.body, "
"tag.name AS tag_name, link.link_to, link.link_from "
"FROM post "
"LEFT JOIN tag ON (post.id = tag.post_id) "
"LEFT JOIN link ON (post.id = link.link_from) "
"WHERE post.thread_id = $1 "
"ORDER BY post.id ASC",
int(request.match_info['thread_id']))
posts = {}
backlinks = {}
for row in data:
post = posts.get(row['id'], {})
for key, value in row.items():
if key == 'tag_name':
tags = post.get('tags', [])
tags.append(value)
post['tags'] = tags
elif key == 'link_to':
link_tos = post.get('link_tos', [])
link_tos.append(value)
post['link_tos'] = link_tos
elif key == 'link_from':
back_post = backlinks.get(row['link_to'], [])
back_post.append(value)
backlinks[row['link_to']] = back_post
post[key] = value
post['tags'] = [t for t in post['tags'] if t]
posts[row['id']] = post
return render_template("thread.html", request, locals())
2019-12-06 18:13:43 -05:00
async def init_app():
"""Initializes the application."""
app = web.Application()
aiohttp_jinja2.setup(
app,
trim_blocks=True,
lstrip_blocks=True,
undefined=jinja2.StrictUndefined,
loader=jinja2.FileSystemLoader('templates'),
2019-12-10 18:19:01 -05:00
filters={
'quotelink': filters.quotelink,
},
2019-12-06 18:13:43 -05:00
)
app['pool'] = await asyncpg.create_pool(**config.db)
app.router.add_routes(routes)
app_wrap = web.Application()
app_wrap.add_subapp(config.url_prefix, app)
return app_wrap
if __name__ == "__main__":
app = init_app()
web.run_app(app, host='0.0.0.0', port=5450)