-
Notifications
You must be signed in to change notification settings - Fork 0
/
activities.py
68 lines (50 loc) · 1.78 KB
/
activities.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
import datetime
import json
import requests
EVENTS_ENDPOINT = "http://145.239.69.80:16655/events"
ACTIVITIES_ENDPOINT = "http://145.239.69.80:16655/activities"
resp = requests.get(EVENTS_ENDPOINT)
events = json.loads(resp.content)
resp = requests.get(ACTIVITIES_ENDPOINT)
activities = json.loads(resp.content)
def process_event(event):
event["date"] = datetime.datetime.fromisoformat(f"{event['date'][:-1]}+00:00")
return event
def process_activity(activity):
activity["start"] = datetime.datetime.fromisoformat(f"{activity['start'][:-1]}+00:00")
activity["end"] = datetime.datetime.fromisoformat(f"{activity['end'][:-1]}+00:00")
activity["events"] = list()
return activity
events = sorted(map(process_event, events["events"]), key=lambda e: e["date"])
activities = sorted(map(process_activity, activities["activities"]), key=lambda a: a["start"])
orphaned = list()
comparisons = 0
a_start_idx = 0
for e in events:
cnt = 0
for ai in range(a_start_idx, len(activities)):
a = activities[ai]
comparisons += 2
if a["start"] >= e["date"]:
a_start_idx = ai
comparisons -= 1
elif e["date"] <= a["end"]:
a["events"].append(e)
cnt += 1
if not cnt:
orphaned.append(e)
print(comparisons)
def process_activity_back(activity):
activity["start"] = activity["start"].isoformat()
activity["end"] = activity["end"].isoformat()
for e in activity["events"]:
if isinstance(e["date"], datetime.datetime):
e["date"] = e["date"].isoformat()
return activity
for e in orphaned:
e["date"] = e["date"].isoformat()
out = {
"activities": [process_activity_back(a) for a in activities if a["events"]],
"orphaned": orphaned,
}
print(json.dumps(out, indent=2))