-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_json.py
More file actions
179 lines (136 loc) · 4.42 KB
/
generate_json.py
File metadata and controls
179 lines (136 loc) · 4.42 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
#!/usr/bin/env python3
"""
Generate JSON files for UI from seed and output data.
This script:
1. Loads all entries from seed/ directory
2. Loads corresponding developer score files from output/ directory (devs only)
3. Creates JSON files for each ecosystem with format:
{
"category": "devrank",
"ecosystem": "<ecosystem_name>",
"seed": [{"i": "peer_id", "v": value}, ...],
"scores": [{"i": "peer_id", "v": value}, ...]
}
4. Saves JSON files to ui/ directory
Usage:
python3 generate_json.py
Requirements:
- pandas (install with: pip install pandas)
- CSV files in seed/ directory
- CSV files in output/ directory (matching seed filenames with _devs suffix)
Output:
- Creates ui/ directory if it doesn't exist
- For each seed file (e.g., bitcoin.csv), creates:
- ui/bitcoin.json with seed and score data
"""
import os
import json
import pandas as pd
from pathlib import Path
def capitalize_ecosystem_name(filename):
"""
Convert filename to capitalized ecosystem name.
Examples:
bitcoin -> Bitcoin
protocol-guild -> Protocol-Guild
cryptoxai -> Cryptoxai
Args:
filename (str): Base filename without extension
Returns:
str: Capitalized ecosystem name
"""
# Split on hyphens and capitalize each part
parts = filename.split("-")
capitalized = "-".join(word.capitalize() for word in parts)
return capitalized
def load_seed_data(seed_file):
"""
Load all entries from seed file.
Args:
seed_file (Path): Path to seed CSV file
Returns:
list: List of dictionaries with 'i' and 'v' keys
"""
df = pd.read_csv(seed_file)
# Convert to list of dictionaries
seed_data = df.to_dict("records")
return seed_data
def load_scores(scores_file):
"""
Load scores from output file.
Args:
scores_file (Path): Path to scores CSV file
Returns:
list: List of dictionaries with 'i' and 'v' keys
"""
if not scores_file.exists():
print(f"Warning: {scores_file} not found, using empty scores")
return []
df = pd.read_csv(scores_file)
# Convert to list of dictionaries
scores_data = df.to_dict("records")
return scores_data
def generate_json_file(ecosystem_name, seed_data, scores_data, output_file):
"""
Generate JSON file with seed and scores data.
Args:
ecosystem_name (str): Name of the ecosystem
seed_data (list): List of seed entries
scores_data (list): List of score entries
output_file (Path): Path to output JSON file
"""
json_data = {
"category": "devrank",
"ecosystem": ecosystem_name,
"seed": seed_data,
"scores": scores_data,
}
# Write JSON file with pretty formatting
with open(output_file, "w", encoding="utf-8") as f:
json.dump(json_data, f, indent=2, ensure_ascii=False)
print(f"✓ Created {output_file}")
print(f" Seed entries: {len(seed_data)}")
print(f" Score entries: {len(scores_data)}")
def main():
"""
Main execution function.
"""
print("=" * 60)
print("Generating JSON files for UI")
print("=" * 60)
print()
# Define directories
seed_dir = Path("seed")
output_dir = Path("output")
ui_dir = Path("ui")
# Create ui directory if it doesn't exist
ui_dir.mkdir(exist_ok=True)
print(f"✓ Output directory: {ui_dir}/")
print()
# Find all seed CSV files
seed_files = sorted(list(seed_dir.glob("*.csv")))
if not seed_files:
print(f"❌ No CSV files found in {seed_dir}")
return
print(f"Found {len(seed_files)} seed file(s) to process...")
print()
# Process each seed file
for seed_file in seed_files:
base_name = seed_file.stem
ecosystem_name = capitalize_ecosystem_name(base_name)
print(f"Processing: {base_name}")
# Load seed data (all entries)
seed_data = load_seed_data(seed_file)
# Find corresponding scores file
scores_file = output_dir / f"{base_name}_devs.csv"
scores_data = load_scores(scores_file)
# Generate JSON file
output_file = ui_dir / f"{base_name}.json"
generate_json_file(ecosystem_name, seed_data, scores_data, output_file)
print()
print("=" * 60)
print("✓ JSON generation complete!")
print("=" * 60)
print(f"\nJSON files saved to {ui_dir}/")
if __name__ == "__main__":
main()