mirror of https://github.com/MISP/misp-modules
Added expansion for Wikidata. Analyst can query Wikidata by label to get additional information for particular term.
parent
3b2ccd8d03
commit
d4370fc0e3
1 changed files with 50 additions and 0 deletions
@ -0,0 +1,50 @@ |
||||
import json |
||||
import requests |
||||
from SPARQLWrapper import SPARQLWrapper, JSON |
||||
|
||||
misperrors = {'error': 'Error'} |
||||
mispattributes = {'input': ['label'], 'output': ['text']} |
||||
moduleinfo = {'version': '0.1', '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 label '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('label'): |
||||
misperrors['error'] = 'Query label missing' |
||||
return misperrors |
||||
|
||||
sparql = SPARQLWrapper(wiki_api_url) |
||||
query_string = \ |
||||
"SELECT ?item \n" \ |
||||
"WHERE { \n" \ |
||||
"?item rdfs:label\"" + request.get('label') + "\" @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 |
||||
|
Loading…
Reference in new issue