feat: 添加账号支持选择学习平台
账号新增 platform 字段,登录/刷新课程/发现课程按平台分发;同时把 JS 脚本收进 script/(目录规范调整)。
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
"""账号 / 登录 / 登录记录接口。"""
|
||||
import time
|
||||
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from study_tool.webapp import app
|
||||
|
||||
from study_tool import store
|
||||
from study_tool.config import *
|
||||
from study_tool.models import *
|
||||
from study_tool.platforms import registry
|
||||
from study_tool.platforms.jspx import *
|
||||
from study_tool.runtime import log
|
||||
from study_tool.runtime import RUNS, RUNS_LOCK, log
|
||||
|
||||
def _log_login(username, ok, code, error, elapsed, stage=""):
|
||||
"""每次登录尝试:统一日志 + login_log 表(登录记录页用)。"""
|
||||
@@ -37,8 +40,14 @@ def api_login(b: LoginIn):
|
||||
_log_login(username or b.account_id, False, "no_credential", err, time.time() - t0,
|
||||
"LoginIn")
|
||||
return JSONResponse({"ok": False, "code": "no_credential", "error": err}, status_code=200)
|
||||
pf = registry.get((acc or {}).get("platform") or b.platform)
|
||||
if pf is None or pf.login is None:
|
||||
err = "平台「%s」还没有实现登录,请换一个平台或先在 platforms/ 里补上实现" % (
|
||||
(acc or {}).get("platform") or b.platform)
|
||||
_log_login(username, False, "no_platform", err, time.time() - t0, "LoginIn")
|
||||
return JSONResponse({"ok": False, "code": "no_platform", "error": err}, status_code=200)
|
||||
try:
|
||||
s, user = login(username, password)
|
||||
s, user = pf.login(username, password)
|
||||
except Exception as e:
|
||||
err = str(e)[:300]
|
||||
_log_login(username, False, "login_failed", err, time.time() - t0, type(e).__name__)
|
||||
@@ -49,10 +58,10 @@ def api_login(b: LoginIn):
|
||||
return JSONResponse({"ok": False, "code": "login_failed", "error": err}, status_code=200)
|
||||
warn = ""
|
||||
try:
|
||||
courses = scrape_courses(s)
|
||||
courses = pf.list_courses(s) if pf.list_courses else []
|
||||
except Exception as e:
|
||||
courses, warn = [], str(e)[:200]
|
||||
cookies = dump_cookies(s)
|
||||
cookies = (pf.dump_cookies or dump_cookies)(s)
|
||||
_log_login(username, True, "ok", warn, time.time() - t0, "user=%s" % user)
|
||||
if acc:
|
||||
store.account_save_login(acc["id"], user, cookies, courses)
|
||||
|
||||
Reference in New Issue
Block a user