-
Notifications
You must be signed in to change notification settings - Fork 1
/
pull_reference.py
67 lines (56 loc) · 2.14 KB
/
pull_reference.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
import requests
from time import sleep
import json
from pprint import pprint
sleep_duration_limit = 10.0
sleep_duration_adjust_factor = 2.0
sleep_duration = 1.0
def reference_filter(messages, rules):
global sleep_duration
arguments = {
'messages': messages,
'rules': rules,
}
while True:
try:
action_sequences = requests.post('http://hola.org/challenge_mail_filter/reference', json=arguments).json()
sleep_duration /= sleep_duration_adjust_factor
return action_sequences
except:
sleep_duration = min(sleep_duration_limit, sleep_duration * sleep_duration_adjust_factor)
sleep(sleep_duration)
def load_json_file(path):
with open(path) as file:
return json.load(file)
def dump_json_file(path, value):
with open(path, 'w') as file:
json.dump(value, file, indent=4, sort_keys=True)
messages = load_json_file('messages.json')
messages = [(k, messages[k]) for k in sorted(messages)]
rules = load_json_file('rules.json')
def chunk(items, k):
items_chunk = []
for item in items:
items_chunk.append(item)
if len(items_chunk) == k:
yield items_chunk
items_chunk = []
if len(items_chunk) != 0:
yield items_chunk
try:
action_sequences = load_json_file('actionSequences.json')
last_written = max(action_sequences)
messages = [(k, v) for k, v in messages if k > last_written]
except:
action_sequences = {}
for messages_chunk in chunk(messages, 10):
messages_chunk = dict(messages_chunk)
action_sequence_chunks = {}
for rules_chunk in chunk(rules, 10):
print('\r{}..{} {}..{}'.format(min(messages_chunk), max(messages_chunk), min(r['action'] for r in rules_chunk), max(r['action'] for r in rules_chunk)), end='')
for message_id, action_sequence_chunk in reference_filter(messages_chunk, rules_chunk).items():
action_sequence_chunks.setdefault(message_id, []).extend(action_sequence_chunk)
print()
pprint(action_sequence_chunks)
action_sequences.update(action_sequence_chunks)
dump_json_file('actionSequences.json', action_sequences)