-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmain.py
More file actions
84 lines (69 loc) · 2.4 KB
/
Copy pathmain.py
File metadata and controls
84 lines (69 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Verify Browserbase built-in and geolocation proxies with Stagehand V4."""
import asyncio
import json
import os
from dotenv import load_dotenv
from pydantic import BaseModel
from stagehand import BrowserbaseProxyConfig, Stagehand, browserbase
load_dotenv()
class GeoInfo(BaseModel):
ip: str
city: str
region: str
country: str
loc: str
timezone: str
org: str
postal: str | None
hostname: str | None
async def test_session(proxies: bool | list[BrowserbaseProxyConfig], name: str) -> GeoInfo:
api_key = os.environ.get("BROWSERBASE_API_KEY")
if not api_key:
raise RuntimeError("BROWSERBASE_API_KEY is required")
print(f"\n=== Testing {name} ===")
browser = await browserbase.launch(api_key=api_key, proxies=proxies)
try:
stagehand = await Stagehand.create(
browser=browser,
)
try:
pages = await browser.context.pages()
page = pages[0] if pages else await browser.context.new_page()
await page.goto("https://ipinfo.io/json", wait_until="domcontentloaded")
extracted = await stagehand.extract(
"Extract the complete IP geolocation record shown in this JSON response",
GeoInfo,
page=page,
)
geo_info = extracted.data
print(json.dumps(geo_info.model_dump(mode="json"), indent=2))
return geo_info
finally:
await stagehand.close()
finally:
await browser.close()
async def main() -> None:
built_in = await test_session(True, "Built-in Proxies")
new_york = await test_session(
[
{
"type": "browserbase",
"geolocation": {"city": "NEW_YORK", "state": "NY", "country": "US"},
}
],
"Geolocation Proxies (New York)",
)
if (
new_york.country != "US"
or new_york.region not in {"New York", "New Jersey"}
or new_york.timezone != "America/New_York"
):
raise RuntimeError(
"Expected a New York metropolitan-area proxy; received "
f"{new_york.city}, {new_york.region}, {new_york.country}"
)
if built_in.ip == new_york.ip:
raise RuntimeError("Built-in and geolocation proxy sessions returned the same IP")
print("\nAll proxy tests completed with distinct IPs")
if __name__ == "__main__":
asyncio.run(main())