mirror of
https://github.com/j93es/browser-use-oauth.git
synced 2026-10-12 07:21:21 +09:00
[Refactor] 파일 구조 변경
This commit is contained in:
parent
b7e6afb227
commit
26b40e0e65
3 changed files with 2 additions and 2 deletions
41
lib/agents/get_sso_list.py
Normal file
41
lib/agents/get_sso_list.py
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
import json
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from lib.prompt.get_sso_list import get_sso_list_task
|
||||||
|
|
||||||
|
from lib.browser_use_utils.run_task import run_task
|
||||||
|
|
||||||
|
|
||||||
|
NOT_FOUND_LOGIN_PAGE = 0
|
||||||
|
FOUND_LOGIN_PAGE = 1
|
||||||
|
|
||||||
|
class FindLoginPageResponse(BaseModel):
|
||||||
|
status: int = NOT_FOUND_LOGIN_PAGE # 0 if not found, 1 if found
|
||||||
|
msg: str | None = None
|
||||||
|
url: str | None = None
|
||||||
|
sso_list: list[str] = [] # List of SSO providers found on the login page
|
||||||
|
|
||||||
|
async def get_sso_list(target_url) -> tuple[bool, str | FindLoginPageResponse | None]:
|
||||||
|
|
||||||
|
task = get_sso_list_task
|
||||||
|
ReturnModel = FindLoginPageResponse
|
||||||
|
success, response = await run_task(target_url, ReturnModel, task)
|
||||||
|
if not success:
|
||||||
|
return False, response
|
||||||
|
if isinstance(response, str):
|
||||||
|
return False, response
|
||||||
|
if isinstance(response, FindLoginPageResponse):
|
||||||
|
if response.status == FOUND_LOGIN_PAGE:
|
||||||
|
if not response.sso_list:
|
||||||
|
response.msg = "로그인 페이지는 찾았지만 SSO 제공자가 없습니다."
|
||||||
|
else:
|
||||||
|
response.msg = "로그인 페이지와 SSO 제공자를 찾았습니다."
|
||||||
|
else:
|
||||||
|
response.msg = "로그인 페이지를 찾지 못했습니다."
|
||||||
|
else:
|
||||||
|
return False, "응답 형식이 올바르지 않습니다. FindLoginPageResponse가 아닙니다."
|
||||||
|
|
||||||
|
return True, response
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
67
lib/browser_use_utils/run_task.py
Normal file
67
lib/browser_use_utils/run_task.py
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
import json
|
||||||
|
from typing import Any
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from browser_use import (
|
||||||
|
Agent,
|
||||||
|
Controller,
|
||||||
|
BrowserSession
|
||||||
|
)
|
||||||
|
from patchright.async_api import async_playwright as async_patchright
|
||||||
|
from lib.utils.logger import logger
|
||||||
|
from lib.prompt.get_sso_list import get_sso_list_task
|
||||||
|
from lib.browser_use_utils.create_google_ai import create_google_ai
|
||||||
|
from lib.browser_use_utils.get_profile import get_profile
|
||||||
|
from lib.browser_use_utils.clean_resources import clean_session_resources, clean_agent_resources
|
||||||
|
from lib.config import GOOGLE_MODEL
|
||||||
|
|
||||||
|
|
||||||
|
async def run_task(target_url: str, ReturnModel: type[BaseModel], task: str) -> tuple[bool, str | Any | None]:
|
||||||
|
session = BrowserSession(
|
||||||
|
playwright=(await async_patchright().start()),
|
||||||
|
browser_profile=await get_profile(),
|
||||||
|
)
|
||||||
|
|
||||||
|
initial_actions = [{"open_tab": {"url": target_url}}]
|
||||||
|
|
||||||
|
controller = Controller(output_model=ReturnModel, exclude_actions=['search_google'])
|
||||||
|
agent = Agent(
|
||||||
|
browser_session=session,
|
||||||
|
initial_actions=initial_actions,
|
||||||
|
task=task,
|
||||||
|
llm=create_google_ai(GOOGLE_MODEL),
|
||||||
|
controller=controller,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = await agent.run()
|
||||||
|
final_result = response.final_result()
|
||||||
|
|
||||||
|
if final_result is None:
|
||||||
|
logger(f"⚠️ 최종 결과가 없습니다. 에이전트 실행 실패: {target_url}")
|
||||||
|
print(f"⚠️ 최종 결과가 없습니다. 에이전트 실행 실패: {target_url}")
|
||||||
|
return False, "최종 결과가 없습니다. 에이전트 실행 실패"
|
||||||
|
except Exception as e:
|
||||||
|
# API 쿼터 문제인지 확인
|
||||||
|
if "ResourceExhausted" in str(e) or "429" in str(e):
|
||||||
|
logger(f"⚠️ API 쿼터 에러로 인한 실패: {target_url} | {e}")
|
||||||
|
print(f"⚠️ API 쿼터 에러로 인한 실패: {target_url} | {e}")
|
||||||
|
return False, "API 쿼터 에러로 인한 실패"
|
||||||
|
# 일반 에러 처리
|
||||||
|
else:
|
||||||
|
logger(f"⚠️ 일반 에러로 인한 실패: {target_url} | {e}")
|
||||||
|
print(f"⚠️ 일반 에러로 인한 실패: {target_url} | {e}")
|
||||||
|
return False, "일반 에러로 인한 실패"
|
||||||
|
finally:
|
||||||
|
await clean_agent_resources(agent)
|
||||||
|
|
||||||
|
try:
|
||||||
|
data = json.loads(final_result)
|
||||||
|
resp = ReturnModel(**data)
|
||||||
|
return True, resp
|
||||||
|
except Exception as e:
|
||||||
|
logger(f"⚠️ LLM 응답 결과 파싱 실패: {target_url} | {e}\n원본 결과: {data.msg}")
|
||||||
|
print(f"⚠️ LLM 응답 결과 파싱 실패: {target_url} | {e}\n원본 결과: {data.msg}")
|
||||||
|
return False, "LLM 응답 결과 파싱 실패"
|
||||||
|
finally:
|
||||||
|
await clean_session_resources(session)
|
||||||
|
|
||||||
2
main.py
2
main.py
|
|
@ -8,7 +8,7 @@ from lib.utils.is_html import is_html_url
|
||||||
from lib.utils.read_txt import read_lines_between
|
from lib.utils.read_txt import read_lines_between
|
||||||
from lib.utils.progress_checker import save_progress, load_progress
|
from lib.utils.progress_checker import save_progress, load_progress
|
||||||
from lib.utils.env_checker import check_env_variables
|
from lib.utils.env_checker import check_env_variables
|
||||||
from lib.get_sso_list import get_sso_list
|
from lib.agents.get_sso_list import get_sso_list
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
check_env_variables()
|
check_env_variables()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue