Created
December 7, 2017 03:19
-
-
Save eggman/14afb8071b5623a6b48be8de25ad9226 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# https://www.bitstamp.net/websocket/ | |
# | |
from datetime import datetime, timedelta, timezone | |
import sched, time | |
import json | |
import pusherclient | |
JST = timezone(timedelta(hours=+9), 'JST') | |
class BitstampStream(): | |
pusher_key = "de504dc5763aeef9ff52" | |
pusher_channel = "live_trades" | |
pusher_event = "trade" | |
def __init__(self): | |
def callback(temp): | |
self.on_message(temp) | |
def connect_handler(data): | |
self.on_open() | |
channel = self.pusher.subscribe(BitstampStream.pusher_channel) | |
channel.bind(BitstampStream.pusher_event, callback) | |
self.pusher = pusherclient.Pusher(BitstampStream.pusher_key) | |
self.pusher.connection.bind('pusher:connection_established', connect_handler) | |
try: | |
self.run() | |
except KeyboardInterrupt: | |
self.on_close() | |
def run(self): | |
self.pusher.connect() | |
print("### run ###") | |
while True: | |
time.sleep(1) | |
def on_message(self, message): | |
now = datetime.now(JST) | |
print(str(now), message) | |
def on_close(self): | |
print("### closed ###") | |
def on_open(self): | |
print("### open ###") | |
if __name__=="__main__": | |
BitstampStream() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment