File size: 9,964 Bytes
d7b3d84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"""
Cloud Example 4: Proxy Usage ๐ŸŒ
===============================

This example demonstrates reliable proxy usage scenarios:
- Different country proxies for geo-restrictions
- IP address and location verification
- Region-specific content access (streaming, news)
- Search result localization by country
- Mobile/residential proxy benefits

Perfect for: Geo-restricted content, location testing, regional analysis

Cost: ~$0.08 (1 task + 6-8 steps with proxy enabled)
"""

import argparse
import os
import time
from typing import Any

import requests
from requests.exceptions import RequestException

# Configuration
API_KEY = os.getenv('BROWSER_USE_API_KEY')
if not API_KEY:
	raise ValueError(
		'Please set BROWSER_USE_API_KEY environment variable. You can also create an API key at https://cloud.browser-use.com/new-api-key'
	)

BASE_URL = os.getenv('BROWSER_USE_BASE_URL', 'https://api.browser-use.com/api/v1')
TIMEOUT = int(os.getenv('BROWSER_USE_TIMEOUT', '30'))
HEADERS = {'Authorization': f'Bearer {API_KEY}', 'Content-Type': 'application/json'}


def _request_with_retry(method: str, url: str, **kwargs) -> requests.Response:
	"""Make HTTP request with timeout and retry logic."""
	kwargs.setdefault('timeout', TIMEOUT)

	for attempt in range(3):
		try:
			response = requests.request(method, url, **kwargs)
			response.raise_for_status()
			return response
		except RequestException as e:
			if attempt == 2:  # Last attempt
				raise
			sleep_time = 2**attempt
			print(f'โš ๏ธ  Request failed (attempt {attempt + 1}/3), retrying in {sleep_time}s: {e}')
			time.sleep(sleep_time)

	raise RuntimeError('Unexpected error in retry logic')


def create_task_with_proxy(instructions: str, country_code: str = 'us') -> str:
	"""
	Create a task with proxy enabled from a specific country.

	Args:
	    instructions: Task description
	    country_code: Proxy country ('us', 'fr', 'it', 'jp', 'au', 'de', 'fi', 'ca')

	Returns:
	    task_id: Unique identifier for the created task
	"""
	print(f'๐ŸŒ Creating task with {country_code.upper()} proxy')
	print(f'๐Ÿ“ Task: {instructions}')

	payload = {
		'task': instructions,
		'llm_model': 'gpt-4.1-mini',
		# Proxy configuration
		'use_proxy': True,  # Required for captcha solving
		'proxy_country_code': country_code,  # Choose proxy location
		# Standard settings
		'use_adblock': True,  # Block ads for faster loading
		'highlight_elements': True,  # Keep highlighting for visibility
		'max_agent_steps': 15,
		# Enable sharing for viewing execution
		'enable_public_share': True,  # Get shareable URLs
	}

	response = _request_with_retry('post', f'{BASE_URL}/run-task', headers=HEADERS, json=payload)

	task_id = response.json()['id']
	print(f'โœ… Task created with {country_code.upper()} proxy: {task_id}')
	return task_id


def test_ip_location(country_code: str) -> dict[str, Any]:
	"""Test IP address and location detection with proxy."""
	task = """
    Go to whatismyipaddress.com and tell me:
    1. The detected IP address
    2. The detected country/location
    3. The ISP/organization
    4. Any other location details shown

    Please be specific about what you see on the page.
    """

	task_id = create_task_with_proxy(task, country_code)
	return wait_for_completion(task_id)


def test_geo_restricted_content(country_code: str) -> dict[str, Any]:
	"""Test access to geo-restricted content."""
	task = """
    Go to a major news website (like BBC, CNN, or local news) and check:
    1. What content is available
    2. Any geo-restriction messages
    3. Local/regional content differences
    4. Language or currency preferences shown

    Note any differences from what you might expect.
    """

	task_id = create_task_with_proxy(task, country_code)
	return wait_for_completion(task_id)


def test_streaming_service_access(country_code: str) -> dict[str, Any]:
	"""Test access to region-specific streaming content."""
	task = """
    Go to a major streaming service website (like Netflix, YouTube, or BBC iPlayer)
    and check what content or messaging appears.

    Report:
    1. What homepage content is shown
    2. Any geo-restriction messages or content differences
    3. Available content regions or language options
    4. Any pricing or availability differences

    Note: Don't try to log in, just observe the publicly available content.
    """

	task_id = create_task_with_proxy(task, country_code)
	return wait_for_completion(task_id)


def test_search_results_by_location(country_code: str) -> dict[str, Any]:
	"""Test how search results vary by location."""
	task = """
    Go to Google and search for "best restaurants near me" or "local news".

    Report:
    1. What local results appear
    2. The detected location in search results
    3. Any location-specific content or ads
    4. Language preferences

    This will show how search results change based on proxy location.
    """

	task_id = create_task_with_proxy(task, country_code)
	return wait_for_completion(task_id)


def wait_for_completion(task_id: str) -> dict[str, Any]:
	"""Wait for task completion and return results."""
	print(f'โณ Waiting for task {task_id} to complete...')

	start_time = time.time()

	while True:
		response = _request_with_retry('get', f'{BASE_URL}/task/{task_id}', headers=HEADERS)
		details = response.json()

		status = details['status']
		steps = len(details.get('steps', []))
		elapsed = time.time() - start_time

		# Build status message
		if status == 'running':
			status_msg = f'๐ŸŒ Proxy task | Step {steps} | โฑ๏ธ  {elapsed:.0f}s | ๐Ÿค– Processing...'
		else:
			status_msg = f'๐ŸŒ Proxy task | Step {steps} | โฑ๏ธ  {elapsed:.0f}s | Status: {status}'

		# Clear line and show status
		print(f'\r{status_msg:<80}', end='', flush=True)

		if status == 'finished':
			print(f'\rโœ… Task completed in {steps} steps! ({elapsed:.1f}s total)' + ' ' * 20)
			return details

		elif status in ['failed', 'stopped']:
			print(f'\rโŒ Task {status} after {steps} steps' + ' ' * 30)
			return details

		time.sleep(3)


