feat: 账号可配置 HTTP/HTTPS/SOCKS 代理
This commit is contained in:
+20
-30
@@ -600,32 +600,33 @@ class AutoStudyWorker:
|
||||
}
|
||||
|
||||
with SessionLocal() as db:
|
||||
users = db.execute(
|
||||
select(Account.employee_id, Account.monthly_target, Account.month_hours,
|
||||
Account.credential_cipher, Account.password_cipher)
|
||||
from euniv.proxy import url_of
|
||||
|
||||
users = db.scalars(
|
||||
select(Account)
|
||||
.where(Account.credential_cipher != "")
|
||||
.where(Account.credential_valid == True)
|
||||
.order_by(Account.employee_id)
|
||||
).all()
|
||||
courses = db.scalars(select(Course).order_by(Course.title)).all()
|
||||
course_list = [(c.path, c.title, c.kind) for c in courses]
|
||||
eligible = []
|
||||
for row in users:
|
||||
target = row.monthly_target or 600
|
||||
current = _parse_minutes(row.month_hours)
|
||||
if target > 0 and current < target:
|
||||
try:
|
||||
password = decrypt_secret(row.password_cipher)
|
||||
credential = json.loads(decrypt_secret(row.credential_cipher))
|
||||
proxy = url_of(row)
|
||||
except Exception:
|
||||
continue
|
||||
eligible.append((row.employee_id, target, password, credential, proxy))
|
||||
|
||||
if not users or not course_list:
|
||||
if not course_list:
|
||||
_set_status(running=False, message="没有可用的用户或课程")
|
||||
return
|
||||
|
||||
eligible = []
|
||||
for row in users:
|
||||
target = row.monthly_target or 600
|
||||
current = _parse_minutes(row.month_hours)
|
||||
if target > 0 and current < target:
|
||||
try:
|
||||
password = decrypt_secret(row.password_cipher)
|
||||
credential = json.loads(decrypt_secret(row.credential_cipher))
|
||||
except Exception:
|
||||
continue
|
||||
eligible.append((row.employee_id, target, password, credential))
|
||||
|
||||
if not eligible:
|
||||
_set_status(running=False, message="所有用户已达标或无有效凭据")
|
||||
return
|
||||
@@ -634,8 +635,8 @@ class AutoStudyWorker:
|
||||
|
||||
sem = asyncio.Semaphore(_MAX_CONCURRENT_USERS)
|
||||
tasks = [
|
||||
self._study_user(emp_id, target, pw, cred, course_list, intervals, auto_mode, sem)
|
||||
for emp_id, target, pw, cred in eligible
|
||||
self._study_user(emp_id, target, pw, cred, proxy, course_list, intervals, auto_mode, sem)
|
||||
for emp_id, target, pw, cred, proxy in eligible
|
||||
]
|
||||
results = await asyncio.gather(*tasks, return_exceptions=True)
|
||||
for emp_id_tuple, result in zip(eligible, results):
|
||||
@@ -646,24 +647,13 @@ class AutoStudyWorker:
|
||||
_set_status(running=False, message="本轮完成")
|
||||
|
||||
async def _study_user(self, employee_id: str, target: int,
|
||||
password: str, credential: dict,
|
||||
password: str, credential: dict, proxy: str | None,
|
||||
course_list: list, intervals: dict,
|
||||
auto_mode: bool, sem: asyncio.Semaphore) -> None:
|
||||
assert self._async_stop is not None
|
||||
async with sem:
|
||||
_set_user_status(employee_id, state="准备", course="")
|
||||
|
||||
# 后续从账号读取 HTTP/SOCKS 代理 URL 和同时学习门数
|
||||
from euniv.proxy import parse_proxy
|
||||
|
||||
proxy = None
|
||||
max_courses = _MAX_COURSE_PER_USER
|
||||
try:
|
||||
proxy = parse_proxy(proxy)
|
||||
except ValueError:
|
||||
_set_user_status(employee_id, state="代理无效")
|
||||
return
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
token, user_id, org_id, cookies = await loop.run_in_executor(
|
||||
None, _prepare_session, employee_id, password, credential, proxy)
|
||||
|
||||
Reference in New Issue
Block a user