-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: export relations to json before imporing
saves time...
- Loading branch information
Showing
1 changed file
with
74 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,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)) |