-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathsparql.py
executable file
·54 lines (47 loc) · 1.53 KB
/
sparql.py
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
#!/usr/bin/env python3
# coding: utf-8
from SPARQLWrapper import SPARQLWrapper, JSON, SPARQLExceptions
import configparser
import config_path
config = configparser.RawConfigParser()
config.read(config_path.CONFIG)
root = config.get("Files and directories", "root")
cachefile = config.get("Files and directories", "image_cache")
def getdbpediaimage(query, cache):
query = query
if "::" in query:
query = " ".join([w.capitalize() for w in query.split("::")])
else:
query = query.capitalize()
if query in cache:
return cache[query]
else:
# return None
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery(
"""
SELECT DISTINCT ?e ?pic
WHERE {
?e rdfs:label "%s"@en .
?e <http://dbpedia.org/ontology/thumbnail> ?pic .
}
"""
% query
)
sparql.setReturnFormat(JSON)
try:
results = sparql.query().convert()
except SPARQLExceptions.QueryBadFormed:
return None
if len(results["results"]["bindings"]) > 0:
image = results["results"]["bindings"][0]["pic"]["value"]
image = image.replace("http://", "https://")
data = open(root + cachefile, "a")
data.write(query + "\t" + image + "\n")
data.close()
return image
else:
data = open(root + cachefile, "a")
data.write(query + "\tNone\n")
data.close()
return None