-
-
Notifications
You must be signed in to change notification settings - Fork 765
Expand file tree
/
Copy pathrecord.py
More file actions
52 lines (41 loc) · 1.91 KB
/
Copy pathrecord.py
File metadata and controls
52 lines (41 loc) · 1.91 KB
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
'''
Copyright (C) 2017-2026 Bryant Moscon - bmoscon@gmail.com
Please see the LICENSE file for the terms and conditions
associated with this software.
'''
import argparse
import asyncio
from cryptofeed import FeedHandler
from cryptofeed.capture.recorder import metadata_path
from cryptofeed.connection import connection_stats
from cryptofeed.defines import L2_BOOK, TRADES
def parse_args():
parser = argparse.ArgumentParser(description='record live exchange data to a pcap capture')
parser.add_argument('exchange', help='exchange id, e.g. COINBASE')
parser.add_argument('symbols', nargs='+', help='normalized symbols, e.g. BTC-USD')
parser.add_argument('--channels', nargs='+', default=[TRADES, L2_BOOK], help='channels to subscribe')
parser.add_argument('--duration', type=float, default=60.0, help='seconds to record')
parser.add_argument('--out', default='captures/', help='output pcap file or directory')
parser.add_argument('--rotate', type=int, default=None, help='rotate the capture at this many bytes')
return parser.parse_args()
async def main():
args = parse_args()
from cryptofeed.capture import PcapRecorder
recorder = PcapRecorder(args.out, rotate_size=args.rotate)
fh = FeedHandler(record=recorder)
fh.add_feed(args.exchange, symbols=args.symbols, channels=args.channels, callbacks={})
async def stop_later():
await asyncio.sleep(args.duration)
print(f'{args.duration:.0f}s elapsed - stopping')
fh.request_stop()
stopper = asyncio.get_running_loop().create_task(stop_later())
try:
await fh.run_async()
finally:
stopper.cancel()
print(f'\ncapture: {", ".join(recorder.files)}')
print(f'metadata: {metadata_path(recorder.files[0])}')
for conn_id, stats in connection_stats().items():
print(f' {conn_id}: received={stats.received} sent={stats.sent}')
if __name__ == '__main__':
asyncio.run(main())