Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/todo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# todo

### Add login system
- Not Done
Binary file modified pocketoptionapi/__pycache__/pocket.cpython-311.pyc
Binary file not shown.
Binary file modified pocketoptionapi/backend/ws/__pycache__/client.cpython-311.pyc
Binary file not shown.
4 changes: 4 additions & 0 deletions pocketoptionapi/backend/ws/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ def on_message(self, ws, message):
# self.api.send_websocket_request(msg="40")
self.logger.info("Wup Wup, we are sending the 40 request!")
ws.send(b'40')
if data == '2':
self.logger.info("Sending the 3 request!")
ws.send(b'3')

def on_error(self, ws, error):
print(error)
Expand All @@ -86,6 +89,7 @@ def on_open(self, ws):
self.logger.info("Opened!")
if self.pocket_api_instance:
self.pocket_api_instance.connected_event.set()
self.ws.run_forever() # test

def run(self):
self.ws.run_forever() # Use dispatcher for automatic reconnection
65 changes: 58 additions & 7 deletions pocketoptionapi/pocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
import urllib
import websocket
import logging
import time
from websocket._exceptions import WebSocketException

class PocketOptionApi:
def __init__(self, token) -> None:
def __init__(self, init_msg) -> None:
self.ws_url = "wss://api-fin.po.market/socket.io/?EIO=4&transport=websocket"
self.token = token
self.token = "TEST_TOKEN"
self.connected_event = threading.Event()
self.client = WebSocketClient(self.ws_url)
self.logger = logging.getLogger(__name__)
self.logger.setLevel(logging.INFO)
self.init_msg = init_msg
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')

self.websocket_client = WebSocketClient(self.ws_url, pocket_api_instance=self)
Expand All @@ -26,7 +29,7 @@ def __init__(self, token) -> None:
file_handler.setFormatter(formatter)
self.logger.addHandler(file_handler)

self.logger.info(f"initialiting Pocket API with token: {token}")
self.logger.info(f"initialiting Pocket API with token: {self.token}")

self.ping_thread = threading.Thread(target=self.auto_ping) # Create a new thread for auto_ping
self.ping_thread.daemon = True # Set the thread as a daemon so it will terminate when the main program terminates
Expand All @@ -37,7 +40,7 @@ def auto_ping(self):
self.logger.info("Starting auto ping thread")

while True:
pause.seconds(5) # Assuming that you've imported 'pause'
pause.seconds(1) # Assuming that you've imported 'pause'
try:
if self.websocket_client.ws.sock and self.websocket_client.ws.sock.connected: # Check if socket is connected
self.ping()
Expand Down Expand Up @@ -88,8 +91,20 @@ def connect(self):

self.logger.info("Connection successful.")

pause.seconds(5)
self.send_websocket_request(msg="40")
time.sleep(3)
self._login(init_msg=self.init_msg)

pause.seconds(5)
try:
self.send_websocket_request(msg="40")
time.sleep(3)
self._login(init_msg=self.init_msg)
except WebSocketException as e:
self.logger.error(f"A error ocured with weboscket... Error: {e}")
pause.seconds(5)
self.send_websocket_request(msg="40")
self._login(init_msg=self.init_msg)
except Exception as e:
print(f"Going for exception.... error: {e}")
self.logger.error(f"Connection failed with exception: {e}")
Expand All @@ -110,7 +125,6 @@ def default(obj):
try:
self.logger.info("Request sent successfully.")
self.websocket_client.ws.send(bytearray(urllib.parse.quote(data).encode('utf-8')), opcode=websocket.ABNF.OPCODE_BINARY)
pause.seconds(5)
return True
except Exception as e:
self.logger.error(f"Failed to send request with exception: {e}")
Expand All @@ -123,9 +137,46 @@ def default(obj):
self.logger.warning(f"Was not able to reconnect: {e}")
pause.seconds(5)

def login(self, init_msg):
def _login(self, init_msg):
self.logger.info("Trying to login...")

self.websocket_thread = threading.Thread(target=self.websocket_client.ws.run_forever, kwargs={
'sslopt': {
"check_hostname": False,
"cert_reqs": ssl.CERT_NONE,
"ca_certs": "cacert.pem"
},
"ping_interval": 0,
'skip_utf8_validation': True,
"origin": "https://pocketoption.com",
# "http_proxy_host": '127.0.0.1', "http_proxy_port": 8890
})

self.websocket_thread.daemon = True
self.websocket_thread.start()

self.logger.info("Login thread initialised successfully!")

self.send_websocket_request(msg=init_msg)

time.sleep(3)

try:
self.websocket_client.ws.run_forever()
except WebSocketException as e:
self.logger.error(f"A error ocured with websocket: {e}")
pause.seconds(3)
self.websocket_client.reconnect
self.send_websocket_request(msg=init_msg)
try:
self.websocket_client.ws.run_forever()
self.send_websocket_request(msg=init_msg)
except Exception as e:
self.logger.error(f"Trying again failed, skiping... error: {e}")
time.sleep(5)
self.send_websocket_request(msg=init_msg)
time.sleep(10)

@property
def ping(self):
self.send_websocket_request(msg="3")
Expand Down
7 changes: 3 additions & 4 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from pocketoptionapi.pocket import PocketOptionApi

pocketapi = PocketOptionApi(token="d8fa39dd2f2f58e34e8640fd61f054c2")
import time

data = """42["auth",{"session":"a:4:{s:10:\"session_id\";s:32:\"d8fa39dd2f2f58e34e8640fd61f054c2\";s:10:\"ip_address\";s:10:\"90.36.9.15\";s:10:\"user_agent\";s:120:\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 OP\";s:13:\"last_activity\";i:1707062743;}6b3f96969615d299c7bbbbb5b2a5ddcd","isDemo":1,"uid":27658142,"platform":1}]"""
pocketapi = PocketOptionApi(init_msg=data)

pocketapi.connect()
pocketapi.login(init_msg=data)
pocketapi.connect()