Skip to content

Commit

Permalink
Merge pull request #5 from chronhq/wh-treaties
Browse files Browse the repository at this point in the history
Fetch treaties
  • Loading branch information
amaury1093 authored Jan 20, 2019
2 parents 2496ad7 + 126094e commit b66455b
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions fetch_treaties.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
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 <https://www.gnu.org/licenses/>.
"""

from datetime import datetime
import requests
import os

URL = "https://query.wikidata.org/sparql"
QUERY = """
SELECT ?treaty ?treatyLabel ?time ?location ?coors
WHERE
{
?treaty wdt:P31 wd:Q625298.
?treaty wdt:P585 ?time.
?treaty wdt:P276 ?location.
?location wdt:P625 ?coors
FILTER (?time > "1789-01-01T00:00:00Z"^^xsd:dateTime)
FILTER (?time < "1816-01-01T00:00:00Z"^^xsd:dateTime)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
"""
R_TREATIES = requests.get(URL, params={"format": "json", "query": QUERY})
TREATIES = R_TREATIES.json()

for treaty in TREATIES["results"]["bindings"]:
try:
data = {
"event_type": 131569,
"wikidata_id": int(treaty["treaty"]["value"].split("Q", 1)[1]),
"location": treaty["coors"]["value"],
"date": datetime.strptime(
treaty["time"]["value"][:-1], "%Y-%m-%dT%H:%M:%S"
).strftime("%Y-%m-%d"),
}
r_treaty = requests.post(
os.getenv("API_ROOT", "http://localhost/api/") + "/cached-data/",
data,
params={"format": "json"},
)
print(str(r_treaty.status_code) + ": " + r_treaty.reason)
except KeyError:
print("No coordinates for " + treaty["treatyLabel"]["value"])

0 comments on commit b66455b

Please sign in to comment.