forked from MISP/misp-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki.py
executable file
·50 lines (39 loc) · 1.51 KB
/
wiki.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
import json
import requests
from SPARQLWrapper import SPARQLWrapper, JSON
misperrors = {'error': 'Error'}
mispattributes = {'input': ['text'], 'output': ['text']}
moduleinfo = {'version': '0.2', 'author': 'Roman Graf', 'description': 'An expansion hover module to extract information from Wikidata to have additional information about particular term for analysis.', 'module-type': ['hover']}
moduleconfig = []
# sample query text 'Microsoft' should provide Wikidata link https://www.wikidata.org/wiki/Q2283 in response
wiki_api_url = 'https://query.wikidata.org/bigdata/namespace/wdq/sparql'
def handler(q=False):
if q is False:
return False
request = json.loads(q)
if not request.get('text'):
misperrors['error'] = 'Query text missing'
return misperrors
sparql = SPARQLWrapper(wiki_api_url)
query_string = \
"SELECT ?item \n" \
"WHERE { \n" \
"?item rdfs:label\"" + request.get('text') + "\" @en \n" \
"}\n";
sparql.setQuery(query_string)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
summary = ''
try:
result = results["results"]["bindings"][0]
summary = result["item"]["value"]
except Exception as e:
misperrors['error'] = 'wikidata API not accessible' + e
return misperrors['error']
r = {'results': [{'types': mispattributes['output'], 'values': summary}]}
return r
def introspection():
return mispattributes
def version():
moduleinfo['config'] = moduleconfig
return moduleinfo