2023-07-11 11:32:13 +00:00
|
|
|
import asyncio
|
|
|
|
import aiotieba
|
|
|
|
|
2023-07-11 14:23:48 +00:00
|
|
|
from aioflask import render_template, request, escape
|
|
|
|
from flask_caching import Cache
|
|
|
|
from urllib.parse import quote_plus
|
2023-07-11 11:32:13 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2023-07-11 14:23:48 +00:00
|
|
|
from aiotieba.api.get_posts._classdef import *
|
|
|
|
from aiotieba.api._classdef.contents import *
|
2023-07-11 11:32:13 +00:00
|
|
|
|
2023-07-11 14:23:48 +00:00
|
|
|
from shared import *
|
|
|
|
from extra import *
|
2023-07-11 11:32:13 +00:00
|
|
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2023-07-11 14:23:48 +00:00
|
|
|
# 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 += '<p>' + str(escape(subfrag)) + '</p>'
|
|
|
|
elif isinstance(frag, FragImage_p):
|
|
|
|
htmlfmt += \
|
|
|
|
f'<a target="_blank" href="/proxy/pic/{ extract_image_name(frag.origin_src) }">' \
|
|
|
|
f'<img width="{ frag.show_width}" height="{ frag.show_height }" '\
|
|
|
|
f'src="/proxy/pic/{ extract_image_name(frag.src) }"></a>'
|
|
|
|
elif isinstance(frag, FragEmoji_p):
|
|
|
|
clear_leading = False
|
|
|
|
if htmlfmt.endswith('</p>'):
|
|
|
|
clear_leading = True
|
|
|
|
htmlfmt = htmlfmt.rstrip('</p>')
|
|
|
|
htmlfmt += f'<img class="emoticons" alt="[{ frag.desc }]" src="/static/emoticons/{ quote_plus(frag.desc) }.png">'
|
|
|
|
if clear_leading:
|
|
|
|
htmlfmt += '</p>'
|
|
|
|
|
|
|
|
return htmlfmt
|
|
|
|
|
2023-07-11 11:32:13 +00:00
|
|
|
######################################################################
|
|
|
|
|
|
|
|
@app.route('/p/<tid>')
|
|
|
|
async def thread_view(tid):
|
|
|
|
tid = int(tid)
|
|
|
|
pn = int(request.args.get('pn') or 1)
|
|
|
|
|
|
|
|
async with aiotieba.Client() as tieba:
|
|
|
|
# Default to 15 posts per page, confirm to tieba.baidu.com
|
|
|
|
thread_info = await tieba.get_posts(tid, rn=15, pn=pn)
|
|
|
|
|
2023-07-11 14:23:48 +00:00
|
|
|
for post in thread_info:
|
|
|
|
print(post.comments)
|
2023-07-11 11:32:13 +00:00
|
|
|
|
|
|
|
return await render_template('thread.html', info=thread_info)
|
|
|
|
|
|
|
|
@app.route('/f')
|
|
|
|
async def forum_view():
|
|
|
|
fname = request.args['kw']
|
|
|
|
pn = int(request.args.get('pn') or 1)
|
|
|
|
|
|
|
|
async with aiotieba.Client() as tieba:
|
2023-07-11 14:23:48 +00:00
|
|
|
forum_info, threads = await asyncio.gather(awaitify(find_tieba_info)(fname),
|
2023-07-11 11:32:13 +00:00
|
|
|
tieba.get_threads(fname, rn=50, pn=pn))
|
|
|
|
|
|
|
|
return await render_template('bar.html', info=forum_info, threads=threads)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(debug=True)
|