-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautomation.py
52 lines (40 loc) · 1.53 KB
/
automation.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
import json
import os
import requests
from ImportAll import import_all_data, header
LAST_LAUNCH_URL = "https://ll.thespacedevs.com/2.3.0/launches/previous/?limit=1&status__ids=3,4,7&mode=list"
def get_last_launch():
print("Getting last launch...")
last_launch = requests.get(LAST_LAUNCH_URL, headers=header, timeout=1440).json()
return (
last_launch["results"][0],
last_launch["results"][0]["id"],
last_launch["results"][0]["status"]["id"],
)
def get_cached_last_launch():
print("Getting cached last launch...")
with open("cache/last_launch.json", "r") as file:
last_launch = json.load(file)
return last_launch["id"], last_launch["status"]["id"]
def update_cache(launch):
print("Updating cache...")
if not os.path.exists("cache"):
os.mkdir("cache")
with open("cache/last_launch.json", "w+") as file:
json.dump(launch, file, indent=4)
if __name__ == "__main__":
launch, new_id, new_status = get_last_launch()
if os.path.exists("cache/last_launch.json"):
old_id, old_status = get_cached_last_launch()
if new_id == old_id and new_status == old_status:
print("Latest launch has not changed. No need to update.")
exit(code=0)
else:
print("Latest launch has changed. Updating all plots.")
else:
print("Cache does not exist. Creating and updating all plots.")
update_cache(launch)
import_all_data()
from plots import generate_plots
generate_plots(show=False)
exit(code=0)