前一回的內容需要更正部分程式碼
str()
把 python dict 直接硬幹成字串,如下方註解處if 'event' in msg_dict and msg_dict['event'] == 'data':
print(f'data: {msg_dict}')
# self.redis_client.publish('mtx', str(msg_dict['data']))
self.redis_client.publish('mtx', json.dumps(msg_dict['data']))
"{'symbol': 'MXFJ4', 'type': 'FUTURE_AH', 'exchange': 'TAIFEX', 'trades': [{'price': 22864, 'size': 1, 'bid': 22864, 'ask': 22866}], 'total': {'tradeVolume': 63567, 'totalBidMatch': 47142, 'totalAskMatch': 46315}, 'time': 1727433578458000, 'serial': 77643}"
- 這會導致之後從 Redis 拿資料出來,無法正確被解析成 json
- 跟這個人一樣: https://stackoverflow.com/questions/39491420/python-jsonexpecting-property-name-enclosed-in-double-quotes
json.dumps(msg_dict['data']
而非 str(msg_dict['data'])
'{"symbol": "MXFJ4", "type": "FUTURE_AH", "exchange": "TAIFEX", "trades": [{"price": 22868, "size": 1, "bid": 22866, "ask": 22868}], "total": {"tradeVolume": 63553, "totalBidMatch": 47132, "totalAskMatch": 46308}, "time": 1727433572066000, "serial": 77625}'
我們已經成功地將行情塞進 Redis 的頻道裡。 還差一步就可以產出檔案。
09:00.000 ~ 09:04.999
這段時間屬於第一個檔案、09:05.000 ~ 09:09.999
這段時間屬於第二個檔案。 免不了提早上線開始監聽。>>> d1 = dt.datetime(2024, 9, 27, 21, 0)
>>> d2 = dt.datetime(2024, 9, 27, 21, 5)
>>> d1.timestamp()
1727442000.0
>>> d2.timestamp()
1727442300.0
>>> 1727442000.0/300
5758140.0
>>> 1727442300.0/300
5758141.0
127.0.0.1:6379> publish mtx '{"symbol": "MXFJ4", "type": "FUTURE_AH", "exchange": "TAIFEX", "trades": [{"price": 22868, "size": 1, "bid": 22866, "ask": 22868}], "total": {"tradeVolume": 63553, "totalBidMatch": 47132, "totalAskMatch": 46308}, "time": 1727539322181337, "serial": 77625}'
(integer) 2
127.0.0.1:6379> publish mtx '{"symbol": "MXFJ4", "type": "FUTURE_AH", "exchange": "TAIFEX", "trades": [{"price": 22868, "size": 1, "bid": 22866, "ask": 22868}], "total": {"tradeVolume": 63553, "totalBidMatch": 47132, "totalAskMatch": 46308}, "time": 1727536322181337, "serial": 77625}'
(integer) 2
127.0.0.1:6379> publish mtx '{"symbol": "MXFJ4", "type": "FUTURE_AH", "exchange": "TAIFEX", "trades": [{"price": 22868, "size": 1, "bid": 22866, "ask": 22868}], "total": {"tradeVolume": 63553, "totalBidMatch": 47132, "totalAskMatch": 46308}, "time": 1727536422181337, "serial": 77625}'
(integer) 2
127.0.0.1:6379> publish mtx '{"symbol": "MXFJ4", "type": "FUTURE_AH", "exchange": "TAIFEX", "trades": [{"price": 22868, "size": 1, "bid": 22866, "ask": 22868}], "total": {"tradeVolume": 63553, "totalBidMatch": 47132, "totalAskMatch": 46308}, "time": 1727536522181337, "serial": 77625}'
(integer) 2
127.0.0.1:6379> publish mtx '{"symbol": "MXFJ4", "type": "FUTURE_AH", "exchange": "TAIFEX", "trades": [{"price": 22868, "size": 1, "bid": 22866, "ask": 22868}], "total": {"tradeVolume": 63553, "totalBidMatch": 47132, "totalAskMatch": 46308}, "time": 1727536622181337, "serial": 77625}'
(integer) 2
127.0.0.1:6379> publish mtx '{"symbol": "MXFJ4", "type": "FUTURE_AH", "exchange": "TAIFEX", "trades": [{"price": 22868, "size": 1, "bid": 22866, "ask": 22868}], "total": {"tradeVolume": 63553, "totalBidMatch": 47132, "totalAskMatch": 46308}, "time": 1727536622183137, "serial": 77625}'
(integer) 2
127.0.0.1:6379> publish mtx '{"symbol": "MXFJ4", "type": "FUTURE_AH", "exchange": "TAIFEX", "trades": [{"price": 22868, "size": 1, "bid": 22866, "ask": 22868}], "total": {"tradeVolume": 63553, "totalBidMatch": 47132, "totalAskMatch": 46308}, "time": 1727536722183137, "serial": 77625}'
(integer) 2
127.0.0.1:6379> publish mtx '{"symbol": "MXFJ4", "type": "FUTURE_AH", "exchange": "TAIFEX", "trades": [{"price": 22868, "size": 1, "bid": 22866, "ask": 22868}], "total": {"tradeVolume": 63553, "totalBidMatch": 47132, "totalAskMatch": 46308}, "time": 1727536822183137, "serial": 77625}'
(integer) 2
127.0.0.1:6379>