import asyncio import aiotieba from aioflask import render_template, request, escape from flask_caching import Cache from urllib.parse import quote_plus from datetime import datetime from aiotieba.api.get_posts._classdef import * from aiotieba.api._classdef.contents import * from shared import * from extra import * ###################################################################### # Convert a timestamp to its simpliest readable date format. @app.template_filter('simpledate') def _jinja2_filter_simpledate(ts): t = datetime.fromtimestamp(ts) now = datetime.now() if t.date() == now.date(): return t.strftime('%H:%m') elif t.year == now.year: return t.strftime('%m-%d') else: return t.strftime('%Y-%m-%d') # Convert a timestamp to a humand readable date format. @app.template_filter('date') def _jinja2_filter_datetime(ts, fmt='%Y年%m月%d日 %H点%m分'): return datetime.fromtimestamp(ts).strftime(fmt) # Convert a integer to the one with separator like 1,000,000. @app.template_filter('intsep') def _jinja2_filter_intsep(i): return f'{int(i):,}' # Reduce the text to a shorter form. @app.template_filter('trim') def _jinja2_filter_trim(text): return text[:78] + '……' if len(text) > 78 else text # Format fragments to its equiviant HTML. @app.template_filter('translate') def _jinja2_filter_translate(frags): htmlfmt = '' for frag in frags: if isinstance(frag, FragText): subfrags = frag.text.split('\n') for subfrag in subfrags: htmlfmt += '
' + str(escape(subfrag)) + '
' elif isinstance(frag, FragImage_p): htmlfmt += \ f'' \ f'' elif isinstance(frag, FragEmoji_p): clear_leading = False if htmlfmt.endswith(''): clear_leading = True htmlfmt = htmlfmt.rstrip('') htmlfmt += f'' if clear_leading: htmlfmt += '' return htmlfmt ###################################################################### @app.route('/p/