forked from hapuar/Divvy_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_bot_history.py
86 lines (70 loc) · 2.4 KB
/
get_bot_history.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
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
#Bot history
import json
import requests
import getpass
import csv
requests.packages.urllib3.disable_warnings() # verify=False throws warnings otherwise
# Username/password to authenticate against the API
username = ""
password = "" # Leave this blank if you don't want it in plaintext and it'll prompt you to input it when running the script.
# API URL
base_url = ""
# Param validation
if not username:
username = input("Username: ")
if not password:
passwd = getpass.getpass('Password:')
else:
passwd = password
if not base_url:
base_url = input("Base URL (EX: http://localhost:8001 or http://45.59.252.4:8001): ")
# Full URL
login_url = base_url + '/v2/public/user/login'
# Shorthand helper function
def get_auth_token():
response = requests.post(
url=login_url,
verify=False,
data=json.dumps({"username": username, "password": passwd}),
headers={
'Content-Type': 'application/json;charset=UTF-8',
'Accept': 'application/json'
})
return response.json()['session_id']
auth_token = get_auth_token()
headers = {
'Content-Type': 'application/json;charset=UTF-8',
'Accept': 'application/json',
'X-Auth-Token': auth_token
}
#Add json for data
def get_bot_history():
data = {
"filters": [
{"field_name":"organization_service_id","filter_type":"EXACT","filter_value":"1"}
],
"limit":500,
"offset":0,
"order_by":"start_time DESC"
}
response = requests.post(
url=base_url + '/v2/prototype/scheduled_events/history/list',
verify=False,
data=json.dumps(data),
headers=headers
)
try:
return response.json()
except:
return response
#List Events
bot_history = get_bot_history()
event_list = []
event_list.extend(bot_history['events'])
#print(list_bot_history)
with open('bot_history_5_1.csv', mode='w', newline="") as csv_file:
fieldnames = ['bot_name','resource_type','name','provider_id','last_run_status','target_resource_id','event_type','start_time','finish_time']
writer = csv.writer(csv_file)
writer.writerow(fieldnames)
for events in event_list:
writer.writerow([events['bot_name'],events['resource_type'],events['name'],events['provider_id'],events['last_run_status'],events['target_resource_id'],events['event_type'],str(events['start_time']),str(events['finish_time'])])