-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_cellar.py
More file actions
72 lines (49 loc) · 2.12 KB
/
query_cellar.py
File metadata and controls
72 lines (49 loc) · 2.12 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
This module contains functions for sending SPARQL queries to the EU Sparql endpoint
and retrieving the results in JSON format.
"""
import os
import json
import pandas as pd
from datetime import datetime
from SPARQLWrapper import SPARQLWrapper, JSON, POST
from compose_qry import get_qry
def send_request(sparql_query):
"""
Send the given sparql_query to the EU Sparql endpoint
and retrieve and return the results in JSON format.
:param sparql_query: str
:return: json dict
"""
# sparql_query = "r'" + sparql_query + "'"
# print('QUERY:', sparql_query)
endpoint = "http://publications.europa.eu/webapi/rdf/sparql" # 2024-04-12 valid
## USING SPARQLWrapper
sparql = SPARQLWrapper(endpoint)
sparql.setQuery(sparql_query)
sparql.setMethod(POST)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
# print('RESULTS:', results)
return results
# print('SPARQL_QUERY:', sparql_query)
# sql_path = 'queries/sparql_queries/all_eu_treaties.rq'
#sql_path = '/Users/yun/Dev/humanet3/human-centered-eu/queries/sparql_queries/legal-in-force-EUROVOC.rq'
#sql_path = get_qry(sql_path)
def to_json_cellar_response(query_type, eurovoc_code=None):
sparql_query, qry_name = get_qry(query_type, eurovoc_code)
if eurovoc_code:
qry_name = qry_name + eurovoc_code + '_'
print('SPARQL_QUERY:', sparql_query)
# Output SPARQL results to file
sparql_query_results_dir = "queries/sparql_query_results/"
os.makedirs(os.path.dirname(sparql_query_results_dir), exist_ok=True)
timestamp = str(datetime.now().strftime("%Y%m%d-%H%M%S"))
sparql_query_results_file = sparql_query_results_dir + qry_name + timestamp + ".json"
doctype = sparql_query_results_file.split('/')[-1].replace('.json', '').split('_')[0]
sparql_query_results = send_request(sparql_query)
#to_json_output_file(sparql_query_results_file, sparql_query_results)
with open(sparql_query_results_file, 'w') as outfile:
# print('JSON_DUMPS:', json.dumps(data))
json.dump(sparql_query_results, outfile, indent=4)
return sparql_query_results_file, doctype