Skip to content

Commit

Permalink
script for easy adding of new subnets
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaMilosa committed Dec 11, 2024
1 parent 1e94192 commit a57b911
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions scripts/update-subnet-topic-map.py
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)

0 comments on commit a57b911

Please sign in to comment.