-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_files_raw.py
68 lines (51 loc) · 1.79 KB
/
example_files_raw.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
#!/usr/bin/env python3
# pip install stomp.py
import stomp
import time
import traceback
from base64 import b64decode
from datetime import datetime
from os import makedirs
from os.path import join as path_join
SERVER = 'stream.abusix.net:61612'
SSL = True
CREDENTIALS = '<user>:<password>'
TOPIC = '<topic>'
# OUTPUT_DIRECTORY supports datetime formatting
# see https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior for details
OUTPUT_DIRECTORY = 'filtered/%Y-%m-%d/%H/'
class STOMPListener(stomp.ConnectionListener):
def on_error(self, frame):
print('report_error', frame.body)
def on_message(self, frame):
msg_date = datetime.now()
path = msg_date.strftime(OUTPUT_DIRECTORY)
makedirs(path, exist_ok=True)
with open(path_join(path, str(hash(frame.body)) + '.file'), 'wb') as f:
f.write(b64decode(frame.body))
def listen():
server, port = SERVER.split(':', 1)
username, password = CREDENTIALS.split(':', 1)
conn = stomp.Connection([(server, port)], heartbeats=(10000, 10000))
if SSL:
conn.set_ssl(for_hosts=[(server, port)])
conn.set_listener('', STOMPListener())
conn.connect(username, password, wait=True)
# Subscribe to topic (default: shared with other connections with the same username)
conn.subscribe(destination=TOPIC, id='1234')
# To disable load-balancing (shared subscriptions):
# conn.subscribe(destination=TOPIC, id='1234', headers={'channel': username + 'something_unique'})
try:
while True:
time.sleep(5)
except KeyboardInterrupt:
pass
conn.unsubscribe(id='1234')
conn.disconnect()
if __name__ == '__main__':
try:
listen()
except KeyboardInterrupt:
pass
except Exception:
traceback.print_exc()