Committed by
GitHub
Merge pull request #11 from craiemer/main
添加日志输出
Showing
1 changed file
with
47 additions
and
33 deletions
| 1 | -from flask import Flask,session,request,redirect,render_template | 1 | +from flask import Flask, session, request, redirect, render_template |
| 2 | import re | 2 | import re |
| 3 | from apscheduler.schedulers.background import BackgroundScheduler | 3 | from apscheduler.schedulers.background import BackgroundScheduler |
| 4 | import subprocess | 4 | import subprocess |
| @@ -6,79 +6,93 @@ import os | @@ -6,79 +6,93 @@ import os | ||
| 6 | from pytz import utc | 6 | from pytz import utc |
| 7 | import logging | 7 | import logging |
| 8 | 8 | ||
| 9 | +# 初始化 Flask 应用 | ||
| 9 | app = Flask(__name__) | 10 | app = Flask(__name__) |
| 10 | -app.secret_key = 'this is secret_key you know ?' | 11 | +app.secret_key = 'this is secret_key you know ?' # 设置 Flask 的密钥,用于 session 加密 |
| 11 | 12 | ||
| 13 | +# 导入蓝图 | ||
| 12 | from views.page import page | 14 | from views.page import page |
| 13 | from views.user import user | 15 | from views.user import user |
| 14 | -app.register_blueprint(page.pb) | ||
| 15 | -app.register_blueprint(user.ub) | 16 | +app.register_blueprint(page.pb) # 注册页面蓝图 |
| 17 | +app.register_blueprint(user.ub) # 注册用户蓝图 | ||
| 16 | 18 | ||
| 19 | +# 首页路由,清空 session | ||
| 17 | @app.route('/') | 20 | @app.route('/') |
| 18 | -def hello_world(): # put application's code here | ||
| 19 | - return session.clear() | 21 | +def hello_world(): |
| 22 | + return session.clear() # 清空 session,用户退出登录 | ||
| 20 | 23 | ||
| 21 | """ | 24 | """ |
| 22 | @app.before_request | 25 | @app.before_request |
| 23 | def before_reuqest(): | 26 | def before_reuqest(): |
| 24 | - pat = re.compile(r'^/static') | ||
| 25 | - if re.search(pat,request.path):return | ||
| 26 | - elif request.path == '/user/login' or request.path == '/user/register':return | ||
| 27 | - elif session.get('username'):return | ||
| 28 | - return redirect('/user/login') | 27 | + pat = re.compile(r'^/static') # 正则匹配静态文件路径 |
| 28 | + if re.search(pat, request.path): # 如果是静态文件,直接返回 | ||
| 29 | + return | ||
| 30 | + elif request.path == '/user/login' or request.path == '/user/register': # 登录或注册页面无需验证 | ||
| 31 | + return | ||
| 32 | + elif session.get('username'): # 如果 session 中有用户名,则允许继续 | ||
| 33 | + return | ||
| 34 | + return redirect('/user/login') # 否则重定向到登录页面 | ||
| 29 | """ | 35 | """ |
| 30 | -#中间件代码逻辑可以优化,以减少重复的 return 语句,并提高可读性: | 36 | + |
| 37 | +# 中间件:处理请求前的逻辑 | ||
| 31 | @app.before_request | 38 | @app.before_request |
| 32 | def before_request(): | 39 | def before_request(): |
| 33 | - # 静态文件路径允许直接访问 | 40 | + # 如果请求的是静态文件路径,允许访问 |
| 34 | if request.path.startswith('/static'): | 41 | if request.path.startswith('/static'): |
| 35 | return | 42 | return |
| 36 | 43 | ||
| 37 | - # 登录和注册页面无需验证会话 | 44 | + # 如果请求的是登录或注册页面,不需要会话验证 |
| 38 | if request.path in ['/user/login', '/user/register']: | 45 | if request.path in ['/user/login', '/user/register']: |
| 39 | return | 46 | return |
| 40 | 47 | ||
| 41 | - # 验证用户是否登录 | 48 | + # 如果 session 中没有用户名,重定向到登录页面 |
| 42 | if not session.get('username'): | 49 | if not session.get('username'): |
| 43 | return redirect('/user/login') | 50 | return redirect('/user/login') |
| 44 | 51 | ||
| 52 | +# 404 错误页面路由 | ||
| 45 | @app.route('/<path:path>') | 53 | @app.route('/<path:path>') |
| 46 | def catch_all(path): | 54 | def catch_all(path): |
| 47 | - return render_template('404.html') | 55 | + return render_template('404.html') # 如果路径不存在,返回 404 页面 |
| 48 | 56 | ||
| 57 | +# 定义定时任务,运行爬虫脚本 | ||
| 49 | def run_script(): | 58 | def run_script(): |
| 50 | - current_dir = os.path.dirname(os.path.abspath(__file__)) | ||
| 51 | - spider_script = os.path.join(current_dir, 'spider', 'main.py') | ||
| 52 | - # cutComments_script = os.path.join(current_dir, 'utils', 'cutComments.py') | ||
| 53 | - # cipingTotal_script = os.path.join(current_dir, 'utils', 'cipingTotal.py') | 59 | + current_dir = os.path.dirname(os.path.abspath(__file__)) # 获取当前脚本的目录 |
| 60 | + spider_script = os.path.join(current_dir, 'spider', 'main.py') # 爬虫脚本路径 | ||
| 61 | + # cutComments_script = os.path.join(current_dir, 'utils', 'cutComments.py') # 评论处理脚本路径 | ||
| 62 | + # cipingTotal_script = os.path.join(current_dir, 'utils', 'cipingTotal.py') # 评分处理脚本路径 | ||
| 54 | 63 | ||
| 64 | + # 定义所有要运行的脚本 | ||
| 55 | scripts = [ | 65 | scripts = [ |
| 56 | ("Spider Script", spider_script), | 66 | ("Spider Script", spider_script), |
| 57 | # ("Cut Comments Script", cutComments_script), | 67 | # ("Cut Comments Script", cutComments_script), |
| 58 | # ("Ciping Total Script", cipingTotal_script) | 68 | # ("Ciping Total Script", cipingTotal_script) |
| 59 | ] | 69 | ] |
| 60 | 70 | ||
| 71 | + # 执行所有脚本 | ||
| 61 | for script_name, script_path in scripts: | 72 | for script_name, script_path in scripts: |
| 62 | try: | 73 | try: |
| 63 | - print(f"Running {script_name}...") | ||
| 64 | - subprocess.run(['python', script_path], check=True) | ||
| 65 | - print(f"{script_name} finished successfully.") | 74 | + print(f"Running {script_name}...") # 打印运行开始的信息 |
| 75 | + subprocess.run(['python', script_path], check=True) # 使用 subprocess 执行脚本 | ||
| 76 | + print(f"{script_name} finished successfully.") # 打印脚本成功完成的消息 | ||
| 66 | except subprocess.CalledProcessError as e: | 77 | except subprocess.CalledProcessError as e: |
| 67 | - print(f"An error occurred while running {script_name}: {e}") | ||
| 68 | - | 78 | + print(f"An error occurred while running {script_name}: {e}") # 打印错误信息 |
| 69 | 79 | ||
| 80 | +# 主程序入口 | ||
| 70 | if __name__ == '__main__': | 81 | if __name__ == '__main__': |
| 71 | - scheduler = BackgroundScheduler(timezone=utc) | ||
| 72 | - scheduler.add_job(run_script, 'interval', hours=5) | ||
| 73 | - scheduler.start() | 82 | + # 设置定时任务,定期执行爬虫脚本 |
| 83 | + scheduler = BackgroundScheduler(timezone=utc) # 创建后台任务调度器 | ||
| 84 | + scheduler.add_job(run_script, 'interval', hours=5) # 每5小时执行一次爬虫脚本 | ||
| 85 | + scheduler.start() # 启动调度器 | ||
| 74 | 86 | ||
| 75 | try: | 87 | try: |
| 76 | - app.run() | 88 | + app.run() # 启动 Flask 应用 |
| 77 | finally: | 89 | finally: |
| 78 | - scheduler.shutdown() | 90 | + scheduler.shutdown() # 确保在应用关闭时关闭调度器 |
| 91 | + | ||
| 92 | +# 设置日志记录,捕获应用的请求信息 | ||
| 93 | +logging.basicConfig(level=logging.INFO) # 配置日志记录,设置日志级别为 INFO | ||
| 79 | 94 | ||
| 80 | -#为了更好地调试和监控,建议为应用添加日志记录,捕获用户请求和错误: | ||
| 81 | -logging.basicConfig(level=logging.INFO) | ||
| 82 | @app.before_request | 95 | @app.before_request |
| 83 | def log_request_info(): | 96 | def log_request_info(): |
| 84 | - logging.info(f"Request: {request.method} {request.path}") | 97 | + # 记录每次请求的信息,便于调试和监控 |
| 98 | + logging.info(f"Request: {request.method} {request.path}") # 记录请求的方式(GET/POST)和路径 |
-
Please register or login to post a comment