diff --git a/app.py b/app.py index 9c150b9..d429e38 100644 --- a/app.py +++ b/app.py @@ -4,6 +4,7 @@ import secrets import sqlite3 +from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore from flask import Flask, g, redirect, url_for from flask_bootstrap import Bootstrap5 from flask_login import LoginManager @@ -22,12 +23,6 @@ app.secret_key = secrets.token_hex(16) login_manager = LoginManager() login_manager.init_app(app) -# 设置session保护等级 -login_manager.session_protection = 'strong' - -scheduler = APScheduler() -scheduler.init_app(app) -scheduler.start() # 用户加载函数 @@ -76,6 +71,9 @@ def connect_db(): def query_db(query, args=(), one=False): + # 查看g对象是否存在db属性,如果不存在则创建 + if not hasattr(g, 'db'): + g.db = connect_db() cur = g.db.execute(query, args) rv = [dict((cur.description[idx][0], value) for idx, value in enumerate(row)) for row in cur.fetchall()] @@ -95,7 +93,6 @@ def after_request(result): def check_require_config(): - PANDORA_NEXT_PATH = os.getenv('PANDORA_NEXT_PATH') # 如果PANDORA_NEXT_PATH 为空则检查/data下是否存在config.json if PANDORA_NEXT_PATH is None: @@ -152,15 +149,28 @@ def check_require_config(): from auth import auth from main import main +check_require_config() +init_db() + +#scheduler jobstore +app.config['SCHEDULER_JOBSTORES'] = { + 'default': SQLAlchemyJobStore(url='sqlite:///' + os.path.join(app.config['pandora_path'], DATABASE)) +} +scheduler = APScheduler() +scheduler.init_app(app) +scheduler.start() + def create_app(): - check_require_config() - init_db() app.register_blueprint(auth.auth_bp, url_prefix='/' + app.config['proxy_api_prefix']) app.register_blueprint(main.main_bp, url_prefix='/' + app.config['proxy_api_prefix']) + # 设置日志等级 + import logging + logging.basicConfig() + logging.getLogger('apscheduler').setLevel(logging.DEBUG) return app if __name__ == '__main__': app = create_app() - app.run(debug=True) + app.run(debug=False) diff --git a/main/main.py b/main/main.py index 4770b3d..edbd57e 100644 --- a/main/main.py +++ b/main/main.py @@ -8,7 +8,7 @@ import login_tools import share_tools import pandora_tools -from app import scheduler + main_bp = Blueprint('main', __name__) @@ -16,7 +16,7 @@ @main_bp.route('/manage-users') @login_required def manage_users(): - from app import query_db + from app import query_db,scheduler users = query_db("select * from users") # 将share_list转换为json对象 for user in users: @@ -145,36 +145,43 @@ def refresh(user_id): def refresh_all_user(): - from app import query_db - users = query_db('select * from users') - for user in users: - try: - # jwt解析access_token 检查access_token是否过期 - if 'access_token' not in user or user['access_token'] is None: - continue - else: - token_info = pandora_tools.get_email_by_jwt(user['access_token']) - # 根据exp判断是否过期,如果过期则刷新 - exp_time = datetime.fromtimestamp(token_info['exp']) - if exp_time > datetime.now(): + from app import scheduler, app + with scheduler.app.app_context(): + print(app.config) + from app import query_db + users = query_db('select * from users') + for user in users: + try: + # jwt解析access_token 检查access_token是否过期 + if 'access_token' not in user or user['access_token'] is None: continue - refresh(user['user_id']) - except Exception as e: - logger.error(e) - sync() + else: + token_info = pandora_tools.get_email_by_jwt(user['access_token']) + # 根据exp判断是否过期,如果过期则刷新 + exp_time = datetime.fromtimestamp(token_info['exp']) + if exp_time > datetime.now(): + continue + refresh(user['user_id']) + except Exception as e: + logger.error(e) + sync_pandora() @main_bp.route('/start_timer') @login_required def refresh_task(): + from app import scheduler scheduler.add_job(func=refresh_all_user, trigger='interval', minutes=1, id='my_job') + if not scheduler.running: + scheduler.start() return redirect(url_for('main.manage_users')) @main_bp.route('/kill_timer') @login_required def kill_refresh_task(): - scheduler.remove_job(func=refresh_all_user, trigger='interval', days=7, id='my_job') + from app import scheduler + scheduler.remove_job(id='my_job') return redirect(url_for('main.manage_users')) @@ -183,16 +190,13 @@ def kill_refresh_task(): def refresh_route(user_id): try: refresh(user_id) - sync() + sync_pandora() except Exception as e: return jsonify({'code': 500, 'msg': '刷新失败: ' + str(e)}), 500 return redirect(url_for('main.manage_users')) -# 同步数据至tokens.json -@main_bp.route('/sync') -@login_required -def sync(): +def make_json(): from app import query_db users = query_db("select * from users") tokens = {} @@ -233,6 +237,17 @@ def sync(): with open(os.path.join(current_app.config['pandora_path'], 'tokens.json'), 'w') as f: # 美化json f.write(json.dumps(tokens, indent=4)) - flash('同步成功', 'success') + + +def sync_pandora(): + make_json() pandora_tools.fresh_setup() + + +# 同步数据至tokens.json +@main_bp.route('/sync') +@login_required +def sync(): + sync_pandora() + flash('同步成功', 'success') return redirect(url_for('main.manage_users'))