forked from ValdikSS/aceproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvwriter.py
77 lines (61 loc) · 2.57 KB
/
csvwriter.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
import time
from multiprocessing import Event
from Queue import Empty
import hashlib
from apscheduler.schedulers.background import BackgroundScheduler
import logging
import xxhash
import uptime
logging.getLogger('apscheduler.scheduler').setLevel('WARNING')
logging.getLogger('apscheduler.executors.default').setLevel('WARNING')
sched = BackgroundScheduler()
stop_command = Event()
class CSVWriter(object):
def __init__(self, filename, queue):
# Append current date and time to filename
self.filename = "{0}-{1}.bin".format(filename,
time.strftime("%Y%m%d-%H%M%S"))
sched.add_job(self.insert_marker, 'interval', args=[queue], seconds=0.1)
sched.start()
def writer(self, queue):
with open(self.filename, 'wb+',1024) as f:
n = int(time.time()*1000)
f.write(self.to_bytes(n,8,endianess='big'))
packet_counter = 0
while not stop_command.is_set():
try:
data = queue.get(True,1)
if len(data) < 6 and data == b'$101$':
f.write(b'\x00')
n = int(time.time()*1000)
f.write(self.to_bytes(n,8,endianess='big'))
# print(n)
else:
hashed_data = self.hash_data(data)
packet_counter += 1
# write time marker every 240 packets
if packet_counter >= 240:
f.write(b'\x00')
n = int(time.time()*1000)
f.write(self.to_bytes(n,8,endianess='big'))
packet_counter = 0
# Floating point of time since epoch in seconds
# f.write("{0}|{1}\n,".format(str(hashed_data), ts))
f.write(hashed_data if hashed_data != b'\x00' else b'\x01')
except Empty:
# This should never happen
pass
f.flush()
sched.shutdown()
def hash_data(self, packet):
hash_object = xxhash.xxh32(packet)
return hash_object.digest()[0]
def stop(self):
global stop_command
stop_command.set()
def insert_marker(self, queue):
queue.put(b'$101$')
def to_bytes(self, n, length, endianess='big'):
h = '%x' % n
s = ('0'*(len(h) % 2) + h).zfill(length*2).decode('hex')
return s if endianess == 'big' else s[::-1]