-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbooli_request_example.py
More file actions
executable file
·131 lines (99 loc) · 4.23 KB
/
booli_request_example.py
File metadata and controls
executable file
·131 lines (99 loc) · 4.23 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python
import argparse
import getopt
import requests
import json
import random
import string
import sys
import time
from hashlib import sha1
"""
Make a sample call to the Booli API asking for all listings in 'Nacka' in JSON format,
using 'YOUR_CALLER_ID' and 'YOUR_PRIVATE_KEY' for authentication
"""
def main(arg):
def get_booli_data(callerId, timestamp, unique, hashstr, limit, offset, query, type):
headers = {'Accept': 'application/vnd.booli-v2+json'}
url = "http://api.booli.se/" + type + "?q=" + query + "&limit=" + str(limit) + "&offset=" + str(
offset) + "&callerId=" + callerId + "&time=" + timestamp + "&unique=" + unique + "&hash=" + hashstr
r = requests.get(url, headers=headers)
r.encoding = 'utf-8'
if (r.status_code != 200):
print("fail")
return json.loads("")
global result
result = json.loads(r.text)
return result
def send_event_to_splunk(splunk_host, splunk_auth_token, listing, source):
try:
# Integer value representing epoch time format
listTime = listing.get("published")
pattern = '%Y-%m-%d %H:%M:%S'
event_time = int(time.mktime(time.strptime(listTime, pattern)))
# String representing the host name or IP
host_id = "localhost"
# Create request URL
request_url = "https://%s:8088/services/collector" % splunk_host
post_data = {
"time": event_time,
"host": host_id,
"sourcetype": source,
"event": listing,
"source": "booli_data"
}
# Encode data in JSON utf-8 format
data = json.dumps(post_data).encode('utf8')
# Create auth header
auth_header = "Splunk %s" % splunk_auth_token
headers = {'Authorization': auth_header}
# Create request
response = requests.post(request_url, headers=headers, data=data, verify=False)
response.raise_for_status()
# read response, should be in JSON format
read_response = response.text
try:
response_json = json.loads(read_response)
if "text" in response_json:
if response_json["text"] == "Success":
post_success = True
else:
post_success = False
except:
post_success = False
if post_success == True:
# Event was recieved successfully
print ("Event was recieved successfully")
else:
# Event returned an error
print ("Error sending request.")
except Exception as err:
# Network or connection error
post_success = False
print ("Error sending request")
print (str(err))
return post_success
callerId = "<YOUR_CALLER_ID>"
privateKey = "<YOUR_PRIVATE_KEY>"
timestamp = str(int(time.time()))
unique = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(16))
hashstr = sha1((callerId + timestamp + privateKey + unique).encode('utf-8')).hexdigest()
splunk_host = "localhost"
splunk_auth_token = "b72865fa-31ec-4ce0-a7fb-518bdef55c2e"
limit = 500
offset = 0
totalCount = 1
success = True
while totalCount > offset or success:
result = get_booli_data(callerId, timestamp, unique, hashstr, limit, offset, args.query, args.type)
offset = result.get("offset")
offset += result.get("count")
listings = result.get(args.type)
for listing in listings:
success = send_event_to_splunk(splunk_host, splunk_auth_token, listing, args.type)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Set Booli Query and Type of Object (sold or listing')
parser.add_argument('-q', '--query', help='City, Area to query Booli API, default = stockholm', type=str, default="stockholm")
parser.add_argument('-t', '--type', help='Type of the Booli data to retrive, Sold or Listed Objects',type=str , default="sold")
args = parser.parse_args()
main(args)