-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape_ws.py
221 lines (170 loc) · 5.39 KB
/
scrape_ws.py
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
import sys
import os
import websocket
import logging
import inspect
import requests
import time
from gzip import decompress as gzip_decompress
from json import dumps
from itertools import chain
from data_logger import DataLogger
logger = logging.getLogger('scrape_ws')
logging.basicConfig(level=logging.INFO)
def mkdirs_exists_ok(path):
try:
os.makedirs(path)
except OSError:
if not os.path.isdir(path):
raise
class CrashOnlyWebSocketApp(websocket.WebSocketApp):
"""WebSocketApp that does not catch exceptions from callbacks."""
def _callback(self, callback, *args):
if callback:
if inspect.ismethod(callback):
callback(*args)
else:
callback(self, *args)
class StreamConfig(DataLogger):
def __init__(self, source, url, **kwargs):
super().__init__('ws_{}'.format(source), **kwargs)
self.url = url
websocket.enableTrace(False)
self.ws = CrashOnlyWebSocketApp(
self.url,
on_message=self.on_message,
on_error=self.on_error,
on_close=self.on_close)
self.ws.on_open = self.on_open
def start(self):
self.ws.run_forever(ping_timeout=1000)
def on_open(self):
pass
def on_error(self, error):
raise error
def on_message(self, message):
self.log_json(message)
def on_close(self):
logger.error('websocket connection closed')
self._close_logfile()
class BinanceStreamConfig(StreamConfig):
_QUERY_PERIOD = 360.
def __init__(self):
self.markets = (
'btcusdt',
'ethusdt',
'ltcusdt',
'ethbtc',
'trxusdt',
'zrxbtc',
)
streams = (['!ticker@arr'] + list(
chain.from_iterable(
('{}@trade'.format(market), '{}@depth'.format(market))
for market in self.markets)))
super().__init__(
'binance',
'wss://stream.binance.com:9443/ws/{}'.format('/'.join(streams)),
max_output_size_bytes=13000000,
max_file_duration_seconds=10 * 60)
self.last_query_time = None
def on_open(self):
self._maybe_redo_queries()
def on_message(self, message):
self.log_json(message)
self._maybe_redo_queries()
def _maybe_redo_queries(self):
lq = self.last_query_time
now = time.time()
if not (lq is None or now > lq + self._QUERY_PERIOD):
return
logger.info('querying order books')
self.last_query_time = now
for market in self.markets:
result = requests.get('https://www.binance.com/api/v1/depth?symbol={}&limit=1000'.format(market.upper()))
result.raise_for_status()
book_json = result.json()
assert 'e' not in book_json
assert 's' not in book_json
book_json['e'] = 'depth'
book_json['s'] = market
self.log_json(book_json)
time.sleep(1.)
class CoinbaseStreamConfig(StreamConfig):
def __init__(self):
super().__init__('coinbase', 'wss://ws-feed.pro.coinbase.com')
def on_open(self):
subscribe_msg = {
'type': 'subscribe',
'product_ids': [
'ETH-USD',
'BTC-USD',
'LTC-USD',
'ETH-BTC',
],
'channels': ['heartbeat', 'level2', 'full']
}
for product_id in subscribe_msg['product_ids']:
result = requests.get('https://api.pro.coinbase.com/products/{}/book?level=3'.format(product_id))
result.raise_for_status()
book_json = result.json()
book_json['product_id'] = product_id
self.log_json(book_json)
self.ws.send(dumps(subscribe_msg))
class BitmexStreamConfig(StreamConfig):
def __init__(self):
markets = ('XBTUSD', 'XBTU19', 'XBTZ19')
subscription_string = ','.join(
'orderBookL2:{0},trade:{0}'.format(market) for market in markets)
connection_url = 'wss://www.bitmex.com/realtime?subscribe=instrument,{}'.format(
subscription_string)
super().__init__('bitmex', connection_url)
class HuobiStreamConfig(StreamConfig):
def __init__(self):
super().__init__(
'huobi', 'wss://api.huobi.pro/ws',
max_file_duration_seconds=200)
self.last_pong_time = time.time()
def on_message(self, message):
result = gzip_decompress(message)
self.log_json(result)
self._maybe_pong()
def on_open(self):
markets = ('btcusdt', 'ethusdt', 'ethbtc', 'eosusdt', 'bchusdt', 'xrpusdt',
'etcusdt')
subs_fmts = (
'market.{}.detail',
'market.{}.kline.1day',
'market.{}.depth.percent10',
'market.{}.trade.detail',
'market.{}.depth.step0',
)
subs = ['market.overview'] + [sf.format(market) for market in markets for sf in subs_fmts]
for sub in subs:
self.ws.send(dumps({'sub': sub}))
def _maybe_pong(self):
now = time.time()
if now - self.last_pong_time > 4.8:
self.ws.send(dumps({'pong': 1000*int(now)}))
self.last_pong_time = now
def get_config_from_source(source):
if source == 'binance':
return BinanceStreamConfig()
elif source == 'coinbase':
return CoinbaseStreamConfig()
elif source == 'huobi':
return HuobiStreamConfig()
elif source == 'bitmex':
return BitmexStreamConfig()
elif source == 'bittrex':
from bittrex_scraper import BittrexStreamConfig
return BittrexStreamConfig()
else:
raise NotImplementedError(source)
def main():
source = sys.argv[1]
config = get_config_from_source(source)
logger.info('Running with source {}: {}'.format(source, config.url))
config.start()
if __name__ == '__main__':
main()