-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
70 lines (55 loc) · 2.16 KB
/
main.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
import requests, json, os
source = os.environ.get('SOURCE').strip()
base_url = f"https://system.spektrix.com/{source}/api/v3"
def get_events():
print("Getting events...")
response = requests.get(f"{base_url}/events")
response.raise_for_status()
return response.json()
def get_venues():
print("Getting venues...")
response = requests.get(f"{base_url}/venues")
response.raise_for_status()
return response.json()
def get_instances():
print("Getting instances...")
response = requests.get(f"{base_url}/instances")
response.raise_for_status()
return response.json()
def get_plans():
print("Getting plans...")
response = requests.get(f"{base_url}/plans")
response.raise_for_status()
return response.json()
def enrich_event_with_location(event, venues, instances, plans):
print(f"Enriching event {event.get('id')} with location...")
event_id = event.get("id")
if not event_id:
return event
event_instances = [instance for instance in instances if instance.get("event", {}).get("id") == event_id]
event['locations'] = []
event['source'] = source
if event_instances:
for instance in event_instances:
plan_id = instance.get("planId")
plan = next((plan for plan in plans if plan.get("id") == plan_id), None)
venue_id = plan.get("venue", {}).get("id")
if any(location.get("id") == venue_id for location in event['locations']):
continue
venue_info = next((venue for venue in venues if venue.get("id") == venue_id), None)
event['locations'].append(venue_info)
return event
def main():
events = get_events()
venues = get_venues()
instances = get_instances()
plans = get_plans()
enriched_events = [enrich_event_with_location(event, venues, instances, plans) for event in events]
json_data = json.dumps(enriched_events, indent=4)
if not os.path.exists("json_data"):
os.makedirs("json_data")
with open(f"json_data/{source}.json", "w") as file:
file.write(json_data)
print(f"Events have been saved to ${source}.json")
if __name__ == "__main__":
main()