账号新增了状态字段

添加适配九游
This commit is contained in:
zayac 2024-03-27 14:01:09 +08:00
parent 1c572c6d30
commit 52ab1aa113
4 changed files with 60 additions and 18 deletions

View File

@ -7,7 +7,7 @@ import requests
from playwright.sync_api import Position, TimeoutError, sync_playwright
from src import logger
from src.entity.account import Account
from src.entity.account import Account, AccountType
from src.entity.database import db
from src.ui import global_signals
@ -39,7 +39,7 @@ def perform_login(playwright, account: Account) -> Account:
page = context.new_page()
page.goto(account.url)
fill_login_form(page, account)
if handle_captcha(page):
if handle_captcha(page,account.type):
account.headers = capture_request_headers(page, account)
logger.info('登录成功')
else:
@ -59,29 +59,57 @@ def fill_login_form(page, account: Account):
logger.info(f'{account.name}登录ing...........')
def handle_captcha(page) -> bool:
def handle_captcha(page, account_type) -> bool:
try:
validate_code = page.wait_for_selector('.geetest_box', state='visible')
validate_code = page.locator('.geetest_box')
validate_code.wait_for(state='visible')
time.sleep(1)
return process_validate_code(validate_code)
return process_validate_code(page,validate_code,account_type)
except TimeoutError:
logger.error('超时了')
return False
def process_validate_code(validate_code):
def process_validate_code(page,validate_code, account_type):
validate_code_buffer = validate_code.screenshot()
img = base64.b64encode(validate_code_buffer).decode('utf-8')
res = base64_api(img=img)
if '|' in res:
click_captcha_positions(validate_code, res)
validate_code.query_selector('.geetest_submit').click()
validate_code.wait_for_element_state('hidden')
logger.debug('验证码点击成功')
return True
#九游滑动验证 需要单独处理
if account_type == AccountType.jy:
res = base64_api(img=img, typeid=33)
drag_btn = validate_code.locator(".geetest_btn")
drag_btn_box = drag_btn.bounding_box()
try:
distance = int(res)
target_x = drag_btn_box['x'] + distance
drag_btn.hover()
page.mouse.down()
# 缓慢滑动元素
step = distance // 10 # 每次滑动的步长
for i in range(0, distance, step):
page.mouse.move(drag_btn_box["x"] + i, drag_btn_box["y"])
page.wait_for_timeout(1 // 10)
# 模拟鼠标移动操作,将元素向 x 轴滑动指定距离
page.mouse.move(target_x, drag_btn_box["y"])
page.mouse.up()
validate_code.wait_for(state='hidden')
logger.debug('验证码点击成功')
return True
except ValueError:
logger.error(f"获取移动距离失败,实际返回内容为:{res}")
return False
else:
logger.error(res)
return False
res = base64_api(img=img)
if '|' in res:
click_captcha_positions(validate_code, res)
validate_code.locator('.geetest_submit').click()
validate_code.wait_for(state='hidden')
logger.debug('验证码点击成功')
return True
else:
logger.error(res)
return False
def click_captcha_positions(validate_code, positions_str):
@ -128,3 +156,11 @@ def persistence(account: Account, headers: dict):
db_account.x_api_token = headers['x-api-token']
session.commit()
logger.info(f'Headers persisted for account {account.name}')
# if __name__ == '__main__':
# with open('C:\\Users\\Administrator\\Desktop\\Snipaste_2024-03-26_21-46-35.png', 'rb') as f:
# base64_data = base64.b64encode(f.read())
# b64 = base64_data.decode('utf-8')
# res = base64_api(img= b64,typeid=33)
# print(res)

View File

@ -1,7 +1,7 @@
from dataclasses import dataclass
from enum import Enum
from sqlalchemy import JSON as Sql_JSON
from sqlalchemy import JSON as Sql_JSON, Integer
from sqlalchemy import Enum as Sql_Enum
from sqlalchemy import ForeignKey, String
from sqlalchemy.orm import Mapped, mapped_column, relationship
@ -13,6 +13,7 @@ from src.entity.database import db
class AccountType(Enum):
ky = 0
hth = 1
jy = 2
@dataclass
@ -23,7 +24,7 @@ class Account(db.Base):
username: Mapped[db.str_required_unique] = mapped_column(comment='账号')
password: Mapped[db.str_required] = mapped_column(comment='密码')
type: Mapped[AccountType] = mapped_column(Sql_Enum(AccountType), default=AccountType.ky, nullable=False,
comment='类型 ky hth')
comment='类型 ky hth jy')
name: Mapped[str] = mapped_column(String(64), nullable=True, comment='别名')
url: Mapped[str] = mapped_column(String(128), nullable=False, comment='url')
x_api_token: Mapped[str] = mapped_column(String(64), nullable=True, comment='x-api-token')
@ -31,3 +32,4 @@ class Account(db.Base):
user_id: Mapped[int] = mapped_column(ForeignKey('ky_user.id'),
nullable=False, comment='关联的用户id')
user: Mapped['user.User'] = relationship('user.User', back_populates='accounts')
status: Mapped[int] = mapped_column(Integer, nullable=True, comment='账号状态,默认0 禁用',default=0)

View File

@ -32,6 +32,8 @@ def get_user_by_username_and_password(username: str, password: str) -> User:
with db.Session() as session:
user = session.query(User).filter(
User.username == username and User.password == password and User.status == 1).one()
# 过滤出所有状态为可用的账户,并将它们存储在 User 对象中
user.accounts = [account for account in user.accounts if account.status == 1]
return user
@ -46,5 +48,7 @@ def get_user_by_telegram_id(telegram_id: int) -> User:
with db.Session() as session:
user = session.query(User).filter(
User.status == 1 and func.find_in_set(telegram_id, User.telegram_ids) > 0).one()
# 过滤出所有状态为可用的账户,并将它们存储在 User 对象中
user.accounts = [account for account in user.accounts if account.status == 1]
user.accounts.sort(key=lambda account: account.type.value)
return user

View File

@ -327,7 +327,7 @@ class Application(QMainWindow):
for col, cell_data in enumerate(data):
cell = self.create_table_cell(cell_data, table, col)
# 使用 row_count 而不是 0
# 使用 row_count 而不是 0l
table.setItem(row_count, col, cell)
self.handle_data_change(table, cell_data, col, account_username, notifications)