From 24c7b52ac0cfb8c78427238f6252ea6cb6f88396 Mon Sep 17 00:00:00 2001 From: whirish Date: Sun, 20 Jan 2019 10:02:46 -0800 Subject: [PATCH 1/2] Fetch DPRs --- fetch_dpr.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 fetch_dpr.py diff --git a/fetch_dpr.py b/fetch_dpr.py new file mode 100644 index 0000000..73cb865 --- /dev/null +++ b/fetch_dpr.py @@ -0,0 +1,67 @@ +""" +Chron. +Copyright (C) 2018 Alisa Belyaeva, Ata Ali Kilicli, Amaury Martiny, +Daniil Mordasov, Liam O’Flynn, Mikhail Orlov. + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +""" + +from datetime import datetime +import requests +import os + +URL = "https://query.wikidata.org/sparql" +QUERY = """ +PREFIX xsd: + +SELECT ?parent ?parentLabel ?child ?childLabel ?inception ?dissolved WHERE { + VALUES (?countryclass) { + (wd:Q3024240) + (wd:Q6256) + } + ?parent wdt:P31 ?countryclass. + ?parent wdt:P150* ?child. + ?parent wdt:P571 ?inception. + OPTIONAL { ?parent wdt:P576 ?dissolved. } + FILTER(?inception < "1815-01-01T00:00:00Z"^^xsd:dateTime) + FILTER((?dissolved >= "1789-01-01T00:00:00Z"^^xsd:dateTime) || (!BOUND(?dissolved))) + FILTER(?parent != ?child) + SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } +} +""" +R_DPRS = requests.get(URL, params={"format": "json", "query": QUERY}) +DPRS = R_DPRS.json() + +for dpr in DPRS["results"]["bindings"]: + try: + data = { + "control_type": 10, # direct + "parent": int(dpr["parent"]["value"].split("Q", 1)[1]), + "child": int(dpr["child"]["value"].split("Q", 1)[1]), + "start_date": datetime.strptime( + dpr["inception"]["value"][:-1], "%Y-%m-%dT%H:%M:%S" + ).strftime("%Y-%m-%d"), + "end_date": datetime.strptime( + dpr["dissolved"]["value"][:-1], "%Y-%m-%dT%H:%M:%S" + ).strftime("%Y-%m-%d"), + } + print(data) + r_dpr = requests.post( + os.getenv("API_ROOT", "http://localhost/api/") + "/cached-data/", + data, + params={"format": "json"}, + ) + print(str(r_dpr.status_code) + ": " + r_dpr.reason) + except KeyError: + print("No coordiantes for " + dpr["dprLabel"]["value"]) From 576a7f80f82aaf85fcfcab5bb08ac96296ae7899 Mon Sep 17 00:00:00 2001 From: whirish Date: Sun, 20 Jan 2019 10:03:32 -0800 Subject: [PATCH 2/2] Lint --- .vscode/settings.json | 3 ++- fetch_dpr.py | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 5b80df3..32c4982 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { - "python.pythonPath": "venv/bin/python" + "python.pythonPath": "venv/bin/python", + "python.formatting.provider": "black" } \ No newline at end of file diff --git a/fetch_dpr.py b/fetch_dpr.py index 73cb865..9fab49a 100644 --- a/fetch_dpr.py +++ b/fetch_dpr.py @@ -46,7 +46,7 @@ for dpr in DPRS["results"]["bindings"]: try: data = { - "control_type": 10, # direct + "control_type": 10, # direct "parent": int(dpr["parent"]["value"].split("Q", 1)[1]), "child": int(dpr["child"]["value"].split("Q", 1)[1]), "start_date": datetime.strptime( @@ -56,7 +56,6 @@ dpr["dissolved"]["value"][:-1], "%Y-%m-%dT%H:%M:%S" ).strftime("%Y-%m-%d"), } - print(data) r_dpr = requests.post( os.getenv("API_ROOT", "http://localhost/api/") + "/cached-data/", data,