-
-
Notifications
You must be signed in to change notification settings - Fork 690
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ability to log/store raw exchange updates for debugging purposes
- Loading branch information
Showing
5 changed files
with
90 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
''' | ||
Copyright (C) 2017-2019 Bryant Moscon - [email protected] | ||
Please see the LICENSE file for the terms and conditions | ||
associated with this software. | ||
''' | ||
import atexit | ||
from collections import defaultdict | ||
|
||
import aiofiles | ||
import aiofiles.os | ||
|
||
|
||
class AsyncFileCallback: | ||
def __init__(self, path, length=10000, rotate=1024 * 1024 * 100): | ||
self.path = path | ||
self.length = length | ||
self.data = defaultdict(list) | ||
self.rotate = rotate | ||
self.count = defaultdict(int) | ||
atexit.register(self.__del__) | ||
|
||
def __del__(self): | ||
for uuid in list(self.data.keys()): | ||
with open(f"{self.path}/{uuid}.{self.count[uuid]}", 'a') as fp: | ||
fp.write("\n".join(self.data[uuid])) | ||
|
||
async def write(self, uuid): | ||
p = f"{self.path}/{uuid}.{self.count[uuid]}" | ||
async with aiofiles.open(p, mode='a') as fp: | ||
await fp.write("\n".join(self.data[uuid])) | ||
self.data[uuid] = [] | ||
|
||
stats = await aiofiles.os.stat(p) | ||
if stats.st_size >= self.rotate: | ||
self.count[uuid] += 1 | ||
|
||
async def __call__(self, data: str, timestamp: float, uuid: str): | ||
self.data[uuid].append(f"{timestamp}: {data}") | ||
if len(self.data[uuid]) >= self.length: | ||
await self.write(uuid) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
''' | ||
Copyright (C) 2017-2019 Bryant Moscon - [email protected] | ||
Please see the LICENSE file for the terms and conditions | ||
associated with this software. | ||
''' | ||
from cryptofeed.util.async_file import AsyncFileCallback | ||
from cryptofeed import FeedHandler | ||
from cryptofeed.exchanges import Coinbase | ||
from cryptofeed.defines import L2_BOOK | ||
|
||
|
||
def main(): | ||
f = FeedHandler(raw_message_capture=AsyncFileCallback('./')) | ||
f.add_feed(Coinbase(pairs=['BTC-USD'], channels=[L2_BOOK])) | ||
|
||
f.run() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains 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