Repository navigation
Expand file tree
/
Copy pathexport.py
More file actions
313 lines (262 loc) · 9.33 KB
/
Copy pathexport.py
File metadata and controls
313 lines (262 loc) · 9.33 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python3
"""
OpenBeta Parquet Exporter
Exports climbing route data from OpenBeta GraphQL API to Parquet format.
"""
import json
import requests
import duckdb
import yaml
import tempfile
import time
from pathlib import Path
from typing import Dict, List, Any
import sys
# GraphQL query to fetch all countries
COUNTRIES_QUERY = """
query GetCountries {
countries {
areaName
}
}
"""
# GraphQL query to fetch areas with climbs for a specific country
# Uses pagination (limit/offset) since the API defaults to 50 results
AREAS_QUERY = """
query GetAreas($tokens: [String!]!, $limit: Int!, $offset: Int!) {
areas(filter: {leaf_status: {isLeaf: true}, path_tokens: {tokens: $tokens}}, limit: $limit, offset: $offset) {
uuid
area_name
pathTokens
metadata {
lat
lng
}
climbs {
uuid
name
fa
length
boltsCount
grades {
yds
vscale
french
}
type {
sport
trad
bouldering
alpine
tr
}
safety
metadata {
lat
lng
}
content {
description
}
pathTokens
}
}
}
"""
# Pagination settings
AREAS_PAGE_SIZE = 500 # Max allowed by API
def load_config() -> Dict[str, Any]:
"""Load configuration from config.yaml"""
config_path = Path(__file__).parent / "config.yaml"
with open(config_path) as f:
return yaml.safe_load(f)
def load_schema() -> str:
"""Load SQL schema from schema.sql"""
schema_path = Path(__file__).parent / "schema.sql"
return schema_path.read_text()
def fetch_country_climbs(api_url: str, country: str) -> List[Dict]:
"""Fetch all climbs for a country using pagination"""
all_climbs = []
offset = 0
total_areas = 0
max_retries = 3
while True:
for attempt in range(max_retries):
try:
response = requests.post(
api_url,
json={
"query": AREAS_QUERY,
"variables": {
"tokens": [country],
"limit": AREAS_PAGE_SIZE,
"offset": offset
}
},
headers={"Content-Type": "application/json"},
timeout=120
)
if response.status_code == 200:
break
if response.status_code in (502, 503, 504) and attempt < max_retries - 1:
print(f" retry {attempt + 1}/{max_retries} after {response.status_code}...")
time.sleep(2)
continue
except requests.Timeout:
if attempt < max_retries - 1:
print(f" retry {attempt + 1}/{max_retries} after timeout...")
time.sleep(2)
continue
print(f" {country}: timeout at offset {offset}")
return all_climbs
if response.status_code != 200:
print(f" {country}: failed ({response.status_code}) at offset {offset}")
return all_climbs
data = response.json()
if "errors" in data:
print(f" {country}: GraphQL error")
return all_climbs
areas = data.get("data", {}).get("areas", [])
total_areas += len(areas)
# Extract climbs from areas
for area in areas:
for climb in area.get("climbs", []):
# Use area pathTokens if climb doesn't have them
if not climb.get("pathTokens"):
climb["pathTokens"] = area.get("pathTokens", [])
# Add area coordinates if climb doesn't have them
if not climb.get("metadata", {}).get("lat"):
if area.get("metadata", {}).get("lat"):
climb.setdefault("metadata", {})["lat"] = area["metadata"]["lat"]
climb["metadata"]["lng"] = area["metadata"]["lng"]
all_climbs.append(climb)
# Check if we've fetched all pages
if len(areas) < AREAS_PAGE_SIZE:
break
offset += AREAS_PAGE_SIZE
# Progress indicator for large countries
if offset % 1000 == 0:
print(f" ... fetched {total_areas} areas, {len(all_climbs)} climbs so far")
return all_climbs
def fetch_all_climbs(api_url: str) -> List[Dict]:
"""Fetch all climbs from GraphQL API"""
print(f"Fetching countries from {api_url}...")
response = requests.post(
api_url,
json={"query": COUNTRIES_QUERY},
headers={"Content-Type": "application/json"}
)
if response.status_code != 200:
raise Exception(f"Countries query failed: {response.status_code} {response.text[:500]}")
data = response.json()
if "errors" in data:
raise Exception(f"GraphQL errors: {data['errors']}")
countries = data.get("data", {}).get("countries", [])
print(f"Found {len(countries)} countries")
all_climbs = []
for i, country in enumerate(countries, 1):
name = country["areaName"]
print(f"[{i}/{len(countries)}] {name}")
climbs = fetch_country_climbs(api_url, name)
if climbs:
print(f" {name}: {len(climbs):,} climbs")
else:
print(f" {name}: 0 climbs")
all_climbs.extend(climbs)
print(f"\nTotal climbs fetched: {len(all_climbs)}")
return all_climbs
def filter_climbs(climbs: List[Dict], config: Dict) -> List[Dict]:
"""Filter climbs by configured regions"""
regions = config.get("export", {}).get("regions", [])
if not regions:
return climbs
filtered = [c for c in climbs if c.get("pathTokens") and c["pathTokens"][0] in regions]
print(f"Filtered to regions {regions}: {len(filtered)} climbs")
return filtered
def export_to_parquet(climbs: List[Dict], config: Dict):
"""Convert climbs to Parquet using DuckDB"""
output_config = config.get("export", {}).get("output", {})
filename = output_config.get("filename", "openbeta-climbs.parquet")
compression = output_config.get("compression", "snappy")
print(f"\nTransforming data with DuckDB...")
# Initialize DuckDB
con = duckdb.connect(database=":memory:")
# Load climbs as JSON via temp file
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as tmp:
json.dump(climbs, tmp)
tmp_path = tmp.name
try:
# Measure JSON size for comparison
json_size_mb = Path(tmp_path).stat().st_size / (1024 * 1024)
print(f" JSON intermediate size: {json_size_mb:.2f} MB")
con.execute(f"CREATE TABLE climbs AS SELECT * FROM read_json_auto('{tmp_path}')")
print(f" Loaded {len(climbs)} climbs into DuckDB")
finally:
Path(tmp_path).unlink() # Clean up temp file
# Load and execute schema transformation
schema_sql = load_schema()
print(f" Applying schema transformation...")
# Export to Parquet
output_path = Path(filename)
print(f"\nExporting to {output_path}...")
con.execute(f"""
COPY ({schema_sql})
TO '{output_path}'
(FORMAT PARQUET, COMPRESSION '{compression}')
""")
# Get file size and show comparison
parquet_size_mb = output_path.stat().st_size / (1024 * 1024)
compression_ratio = json_size_mb / parquet_size_mb if parquet_size_mb > 0 else 0
space_saved_pct = (1 - parquet_size_mb / json_size_mb) * 100 if json_size_mb > 0 else 0
print(f"Export complete: {output_path} ({parquet_size_mb:.2f} MB)")
print(f" Size comparison: JSON {json_size_mb:.2f} MB → Parquet {parquet_size_mb:.2f} MB")
print(f" Compression: {compression_ratio:.1f}x smaller ({space_saved_pct:.1f}% space saved)")
# Write stats for GitHub Actions
stats = {
"total_climbs": len(climbs),
"json_size_mb": round(json_size_mb, 2),
"parquet_size_mb": round(parquet_size_mb, 2),
"compression_ratio": round(compression_ratio, 1),
"space_saved_pct": round(space_saved_pct, 1)
}
stats_path = Path("export-stats.json")
stats_path.write_text(json.dumps(stats, indent=2))
# Show sample
print(f"\nSample data (first 5 rows):")
result = con.execute(f"SELECT * FROM ({schema_sql}) LIMIT 5")
cols = [d[0] for d in result.description]
rows = result.fetchall()
print(" | ".join(cols))
print("-" * min(120, len(" | ".join(cols))))
for row in rows:
print(" | ".join(str(v)[:30] for v in row))
con.close()
def main():
"""Main export process"""
print("=" * 60)
print("OpenBeta Parquet Exporter")
print("=" * 60)
try:
# Load configuration
config = load_config()
api_url = config["export"]["api_url"]
# Fetch data
climbs = fetch_all_climbs(api_url)
if not climbs:
print("WARNING: No climbs found!")
sys.exit(1)
# Apply filters
climbs = filter_climbs(climbs, config)
if not climbs:
print("WARNING: No climbs remained after filtering!")
sys.exit(1)
# Export to Parquet
export_to_parquet(climbs, config)
print("\nExport successful!")
except Exception as e:
print(f"\nERROR: Export failed: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()