From ba7f8c04564c5126dc483aee471b5289bf2a8e88 Mon Sep 17 00:00:00 2001 From: zhouzhongping Date: Mon, 6 Nov 2023 14:46:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=9A=E7=A6=BB=E7=BA=BF?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F&=E6=9B=B4=E6=96=B0=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.ini | 3 +-- main.py | 53 +++++++++++++++++++++++++++++++++++++----------- requirements.txt | 7 ++++--- src/config.py | 35 ++++++-------------------------- src/const.py | 13 ++++++------ 5 files changed, 59 insertions(+), 52 deletions(-) diff --git a/config.ini b/config.ini index 6cc2771..3c67034 100644 --- a/config.ini +++ b/config.ini @@ -2,5 +2,4 @@ web = true [blog] -rss = -data = \ No newline at end of file +rss = \ No newline at end of file diff --git a/main.py b/main.py index 853935f..1e8d45f 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,7 @@ from loguru import logger import const import models import tools +from config import Config from generator import Generator app = Flask(__name__) @@ -18,9 +19,12 @@ def home(): @app.route('/painting') def painting(): + # 读取配置文件 + config = Config("config.ini") + # 站点数据 site = models.Site( - service=const.SITE_SERVICE, + service=config.web_status, title=const.SITE_NAME ).to_dict() @@ -30,18 +34,43 @@ def painting(): ).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', - 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 config.web_status == const.SITE_SERVICE_STATIC: + # 静态网站模式 + html_static_file = 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 + ) + + 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__': diff --git a/requirements.txt b/requirements.txt index 044a208..1f2e685 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,9 @@ beautifulsoup4==4.12.2 -blinker==1.6.3 +blinker==1.7.0 certifi==2023.7.22 -charset-normalizer==3.3.1 +charset-normalizer==3.3.2 click==8.1.7 +docopt==0.6.2 feedparser==6.0.10 Flask==3.0.0 idna==3.4 @@ -25,4 +26,4 @@ soupsieve==2.5 textblob==0.17.1 tqdm==4.66.1 urllib3==2.0.7 -Werkzeug==3.0.1 +Werkzeug==3.0.1 \ No newline at end of file diff --git a/src/config.py b/src/config.py index b0a3581..e4e5b74 100644 --- a/src/config.py +++ b/src/config.py @@ -5,6 +5,7 @@ from urllib.parse import urlparse from loguru import logger +import const from src.tools import check_website_status @@ -69,30 +70,6 @@ class Config: 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 def web_status(self): try: @@ -103,10 +80,10 @@ class Config: if web_status is None: logger.error('web 配置值为空') - return True + return const.SITE_SERVICE_WEB - if web_status == "True" or web_status == "true": - return True + if web_status == "True" or web_status == "true" or web_status == "t" or web_status == "T": + return const.SITE_SERVICE_WEB - if web_status == "False" or web_status == "false": - return False + if web_status == "False" or web_status == "false" or web_status == "f" or web_status == "F": + return const.SITE_SERVICE_STATIC diff --git a/src/const.py b/src/const.py index c1fb727..89150f6 100644 --- a/src/const.py +++ b/src/const.py @@ -1,15 +1,16 @@ +# 站点标题 +SITE_NAME = "EndOfYear" + +# 站点服务模式 +SITE_SERVICE_WEB = 1 +SITE_SERVICE_STATIC = 0 + # 时区 TIME_ZONE = "Asia/Shanghai" # 时间格式 FORMAT_TIME = "%Y-%m-%d %H:%M:%S" -# 站点服务模式 -SITE_SERVICE = 1 - -# 站点标题 -SITE_NAME = "EndOfYear" - # 博客文章分类-生活 BLOG_POST_CATEGORY_LIFE = 1