Skip to content

Commit

Permalink
refactor: export relations to json before imporing
Browse files Browse the repository at this point in the history
saves time...
  • Loading branch information
b1rger committed Dec 19, 2023
1 parent 3eb308a commit 9f17250
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions apis_ontology/management/commands/relations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import json
import requests
import os
import pathlib

from django.core.management.base import BaseCommand

SRC = "https://apis.acdh.oeaw.ac.at/apis/api"
TOKEN = os.environ.get("TOKEN")
HEADERS = {"Authorization": f"Token {TOKEN}"}
RELATIONS = {}

def import_relations():
relations = {
'personevent': {
"subj": "related_person",
"obj": "related_event",
},
'personinstitution': {
"subj": "related_person",
"obj": "related_institution"
},
"personperson": {
"subj": "related_personA",
"obj": "related_personB",
},
"personplace": {
"subj": "related_person",
"obj": "related_place",
},
"personwork": {
"subj": "related_person",
"obj": "related_work",
},
"placeplace": {
"subj": "related_placeA",
"obj": "related_placeB",
},
}
relationlist = {}
for relation, relationsettings in relations.items():
nextpage = f"{SRC}/relations/{relation}/?format=json&limit=1000"
while nextpage:
print(nextpage)
page = requests.get(nextpage, headers=HEADERS)
data = page.json()
nextpage = data["next"]
for result in data["results"]:
if result["relation_type"]:
propdata = relationlist.get(result["relation_type"]["id"])
if not propdata:
proppage = requests.get(result["relation_type"]["url"])
propdata = proppage.json()
relationlist[result["relation_type"]["id"]] = propdata
if result[relationsettings["subj"]] and result[relationsettings["obj"]]:
RELATIONS[result["id"]] = {
"name": propdata["name"],
"name_reverse": propdata["name_reverse"] or propdata["name"] + " (reverse)",
"subj": result[relationsettings["subj"]]["id"],
"obj": result[relationsettings["obj"]]["id"],
}
else:
print(result)
else:
print(f"No relation type for relation {result}")


class Command(BaseCommand):
help = "Import data from legacy APIS instance"

def handle(self, *args, **options):
import_relations()
rj = pathlib.Path("relations.json")
rj.write_text(json.dumps(RELATIONS, indent=2))

0 comments on commit 9f17250

Please sign in to comment.