功能:离线模式&更新依赖

This commit is contained in:
周中平 2023-11-06 14:46:18 +08:00
parent 69f883f0a5
commit ba7f8c0456
Signed by: zhouzhongping
GPG Key ID: 6666822800008000
5 changed files with 59 additions and 52 deletions

View File

@ -2,5 +2,4 @@
web = true web = true
[blog] [blog]
rss = rss =
data =

53
main.py
View File

@ -4,6 +4,7 @@ from loguru import logger
import const import const
import models import models
import tools import tools
from config import Config
from generator import Generator from generator import Generator
app = Flask(__name__) app = Flask(__name__)
@ -18,9 +19,12 @@ def home():
@app.route('/painting') @app.route('/painting')
def painting(): def painting():
# 读取配置文件
config = Config("config.ini")
# 站点数据 # 站点数据
site = models.Site( site = models.Site(
service=const.SITE_SERVICE, service=config.web_status,
title=const.SITE_NAME title=const.SITE_NAME
).to_dict() ).to_dict()
@ -30,18 +34,43 @@ def painting():
).to_dict() ).to_dict()
# 初始化数据生成器 # 初始化数据生成器
generator = Generator("https://blog.7wate.com/rss.xml") generator = Generator(config.rss_url)
logger.info(f"Site: {site}")
logger.info(f"Blog: {generator.blog()}")
logger.info(f"Special Post: {generator.special_post()}")
logger.info(f"Sentiment Post: {generator.sentiment_post()}")
logger.info(f"Long Post: {generator.long_post()}")
logger.info(f"Short Post: {generator.short_post()}")
logger.info(f"Custom: {custom}")
# 渲染模板 # 服务模式
return render_template('painting.html', if config.web_status == const.SITE_SERVICE_STATIC:
site=site, # 静态网站模式
blog=generator.blog(), html_static_file = render_template('painting.html',
special_post=generator.special_post(), site=site,
sentiment_post=generator.sentiment_post(), blog=generator.blog(),
long_post=generator.long_post(), special_post=generator.special_post(),
short_post=generator.short_post(), sentiment_post=generator.sentiment_post(),
custom=custom long_post=generator.long_post(),
) short_post=generator.short_post(),
custom=custom
)
with open("static/index.html", "w") as f:
f.write(html_static_file)
return 'OK'
else:
# web 模式
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__': if __name__ == '__main__':

View File

@ -1,8 +1,9 @@
beautifulsoup4==4.12.2 beautifulsoup4==4.12.2
blinker==1.6.3 blinker==1.7.0
certifi==2023.7.22 certifi==2023.7.22
charset-normalizer==3.3.1 charset-normalizer==3.3.2
click==8.1.7 click==8.1.7
docopt==0.6.2
feedparser==6.0.10 feedparser==6.0.10
Flask==3.0.0 Flask==3.0.0
idna==3.4 idna==3.4
@ -25,4 +26,4 @@ soupsieve==2.5
textblob==0.17.1 textblob==0.17.1
tqdm==4.66.1 tqdm==4.66.1
urllib3==2.0.7 urllib3==2.0.7
Werkzeug==3.0.1 Werkzeug==3.0.1

View File

@ -5,6 +5,7 @@ from urllib.parse import urlparse
from loguru import logger from loguru import logger
import const
from src.tools import check_website_status from src.tools import check_website_status
@ -69,30 +70,6 @@ class Config:
return '.'.join(domain_parts[-2:]) return '.'.join(domain_parts[-2:])
@property
def blog_data(self):
try:
data = self.config.get('blog', 'data', fallback=None)
except configparser.NoSectionError:
logger.error('未找到 section 配置项,请检查拼写')
return None
if not data:
logger.error('data 配置值为空')
return None
return json.loads(data)
@blog_data.setter
def blog_data(self, value):
if not self.config.has_section('blog'):
self.config.add_section('blog')
self.config.set('blog', 'data', json.dumps(value))
with open(self.path, 'w') as configfile:
self.config.write(configfile)
@property @property
def web_status(self): def web_status(self):
try: try:
@ -103,10 +80,10 @@ class Config:
if web_status is None: if web_status is None:
logger.error('web 配置值为空') logger.error('web 配置值为空')
return True return const.SITE_SERVICE_WEB
if web_status == "True" or web_status == "true": if web_status == "True" or web_status == "true" or web_status == "t" or web_status == "T":
return True return const.SITE_SERVICE_WEB
if web_status == "False" or web_status == "false": if web_status == "False" or web_status == "false" or web_status == "f" or web_status == "F":
return False return const.SITE_SERVICE_STATIC

View File

@ -1,15 +1,16 @@
# 站点标题
SITE_NAME = "EndOfYear"
# 站点服务模式
SITE_SERVICE_WEB = 1
SITE_SERVICE_STATIC = 0
# 时区 # 时区
TIME_ZONE = "Asia/Shanghai" TIME_ZONE = "Asia/Shanghai"
# 时间格式 # 时间格式
FORMAT_TIME = "%Y-%m-%d %H:%M:%S" FORMAT_TIME = "%Y-%m-%d %H:%M:%S"
# 站点服务模式
SITE_SERVICE = 1
# 站点标题
SITE_NAME = "EndOfYear"
# 博客文章分类-生活 # 博客文章分类-生活
BLOG_POST_CATEGORY_LIFE = 1 BLOG_POST_CATEGORY_LIFE = 1