mirror of
https://github.com/j93es/browser-use-oauth.git
synced 2026-06-04 07:51:52 +09:00
feat: OAuth 리스트 추출 및 로그인 기능 개선
- README.md: uv 실행 명령어 수정 - lib/llm/prompt: OAuth 리스트 추출 및 fallback 프롬프트 추가 - lib/utils/browser_use: 프로필 생성 시 스토리지 상태 파일 처리 개선 - lib/utils/browser_use/func: 안전한 JSON 읽기 및 쓰기 함수 추가 - main.py: OAuth 리스트 추출 및 개별 로그인 시도 통합 - model.py: OAuth 모델 수정
This commit is contained in:
parent
4f90285bdd
commit
4b3637b762
8 changed files with 444 additions and 206 deletions
|
|
@ -1,11 +1,26 @@
|
|||
import os
|
||||
from lib.utils.browser_use.func import *
|
||||
|
||||
# Initialize configuration
|
||||
proxy_url = setup_proxy()
|
||||
|
||||
# Create browser profile
|
||||
async def GetProfile():
|
||||
storage_state_path = await setup_storage_state()
|
||||
|
||||
# Handle potential encoding issues with storage state file
|
||||
try:
|
||||
if storage_state_path and os.path.exists(storage_state_path):
|
||||
# Test if file can be read properly, if not, skip it
|
||||
with open(storage_state_path, 'r', encoding='utf-8') as f:
|
||||
f.read()
|
||||
storage_state = storage_state_path
|
||||
else:
|
||||
print("⚠️ Storage state file not found or inaccessible, proceeding without it.")
|
||||
storage_state = None
|
||||
except (UnicodeDecodeError, FileNotFoundError):
|
||||
# If there's an encoding error, don't use the storage state
|
||||
storage_state = None
|
||||
|
||||
profile = BrowserProfile(
|
||||
# Security settings
|
||||
disable_security=True,
|
||||
|
|
@ -19,7 +34,7 @@ async def GetProfile():
|
|||
|
||||
# Data persistence
|
||||
user_data_dir=None,
|
||||
storage_state=storage_state_path,
|
||||
storage_state=storage_state,
|
||||
|
||||
# Network settings
|
||||
proxy={"server": proxy_url} if proxy_url else None,
|
||||
|
|
@ -28,4 +43,4 @@ async def GetProfile():
|
|||
args=get_browser_args(),
|
||||
)
|
||||
|
||||
return profile
|
||||
return profile
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import os
|
||||
import json
|
||||
from pathlib import Path
|
||||
from dotenv import load_dotenv
|
||||
from browser_use import BrowserProfile
|
||||
|
|
@ -6,6 +7,27 @@ from browser_use import BrowserProfile
|
|||
# Load environment variables
|
||||
load_dotenv(override=True)
|
||||
|
||||
def safe_json_read(file_path: Path) -> dict:
|
||||
"""Safely read JSON file with proper encoding handling."""
|
||||
try:
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
except (UnicodeDecodeError, json.JSONDecodeError):
|
||||
# Try with different encodings
|
||||
for encoding in ['utf-8-sig', 'latin1', 'cp1252']:
|
||||
try:
|
||||
with open(file_path, 'r', encoding=encoding) as f:
|
||||
return json.load(f)
|
||||
except (UnicodeDecodeError, json.JSONDecodeError):
|
||||
continue
|
||||
return {}
|
||||
|
||||
def safe_json_write(file_path: Path, data: dict):
|
||||
"""Safely write JSON file with proper encoding handling."""
|
||||
with open(file_path, 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=4)
|
||||
|
||||
|
||||
def setup_proxy():
|
||||
"""Configure proxy settings from environment variables."""
|
||||
proxy_host = os.getenv("PROXY_HOST")
|
||||
|
|
@ -31,14 +53,27 @@ async def setup_storage_state():
|
|||
print(f"📂 Temp storage state path: {storage_state_temp_path}")
|
||||
|
||||
if storage_state_path.exists():
|
||||
if storage_state_temp_path.exists():
|
||||
storage_state_temp_path.unlink()
|
||||
try:
|
||||
if storage_state_temp_path.exists():
|
||||
storage_state_temp_path.unlink()
|
||||
|
||||
storage_state_temp_path.write_text(
|
||||
storage_state_path.read_text(encoding="utf-8"), encoding="utf-8"
|
||||
)
|
||||
print(f"🔄 Using existing storage state: {storage_state_temp_path}")
|
||||
return str(storage_state_temp_path)
|
||||
# 안전한 JSON 파일 처리 (인코딩 문제 해결)
|
||||
storage_data = safe_json_read(storage_state_path)
|
||||
|
||||
if storage_data: # 데이터가 성공적으로 읽혔다면
|
||||
safe_json_write(storage_state_temp_path, storage_data)
|
||||
print(f"🔄 Using existing storage state: {storage_state_temp_path}")
|
||||
return str(storage_state_temp_path)
|
||||
else:
|
||||
print("⚠️ Storage state file is empty or corrupted")
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
print(f"⚠️ Error processing storage state: {e}")
|
||||
# 문제가 있는 파일을 제거하고 새로 시작
|
||||
if storage_state_temp_path.exists():
|
||||
storage_state_temp_path.unlink()
|
||||
return None
|
||||
|
||||
print("⚠️ No existing storage state found")
|
||||
return None
|
||||
|
|
@ -73,3 +108,19 @@ def get_browser_args():
|
|||
# Language
|
||||
f"--lang={os.getenv('LANG', 'en_US')}",
|
||||
]
|
||||
|
||||
def cleanup_corrupted_storage_files():
|
||||
"""Clean up corrupted storage state files."""
|
||||
script_dir = Path(__file__).parent.parent.parent.parent
|
||||
storage_state_temp_path = script_dir / "data" / "storage_state_temp.json"
|
||||
|
||||
if storage_state_temp_path.exists():
|
||||
try:
|
||||
# Try to read the file to check if it's corrupted
|
||||
with open(storage_state_temp_path, 'r', encoding='utf-8') as f:
|
||||
json.load(f)
|
||||
print(f"✅ Storage temp file is valid: {storage_state_temp_path}")
|
||||
except (UnicodeDecodeError, json.JSONDecodeError) as e:
|
||||
print(f"🗑️ Removing corrupted storage temp file: {e}")
|
||||
storage_state_temp_path.unlink()
|
||||
|
||||
|
|
|
|||
|
|
@ -4,8 +4,12 @@ from pydantic import BaseModel
|
|||
# 출력 모델
|
||||
class OAuth(BaseModel):
|
||||
provider: str
|
||||
oauth_uri: str
|
||||
oauth_uri: str = "" # OAuth 리스트 추출 단계에서는 URI가 없을 수 있음
|
||||
|
||||
|
||||
class OAuthList(BaseModel):
|
||||
oauth_providers: List[OAuth]
|
||||
oauth_providers: List[OAuth]
|
||||
|
||||
|
||||
# 기존 모델 유지 (backward compatibility)
|
||||
BaseModel = OAuthList
|
||||
Loading…
Add table
Add a link
Reference in a new issue