2022-12-30 01:37:24 -05:00
|
|
|
from os import getenv
|
|
|
|
from urllib.parse import urljoin
|
|
|
|
from html import unescape
|
|
|
|
|
2022-12-31 02:04:57 -05:00
|
|
|
|
2022-12-30 01:37:24 -05:00
|
|
|
import pituophis
|
2022-12-31 02:04:57 -05:00
|
|
|
from pituophis import Item, Request
|
2022-12-30 01:37:24 -05:00
|
|
|
from requests import get
|
2022-12-31 02:04:57 -05:00
|
|
|
from parse import parse
|
|
|
|
from bs4 import BeautifulSoup
|
2022-12-30 01:37:24 -05:00
|
|
|
|
|
|
|
wordpress_url = getenv("URL")
|
|
|
|
|
2022-12-31 02:04:57 -05:00
|
|
|
|
|
|
|
handlers = {}
|
|
|
|
|
|
|
|
def register_handler(path: str):
|
|
|
|
def decorator_handler(func):
|
|
|
|
handlers[path] = func
|
|
|
|
return func
|
|
|
|
return decorator_handler
|
|
|
|
|
|
|
|
@register_handler("/post/{id}")
|
|
|
|
def post(request: Request, id: int):
|
|
|
|
post = get(urljoin(wordpress_url, f"wp-json/wp/v2/posts/{id}")).json()
|
2023-01-01 01:17:07 -05:00
|
|
|
return BeautifulSoup(post["content"]["rendered"]).get_text()
|
2022-12-31 02:04:57 -05:00
|
|
|
|
|
|
|
|
2022-12-30 01:37:24 -05:00
|
|
|
def handle(request):
|
2022-12-31 02:04:57 -05:00
|
|
|
for path, handler in handlers.items():
|
|
|
|
parse_result = parse(path, request.path)
|
|
|
|
if parse_result is not None:
|
|
|
|
return handler(request, **parse_result.named)
|
|
|
|
|
2022-12-30 01:37:24 -05:00
|
|
|
posts = get(urljoin(wordpress_url, "wp-json/wp/v2/posts")).json()
|
|
|
|
|
|
|
|
menu = [
|
2022-12-31 02:04:57 -05:00
|
|
|
Item(itype=0, path=f"/post/{post['id']}", text=unescape(post['title']['rendered']), host=request.host, port=request.port)
|
|
|
|
for i, post in enumerate(posts)
|
2022-12-30 01:37:24 -05:00
|
|
|
]
|
|
|
|
return menu
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
pituophis.serve("127.0.0.1", int(getenv("PORT")), handler=handle)
|
2022-12-31 02:04:57 -05:00
|
|
|
|
|
|
|
# Itypes:
|
|
|
|
# 0: FILE
|
|
|
|
# 1: dir
|
|
|
|
# 2: CSO
|
|
|
|
# 3: UNKN
|
|
|
|
# 4: HQX
|
|
|
|
# 5: BIN
|
|
|
|
# 6: UUE
|
|
|
|
# 7: ?
|
|
|
|
# 8: TEL
|
|
|
|
# 9: BIN
|