-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
script for easy adding of new subnets
- Loading branch information
1 parent
1e94192
commit a57b911
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import subprocess | ||
import urllib.request | ||
import json | ||
import pathlib | ||
|
||
def get_root() -> str: | ||
output = subprocess.run(["git", "rev-parse", "--show-toplevel"], check=True, text=True, stdout=subprocess.PIPE) | ||
return output.stdout.strip() | ||
|
||
def get_subnets_data() -> dict: | ||
req = urllib.request.Request(url='https://ic-api.internetcomputer.org/api/v3/subnets?format=json', headers={ | ||
"user-agent": "python" | ||
}) | ||
|
||
subnets = {} | ||
with urllib.request.urlopen(req, timeout=30) as response: | ||
subnets_data = json.loads(response.read().decode())["subnets"] | ||
for subnet in subnets_data: | ||
subnets[subnet["subnet_id"]] = { | ||
"topic_id": 0, | ||
"slug": "" | ||
} | ||
|
||
return subnets | ||
|
||
def fetch_existing_data(path) -> dict: | ||
with open(path, "r") as f: | ||
return json.load(f) | ||
|
||
if __name__ == "__main__": | ||
root = pathlib.Path(get_root()) | ||
all_subnets = get_subnets_data() | ||
subnet_topic_file = root / "rs" / "cli" / "src" / "assets" / "subnet_topic_map.json" | ||
existing_subnets = fetch_existing_data(subnet_topic_file) | ||
for subnet in all_subnets: | ||
if subnet in existing_subnets: | ||
print(f"Subnet '{subnet}' already present") | ||
continue | ||
|
||
existing_subnets[subnet] = all_subnets[subnet] | ||
|
||
with open(subnet_topic_file, "w+") as f: | ||
json.dump(existing_subnets, f, indent=4) |