Commit 1a85ce89 by baiquan

获取token

parent 4ea0e1b3
import re
import time
from DrissionPage.errors import *
from datetime import datetime, timedelta
from DrissionPage import Chromium
from DrissionPage._functions.by import By
from DrissionPage.errors import *
from loguru import logger
from dao.db import find_account_by_environment_id
......@@ -99,8 +102,32 @@ async def page_login(browser_id):
logger.error(f"{browser_id}-->{e}")
except Exception as e:
logger.error(f"{browser_id}-->{e}")
for _ in range(3):
if tab.title == "首页":
set_token(tab)
tab.close()
return
tab.wait(3)
raise AppError("登录失败-->未获取到token")
def set_token(tab):
token_pattern = r'"token":\s*"([a-f0-9]{32})"'
token_match = re.search(token_pattern, tab.html)
if token_match:
token = token_match.group(1)
# 设置 30 天后过期
expires = (datetime.now() + timedelta(days=30)).strftime("%a, %d %b %Y %H:%M:%S GMT")
cookies = (
f'_shop_token_={token}; '
'path=/; '
'domain=.fxg.jinritemai.com; '
f'expires={expires}; ' # 指定过期时间
'Secure; ' # 仅 HTTPS 传输
'HttpOnly' # 禁止 JS 访问
)
tab.set.cookies(cookies)
else:
raise AppError("未找到 token")
def listen_login(tab):
for packet in tab.listen.steps(timeout=10):
......
......@@ -181,8 +181,7 @@ async def run():
}
try:
tasks = await get_task(upload_data)
# tasks = {"data": [{"app_name": "admin", "type": 1, "browser_type": 1}]}
# tasks = {"data": [{"app_name": "admin", "type": 1, "browser_type": 1, "browser_id": "1075653218"},
# tasks = {"data": [{"app_name": "admin", "type": 1, "browser_type": 1, "browser_id": "1075653218"},]}
# {"app_name": "admin", "type": 1, "browser_type": 1, "browser_id": "1075653218"},
# {"app_name": "admin", "type": 1, "browser_type": 1, "browser_id": "1075653218"},
# {"app_name": "admin", "type": 1, "browser_type": 1, "browser_id": "1075653218"},
......
......@@ -24,3 +24,8 @@ class ABogusParamsError(AppError):
"""a_bogus参数异常"""
def __init__(self, msg='a_bogus参数错误', data=None):
super().__init__(code=401, msg=msg, data=data)
class CookiesExpiredError(AppError):
"""cookies过期异常"""
def __init__(self, msg='cookies过期', data=None):
super().__init__(code=402, msg=msg, data=data)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment