~dcrck/alphahub-listener unlisted

4333c75c50dfccf055d3b3625aa6601c75581a4d — Derek Meer 11 months ago
initial commit
3 files changed, 64 insertions(+), 0 deletions(-)

A .gitignore
A __main__.py
A creds.py
A  => .gitignore +1 -0
@@ 1,1 @@
__pycache__

A  => __main__.py +61 -0
@@ 1,61 @@
# for listening to websockets messages
import asyncio
import websockets
# for retrieving API tokens from the AlphaHub server
import requests
from creds import email, password

import json

SERVER = { "hostname": "alphahub.us" }
IDS = [14, 16, 17]

def sockets_uri(server):
    return "wss://" + server["hostname"]

def http_uri(server):
    return "https://" + server["hostname"]

def authenticate():
    url = http_uri(SERVER) + "/api/v1/session"
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    data = {'user[email]': email, 'user[password]': password}
    response = requests.post(url, data, headers=headers)
    json = response.json()
    return json["data"]

async def handle_messages(websocket):
    async for json_message in websocket:
        message = json.loads(json_message)
        channel = message[2]
        topic = message[3]
        contents = message[4]
        if contents["open"]:
            print("new open signals on", channel, ":", contents["open"])
        if contents["close"]:
            print("new close signals on", channel, ":", contents["close"])
        if not contents["open"] and not contents["close"]:
            print("channel: ", channel)
            print("contents: ", contents)


async def send_join_messages(websocket, ids):
    for id in ids:
        join_message = [None, None, "algorithms:" + str(id), "phx_join", {}]
        await websocket.send(json.dumps(join_message))
        await websocket.recv()

async def consumer(credentials, ids):
    url = sockets_uri(SERVER) + "/socket/websocket?api_token=" + credentials["token"] + "&vsn=2.0.0"
    async with websockets.connect(url) as websocket:
        await send_join_messages(websocket, ids)
        await handle_messages(websocket)

print("Retrieving tokens...")
tokens = authenticate()
print("Tokens retrieved")

print("listening for new signals...")
loop = asyncio.get_event_loop()
loop.run_until_complete(consumer(tokens, IDS))
loop.run_forever()

A  => creds.py +2 -0
@@ 1,2 @@
email = "YOUR_EMAIL"
password = "YOUR_PASSWORD"