重构:基础代码
This commit is contained in:
44
main.py
44
main.py
@@ -1,8 +1,10 @@
|
||||
from flask import Flask, render_template, redirect, url_for
|
||||
from loguru import logger
|
||||
|
||||
from src.config import Config
|
||||
from src.generator import build_data
|
||||
import const
|
||||
import models
|
||||
import tools
|
||||
from generator import Generator
|
||||
|
||||
app = Flask(__name__)
|
||||
logger.add("endofyear.log")
|
||||
@@ -10,28 +12,36 @@ logger.add("endofyear.log")
|
||||
|
||||
@app.route('/')
|
||||
def home():
|
||||
# 默认主题 painting
|
||||
# 重定向 painting
|
||||
return redirect(url_for('painting'))
|
||||
|
||||
|
||||
@app.route('/painting')
|
||||
def painting():
|
||||
if Config("config.ini").web_status:
|
||||
# web 服务
|
||||
# 如果数据存在,直接返回
|
||||
if blog_data := Config("config.ini").blog_data:
|
||||
return render_template('painting.html', data=blog_data, web_status=1)
|
||||
# 站点数据
|
||||
site = models.Site(
|
||||
service=const.SITE_SERVICE,
|
||||
title=const.SITE_NAME
|
||||
).to_dict()
|
||||
|
||||
# 如果数据不存在,需要生成,并写入配置
|
||||
return render_template('painting.html', data=build_data(), web_status=1)
|
||||
else:
|
||||
# Github 静态
|
||||
# 数据需要生成,并写入静态文件
|
||||
html_data = render_template('painting.html', data=build_data(), web_status=0)
|
||||
with open("static/index.html", "w") as f:
|
||||
f.write(html_data)
|
||||
# 自定义数据
|
||||
custom = models.Custom(
|
||||
yiyan=tools.get_yiyan()
|
||||
).to_dict()
|
||||
|
||||
return 'OK'
|
||||
# 初始化数据生成器
|
||||
generator = Generator("https://blog.7wate.com/rss.xml")
|
||||
|
||||
# 渲染模板
|
||||
return render_template('painting.html',
|
||||
site=site,
|
||||
blog=generator.blog(),
|
||||
special_post=generator.special_post(),
|
||||
sentiment_post=generator.sentiment_post(),
|
||||
long_post=generator.long_post(),
|
||||
short_post=generator.short_post(),
|
||||
custom=custom
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user