-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchive-ce.py
49 lines (41 loc) · 1.13 KB
/
archive-ce.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
# Get a list of all IDs in the CE and download each node as json-ld
import datetime
import json
import os
import requests
from trompace.config import config
from trompace import connection
def download_ids(ids):
outdir = f"cearchive-{datetime.date.today().isoformat()}"
host = config.host
os.makedirs(outdir, exist_ok=True)
total = len(ids)
for i, id_ in enumerate(ids, 1):
outname = os.path.join(outdir, f"{id_}.json")
if os.path.exists(outname):
continue
print(f"{i}/{total}")
print(id_)
r = requests.get(host + "/" + id_, headers={"Accept": "application/ld+json"})
try:
resp = r.json()
with open(outname, "w") as fp:
json.dump(resp, fp, indent=2)
except ValueError:
print(r.content)
def get_ids():
query = """
query {
ThingInterface {
identifier
}
}
"""
response = connection.submit_query(query)
data = response["data"]["ThingInterface"]
ids = [i["identifier"] for i in data]
return ids
if __name__ == '__main__':
config.load()
ids = get_ids()
download_ids(ids)