-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch_C03_projects.py
executable file
·65 lines (56 loc) · 2.08 KB
/
fetch_C03_projects.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
#!/usr/bin/env python
import glob
import json
import requests
# API docs for mainnet:
# http://mainnet.regen.network:1317/swagger/#/Query/ProjectsByClass
#
# API docs for registry-server:
# https://api.registry.regen.network/api-docs/
def projects_currently_in_ops():
return sorted([x for x in glob.glob("ops/C03/project-metadata/*.jsonld")])
def on_chain_projects():
class_id = "C03"
resp = requests.get(
f"http://mainnet.regen.network:1317/regen/ecocredit/v1/projects-by-class/{class_id}"
)
resp.raise_for_status()
projects = resp.json()["projects"]
return projects
def off_chain_metadata(iri):
resp = requests.get(f"https://api.registry.regen.network/metadata-graph/{iri}")
resp.raise_for_status()
metadata = resp.json()
return metadata
if __name__ == "__main__":
current_projects = projects_currently_in_ops()
tasks = []
for p in on_chain_projects():
metadata = off_chain_metadata(p["metadata"])
project_metadata = f'ops/C03/project-metadata/{p["id"]}-project.jsonld'
if project_metadata not in current_projects:
print(
f"this script will add project metadata into ops: filename={project_metadata} iri={p['metadata']}"
)
tasks.append({"filename": project_metadata, "metadata": metadata})
project_page_metadata = (
f'ops/C03/project-metadata/{p["id"]}-project-page.jsonld'
)
if project_page_metadata not in current_projects:
print(
f"this script will create an empty project page: filename={project_page_metadata}"
)
tasks.append(
{
"filename": project_page_metadata,
"metadata": {},
}
)
if tasks:
proceed = input("proceed? [y/n] ").lower().strip()
if proceed == "y":
for task in tasks:
with open(task["filename"], "w") as fp:
json.dump(task["metadata"], fp)
else:
print("there are no C03 projects to sync at this time")