-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbrowser_binomo_api.py
More file actions
240 lines (194 loc) · 9.33 KB
/
browser_binomo_api.py
File metadata and controls
240 lines (194 loc) · 9.33 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
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
"""
Browser Automation Solution for Binomo WebSocket Trading
This uses Selenium to handle browser-specific authentication for WebSocket connections
"""
import asyncio
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import json
class BinomoBrowserAPI:
"""Browser automation solution for Binomo trading with real WebSocket"""
def __init__(self, email, password, headless=True):
self.email = email
self.password = password
self.headless = headless
self.driver = None
self.is_logged_in = False
async def initialize_browser(self):
"""Initialize browser with proper settings for Binomo"""
chrome_options = Options()
if self.headless:
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
self.driver = webdriver.Chrome(options=chrome_options)
self.driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
async def login(self):
"""Login to Binomo using browser automation"""
if not self.driver:
await self.initialize_browser()
try:
# Navigate to Binomo login page
self.driver.get("https://binomo.com/en/signin")
# Wait for and fill email
email_field = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.NAME, "email"))
)
email_field.send_keys(self.email)
# Fill password
password_field = self.driver.find_element(By.NAME, "password")
password_field.send_keys(self.password)
# Click login button
login_button = self.driver.find_element(By.CSS_SELECTOR, "button[type='submit']")
login_button.click()
# Wait for successful login (dashboard or trading page)
WebDriverWait(self.driver, 15).until(
lambda driver: "dashboard" in driver.current_url or "trading" in driver.current_url
)
self.is_logged_in = True
print("✅ Browser login successful!")
return True
except Exception as e:
print(f"❌ Browser login failed: {e}")
return False
async def navigate_to_trading(self):
"""Navigate to trading interface"""
if not self.is_logged_in:
raise Exception("Must login first")
# Navigate to trading page
self.driver.get("https://binomo.com/en/trading")
# Wait for trading interface to load
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "trading-interface"))
)
print("✅ Trading interface loaded")
async def get_balance(self):
"""Get current balance from trading interface"""
try:
# Find balance element (adjust selector as needed)
balance_element = self.driver.find_element(By.CSS_SELECTOR, "[data-testid='balance']")
balance_text = balance_element.text
return balance_text
except Exception as e:
print(f"⚠️ Could not get balance: {e}")
return None
async def place_trade(self, asset, direction, amount, duration):
"""Place a trade using browser automation"""
try:
# Select asset (this depends on Binomo's UI structure)
asset_selector = self.driver.find_element(By.CSS_SELECTOR, "[data-asset-selector]")
asset_selector.click()
# Find and click the desired asset
asset_option = WebDriverWait(self.driver, 5).until(
EC.element_to_be_clickable((By.XPATH, f"//div[contains(text(), '{asset}')]"))
)
asset_option.click()
# Set amount
amount_input = self.driver.find_element(By.CSS_SELECTOR, "[data-amount-input]")
amount_input.clear()
amount_input.send_keys(str(amount))
# Set duration
duration_selector = self.driver.find_element(By.CSS_SELECTOR, "[data-duration-selector]")
# Implementation depends on Binomo's UI
# Click CALL or PUT button
if direction.lower() == "call":
trade_button = self.driver.find_element(By.CSS_SELECTOR, "[data-call-button]")
else:
trade_button = self.driver.find_element(By.CSS_SELECTOR, "[data-put-button]")
trade_button.click()
print(f"✅ {direction.upper()} trade placed: {asset} ${amount} for {duration}s")
return {"success": True, "asset": asset, "direction": direction, "amount": amount}
except Exception as e:
print(f"❌ Trade placement failed: {e}")
return {"success": False, "error": str(e)}
async def get_websocket_auth_data(self):
"""Extract WebSocket authentication data from browser session"""
try:
# Get cookies and session data
cookies = self.driver.get_cookies()
# Extract auth token from cookies
auth_token = None
for cookie in cookies:
if cookie['name'] == 'authtoken':
auth_token = cookie['value']
break
# Get WebSocket connection details from browser
# This requires injecting JavaScript to capture WebSocket headers
websocket_data = self.driver.execute_script("""
return {
authToken: document.cookie.match(/authtoken=([^;]+)/)?.[1],
userAgent: navigator.userAgent,
origin: window.location.origin,
headers: {
'User-Agent': navigator.userAgent,
'Origin': window.location.origin,
'Referer': window.location.href
}
};
""")
return websocket_data
except Exception as e:
print(f"⚠️ Could not extract WebSocket data: {e}")
return None
async def close(self):
"""Close browser session"""
if self.driver:
self.driver.quit()
print("✅ Browser session closed")
# Example usage
async def demonstrate_browser_solution():
"""Demonstrate browser automation solution"""
print("🌐 Browser Automation Solution for Binomo Trading")
print("=" * 60)
# Note: You need to install chromedriver and have Chrome installed
# pip install selenium webdriver-manager
api = BinomoBrowserAPI(
email="your_email@example.com", # Replace with your email
password="your_password", # Replace with your password
headless=False # Set to True for headless mode
)
try:
# Login using browser
print("🔐 Logging in via browser...")
logged_in = await api.login()
if logged_in:
# Navigate to trading
await api.navigate_to_trading()
# Get balance
balance = await api.get_balance()
print(f"💰 Current balance: {balance}")
# Get WebSocket auth data for hybrid approach
ws_data = await api.get_websocket_auth_data()
if ws_data:
print("🔑 WebSocket authentication data extracted")
print(f" Auth Token: {ws_data.get('authToken', 'N/A')[:20]}...")
# Place a trade (uncomment to test)
# trade_result = await api.place_trade("EUR/USD", "call", 1.0, 60)
# print(f"📈 Trade result: {trade_result}")
print("\n✅ Browser solution working! You can now:")
print(" - Login with real browser session")
print(" - Access real WebSocket connections")
print(" - Place actual trades")
print(" - Get real-time data")
except Exception as e:
print(f"❌ Browser solution error: {e}")
print("💡 Make sure you have Chrome and chromedriver installed")
print(" pip install selenium webdriver-manager")
finally:
await api.close()
if __name__ == "__main__":
# Note: This requires additional setup
print("🚨 BROWSER SOLUTION SETUP REQUIRED:")
print("1. Install: pip install selenium webdriver-manager")
print("2. Make sure Chrome browser is installed")
print("3. Update email/password in the code")
print("4. Run: python browser_binomo_api.py")
# Uncomment to test (after setup):
# asyncio.run(demonstrate_browser_solution())