Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sanic import Sanic
- from sanic.response import json
- from sanic.response import stream
- import asyncio
- import psycopg2
- import ccxt.async_support as ccxt
- import json
- import logging
- logger = logging.getLogger("ozzo")
- try:
- conn = psycopg2.connect("dbname='postgres' user='postgres' host='localhost' port='5432' password='password'")
- except:
- print("I am unable to connect to the database")
- app = Sanic()
- kraken = ccxt.kraken({
- 'apiKey': "hEvQNMDIeoCJbr7W/ZBb5CGOrx3G0lWF5B3zqa1JBxdZlEaL8EK+D0Mw",
- 'secret': "JaE9wI6Nwgh5oRxiHcVxurwzwBxwc05W/qv/k1srGg4s3EYuXPpNkLLM5NYbbWpM8rCyijIeDavRuqWbU0ZV9A==",
- })
- async def poll(seconds):
- while True:
- try:
- yield await asyncio.Task(kraken.fetch_ticker('BTC/USD'))
- except RuntimeError:
- logger.exception("Something went wrong while calling kraken")
- if seconds * 1000 < kraken.rateLimit:
- raise ("poll rate exceed the rate limit")
- await asyncio.sleep(seconds)
- @app.get('/')
- async def test(request):
- async def stream_from_exchange(response):
- async for ticker in poll(5):
- payload = json.dumps(ticker)
- print("payload:", payload)
- await response.write(f"{payload}\n")
- return stream(
- stream_from_exchange,
- content_type='application/stream+json'
- )
- async def main():
- async for ticker in poll(5):
- print(ticker)
- if __name__ == '__main__':
- app.run(host='0.0.0.0', port=8000)
Add Comment
Please, Sign In to add comment