-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
refresh.py
142 lines (116 loc) · 5.06 KB
/
refresh.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import csv
import requests
import json
from datetime import datetime, timedelta
from typing import Optional
from core.util.constants import DEEP_DIVE_METADATA_URL, DEEP_DIVE_INFORMATION_URL, V1_DEEPDIVES_PATH
from core.util.json_encoder import JSONEncoder
from v1.model.deep_dives import (
Anomaly,
Biome,
Type,
DeepDives,
Stage,
Variant,
Warning
)
def get_deep_dive_metadata():
response = requests.get(DEEP_DIVE_METADATA_URL)
if response.status_code == 200:
return response.json()
else:
raise Exception("Failed to fetch deep dive metadata")
def get_deep_dive_information() -> str:
response = requests.get(DEEP_DIVE_INFORMATION_URL)
if response.status_code == 200:
return response.text
else:
raise Exception("Failed to fetch deep dive information")
def construct_deep_dives(metadata: dict, information: str) -> DeepDives:
# Unpack metadata
expiration_time: datetime = datetime.fromisoformat(metadata["ExpirationTime"])
end_time: str = expiration_time.strftime("%Y-%m-%dT%H:%M:%SZ")
start_time: str = (expiration_time - timedelta(days=7)).strftime("%Y-%m-%dT%H:%M:%SZ")
data = list(csv.reader(information.splitlines(), delimiter=','))
# Construct Deep Dive Variant
dd_seed: int = metadata["Seed"]
dd_name: str = data[5][2]
dd_biome: Biome = map_biome(data[6][2])
dd_type: Type = Type.DEEP_DIVE
dd_stage_1: Stage = Stage(id=1,
primary=format_objective(data[8][2]),
secondary=format_objective(data[9][2]),
anomaly=map_anomaly(data[10][3]),
warning=map_warning(data[10][2]))
dd_stage_2: Stage = Stage(id=2,
primary=format_objective(data[12][2]),
secondary=format_objective(data[13][2]),
anomaly=map_anomaly(data[14][3]),
warning=map_warning(data[14][2]))
dd_stage_3: Stage = Stage(id=3,
primary=format_objective(data[16][2]),
secondary=format_objective(data[17][2]),
anomaly=map_anomaly(data[18][3]),
warning=map_warning(data[18][2]))
dd_variant: Variant = Variant(type=dd_type,
name=dd_name,
biome=dd_biome,
seed=dd_seed,
stages=[dd_stage_1, dd_stage_2, dd_stage_3])
# Construct Elite Deep Dive Variant
edd_seed: int = metadata["SeedV2"]
edd_name: str = data[5][7]
edd_biome: Biome = map_biome(data[6][7])
edd_type: Type = Type.ELITE_DEEP_DIVE
edd_stage_1: Stage = Stage(id=1,
primary=format_objective(data[8][7]),
secondary=format_objective(data[9][7]),
anomaly=map_anomaly(data[10][8]),
warning=map_warning(data[10][7]))
edd_stage_2: Stage = Stage(id=2,
primary=format_objective(data[12][7]),
secondary=format_objective(data[13][7]),
anomaly=map_anomaly(data[14][8]),
warning=map_warning(data[14][7]))
edd_stage_3: Stage = Stage(id=3,
primary=format_objective(data[16][7]),
secondary=format_objective(data[17][7]),
anomaly=map_anomaly(data[18][8]),
warning=map_warning(data[18][7]))
edd_variant: Variant = Variant(type=edd_type,
name=edd_name,
biome=edd_biome,
seed=edd_seed,
stages=[edd_stage_1, edd_stage_2, edd_stage_3])
return DeepDives(startTime=start_time, endTime=end_time, variants=[dd_variant, edd_variant])
def format_objective(objective: str) -> str:
return objective.replace(" + Uplink", "") \
.replace(" + Drill", "")
def map_biome(biome: str) -> Optional[Biome]:
try:
return Biome(biome)
except ValueError:
return None
def map_anomaly(anomaly: str) -> Optional[Anomaly]:
try:
return Anomaly(anomaly)
except ValueError:
return None
def map_warning(warning: str) -> Optional[Warning]:
try:
return Warning(warning)
except ValueError:
return None
def save_json(schema: DeepDives):
with open(V1_DEEPDIVES_PATH, "w") as file:
file.write(json.dumps(schema.dict(), cls=JSONEncoder, indent=2))
def refresh():
# Fetch the deep dive metadata and information
metadata = get_deep_dive_metadata()
information = get_deep_dive_information()
# Aggregate the deep dive data
deep_dives = construct_deep_dives(metadata, information)
# Save the deep dive data to a JSON file
save_json(deep_dives)
if __name__ == "__main__":
refresh()