[Update] 레이트 리밋 등의 에러 발생 시 1분 간 멈추고 실행

This commit is contained in:
tv0924@icloud.com 2025-06-08 14:18:39 +09:00
commit 61b6b8b356

157
main.py
View file

@ -133,86 +133,105 @@ async def scan_one_url(url: str, skip_html_check: bool = False):
except Exception as e: except Exception as e:
print(f"⚠️ Failed to notify backend: {e}") print(f"⚠️ Failed to notify backend: {e}")
# 2) Browser + Context 생성 while True:
browser = Browser(config=BrowserConfig(**browser_config_kwargs())) # 2) Browser + Context 생성
context = BrowserContext( browser = Browser(config=BrowserConfig(**browser_config_kwargs()))
browser=browser, context = BrowserContext(
config=BrowserContextConfig( browser=browser,
wait_for_network_idle_page_load_time=3.0, config=BrowserContextConfig(
window_width=1600, wait_for_network_idle_page_load_time=3.0,
window_height=900, window_width=1600,
locale='en-US', window_height=900,
highlight_elements=True, locale='en-US',
viewport_expansion=500, highlight_elements=True,
keep_alive=False viewport_expansion=500,
keep_alive=False
)
) )
)
# 3) Agent, Controller 생성 # 3) Agent, Controller 생성
initial_actions = [ initial_actions = [
{'open_tab': {'url': url}} {'open_tab': {'url': url}}
] ]
controller = make_controller() controller = make_controller()
agent = Agent( agent = Agent(
browser_context=context, browser_context=context,
browser=browser, browser=browser,
initial_actions=initial_actions, initial_actions=initial_actions,
task=f"Navigate to the login page, and collect the OAuth provider buttons and their login URLs. Ignore Passkey.", task=f"Navigate to the login page, and collect the OAuth provider buttons and their login URLs. Ignore Passkey.",
llm=ChatGoogleGenerativeAI(model=os.getenv("GOOGLE_MODEL")), llm=ChatGoogleGenerativeAI(model=os.getenv("GOOGLE_MODEL")),
planner_llm=ChatGoogleGenerativeAI(model=os.getenv("GOOGLE_PLANNER_MODEL")), planner_llm=ChatGoogleGenerativeAI(model=os.getenv("GOOGLE_PLANNER_MODEL")),
controller=controller, controller=controller,
extend_planner_system_message=extend_planner_system_message, extend_planner_system_message=extend_planner_system_message,
retry_delay=60, retry_delay=60,
) )
try:
# 4) 실제 스캔 실행
response = await agent.run()
final_result = response.final_result()
if final_result is None:
raise ValueError("final_result()가 None을 반환했습니다.")
data = json.loads(final_result)
try: try:
oauth_entries: List[OAuth] = [OAuth(**entry) for entry in data["oauth_providers"]] # 4) 실제 스캔 실행
except Exception as e: response = await agent.run()
raise ValueError(f"결과 파싱 실패: {e}\n원본 결과: {final_result}") final_result = response.final_result()
if final_result is None:
raise ValueError("final_result()가 None을 반환했습니다.")
# 5) 결과 출력 data = json.loads(final_result)
print("-" * 50) try:
print(f"🔗 Scanned URL: {url}\n") oauth_entries: List[OAuth] = [OAuth(**entry) for entry in data["oauth_providers"]]
print("🔐 Detected OAuth Providers and URLs:") except Exception as e:
for entry in oauth_entries: raise ValueError(f"결과 파싱 실패: {e}\n원본 결과: {final_result}")
if "<" in entry.oauth_uri or "..." in entry.oauth_uri:
print(f"⚠️ WARNING: {entry.provider} URL may be masked or incomplete:\n{entry.oauth_uri}\n")
else:
print(f"- {entry.provider}: {entry.oauth_uri}")
print("-" * 50)
# 6) CSV에 저장 (append) # 5) 결과 출력
csv_file = "./oauth_providers.csv" print("-" * 50)
file_exists = os.path.isfile(csv_file) print(f"🔗 Scanned URL: {url}\n")
with open(csv_file, "a", newline="", encoding="utf-8") as f: print("🔐 Detected OAuth Providers and URLs:")
writer = csv.writer(f)
if not file_exists:
writer.writerow(["issuer", "provider", "oauth_uri"])
for entry in oauth_entries: for entry in oauth_entries:
writer.writerow([url, entry.provider, entry.oauth_uri]) if "<" in entry.oauth_uri or "..." in entry.oauth_uri:
print(f"✅ OAuth providers saved to {csv_file}\n") print(f"⚠️ WARNING: {entry.provider} URL may be masked or incomplete:\n{entry.oauth_uri}\n")
else:
print(f"- {entry.provider}: {entry.oauth_uri}")
print("-" * 50)
# 7) Agent와 Browser 닫기 # 6) CSV에 저장 (append)
await agent.close() # Agent 내부 작업 정리 csv_file = "./oauth_providers.csv"
await context.close() # 브라우저 컨텍스트 종료 (탭/세션 닫기) file_exists = os.path.isfile(csv_file)
await browser.close() # 실제 브라우저 프로세스 종료 with open(csv_file, "a", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
if not file_exists:
writer.writerow(["issuer", "provider", "oauth_uri"])
for entry in oauth_entries:
writer.writerow([url, entry.provider, entry.oauth_uri])
print(f"✅ OAuth providers saved to {csv_file}\n")
# 7) Agent와 Browser 닫기
await agent.close() # Agent 내부 작업 정리
await context.close() # 브라우저 컨텍스트 종료 (탭/세션 닫기)
await browser.close() # 실제 브라우저 프로세스 종료
# 성공적으로 처리했으므로 반복문 탈출
break
except Exception as e: except Exception as e:
print(f"❌ Error scanning {url}: {e}") print(f"⚠️ 429 에러 발생, 60초 대기 후 재시도합니다. (URL: {url})")
# 에러 발생 시에도 Agent와 Browser는 닫아야 합니다.
await agent.close() # 리소스 정리
await context.close() try:
await browser.close() await agent.close()
except:
pass
try:
await context.close()
except:
pass
try:
await browser.close()
except:
pass
# 1분 대기
await asyncio.sleep(60)
# 반복문을 통해 재시도
continue
async def loop(filepath: str, start_line: int, end_line: int, skip_html_check: bool = False): async def loop(filepath: str, start_line: int, end_line: int, skip_html_check: bool = False):
# 인자값으로 받은 파일 경로와 줄 범위를 통해 도메인 리스트 생성 # 인자값으로 받은 파일 경로와 줄 범위를 통해 도메인 리스트 생성