#!/usr/bin/env python3 ''' This script will perform a simple query to the DarkOwl Vision Search API. By default, the query has just one API paramater: 'q', with the value 'test' Query paramaters can be changed in the 'payload' variable in the main function ''' import requests import datetime import hmac import hashlib import base64 publicKey = 'PUBLIC_KEY_HERE' privateKey = 'PRIVATE_KEY_HERE' def main(): #API Parameters and values in for form of a list of tuples #('API_Param', 'Param_value') payload = [ ('q', 'test') ] response = perform_query(payload) print_data(response) def print_data(response): response = response.json() print(f'Total Results: {response["total"]}\n') #example, this just prints the document ID of each result from the query #modify as needed for result in response['results']: print(result['id']) def perform_query(payload): host = 'api.darkowl.com' endpoint = '/api/v1/search' #Generate search string search = payloadToString(payload) url = f'https://{host}{endpoint}{search}' absPath = endpoint + search date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT') auth = generate_auth_header(absPath, 'GET', privateKey, publicKey, date) headers = {'Authorization': auth, 'X-VISION-DATE': date, 'Accept': 'application/json'} r = requests.get(url, headers=headers) if r.status_code == 200: return r else: print(r.content) exit(1) def generate_auth_header(abs_path, http_method, private_key, public_key, time_stamp): string2hash = http_method + abs_path + time_stamp bkey = bytes(source=private_key, encoding='UTF-8') bpayload = bytes(source=string2hash, encoding='UTF-8') hmacsha1 = hmac.new(bkey, bpayload, hashlib.sha1).digest() base64encoded = base64.b64encode(hmacsha1).decode('UTF-8') auth_header = f'OWL {public_key}:{base64encoded}' return auth_header ### Takes a payload (list of tuples) and generates a URL query string def payloadToString(payload): search = '' count = 0 for key, value in payload: if count == 0: search += f'?{key}={value}' count = 1 else: search += f'&{key}={value}' return search if __name__ == '__main__': main()