def demo_proxy_countries():
	"""Demonstrate proxy usage across different countries."""
	print('\n๐ŸŒ Demo 1: Proxy Countries Comparison')
	print('-' * 45)

	countries = [('us', 'United States'), ('de', 'Germany'), ('jp', 'Japan'), ('au', 'Australia')]

	results = {}

	for code, name in countries:
		print(f'\n๐ŸŒ Testing {name} ({code.upper()}) proxy:')
		print('=' * 40)

		result = test_ip_location(code)
		results[code] = result

		if result.get('output'):
			print(f'๐Ÿ“ Location Result: {result["output"][:200]}...')

		# Show execution URLs
		if result.get('live_url'):
			print(f'๐Ÿ”— Live Preview: {result["live_url"]}')
		if result.get('public_share_url'):
			print(f'๐ŸŒ Share URL: {result["public_share_url"]}')
		elif result.get('share_url'):
			print(f'๐ŸŒ Share URL: {result["share_url"]}')

		print('-' * 40)
		time.sleep(2)  # Brief pause between tests

	# Summary comparison
	print('\n๐Ÿ“Š Proxy Location Summary:')
	print('=' * 30)
	for code, result in results.items():
		status = result.get('status', 'unknown')
		print(f'{code.upper()}: {status}')


def demo_geo_restrictions():
	"""Demonstrate geo-restriction bypass."""
	print('\n๐Ÿšซ Demo 2: Geo-Restriction Testing')
	print('-' * 40)

	# Test from different locations
	locations = [('us', 'US content'), ('de', 'European content')]

	for code, description in locations:
		print(f'\n๐ŸŒ Testing {description} with {code.upper()} proxy:')
		result = test_geo_restricted_content(code)

		if result.get('output'):
			print(f'๐Ÿ“ฐ Content Access: {result["output"][:200]}...')

		time.sleep(2)


def demo_streaming_access():
	"""Demonstrate streaming service access with different proxies."""
	print('\n๐Ÿ“บ Demo 3: Streaming Service Access')
	print('-' * 40)

	locations = [('us', 'US'), ('de', 'Germany')]

	for code, name in locations:
		print(f'\n๐ŸŒ Testing streaming access from {name}:')
		result = test_streaming_service_access(code)

		if result.get('output'):
			print(f'๐Ÿ“บ Access Result: {result["output"][:200]}...')

		time.sleep(2)


def demo_search_localization():
	"""Demonstrate search result localization."""
	print('\n๐Ÿ” Demo 4: Search Localization')
	print('-' * 35)

	locations = [('us', 'US'), ('de', 'Germany')]

	for code, name in locations:
		print(f'\n๐ŸŒ Testing search results from {name}:')
		result = test_search_results_by_location(code)

		if result.get('output'):
			print(f'๐Ÿ” Search Results: {result["output"][:200]}...')

		time.sleep(2)


def main():
	"""Demonstrate comprehensive proxy usage."""
	print('๐ŸŒ Browser Use Cloud - Proxy Usage Examples')
	print('=' * 50)

	print('๐ŸŽฏ Proxy Benefits:')
	print('โ€ข Bypass geo-restrictions')
	print('โ€ข Test location-specific content')
	print('โ€ข Access region-locked websites')
	print('โ€ข Mobile/residential IP addresses')
	print('โ€ข Verify IP geolocation')

	print('\n๐ŸŒ Available Countries:')
	countries = ['๐Ÿ‡บ๐Ÿ‡ธ US', '๐Ÿ‡ซ๐Ÿ‡ท France', '๐Ÿ‡ฎ๐Ÿ‡น Italy', '๐Ÿ‡ฏ๐Ÿ‡ต Japan', '๐Ÿ‡ฆ๐Ÿ‡บ Australia', '๐Ÿ‡ฉ๐Ÿ‡ช Germany', '๐Ÿ‡ซ๐Ÿ‡ฎ Finland', '๐Ÿ‡จ๐Ÿ‡ฆ Canada']
	print(' โ€ข '.join(countries))

	try:
		# Parse command line arguments
		parser = argparse.ArgumentParser(description='Proxy usage examples')
		parser.add_argument(
			'--demo', choices=['countries', 'geo', 'streaming', 'search', 'all'], default='countries', help='Which demo to run'
		)
		args = parser.parse_args()

		print(f'\n๐Ÿ” Running {args.demo} demo(s)...')

		if args.demo == 'countries':
			demo_proxy_countries()
		elif args.demo == 'geo':
			demo_geo_restrictions()
		elif args.demo == 'streaming':
			demo_streaming_access()
		elif args.demo == 'search':
			demo_search_localization()
		elif args.demo == 'all':
			demo_proxy_countries()
			demo_geo_restrictions()
			demo_streaming_access()
			demo_search_localization()

	except requests.exceptions.RequestException as e:
		print(f'โŒ API Error: {e}')
	except Exception as e:
		print(f'โŒ Error: {e}')


if __name__ == '__main__':
	main()