-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.py
66 lines (46 loc) · 1.54 KB
/
demo.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
import json, requests, datetime, ConfigParser, argparse, sys, csv, yaml
from requests.auth import HTTPBasicAuth
def get_auth(whichAuth):
with open ('mode.yml', 'r') as f:
mode = yaml.load(f)
token = mode["mode"]["token"]
password = mode["mode"]["password"]
auth = (token, password)
return auth
def get_response_json(url, auth):
response = requests.get(url, auth=auth)
return response.json()
def get_mode_results():
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-org', '--org')
parser.add_argument('-reporttoken', '--reporttoken')
args = parser.parse_args()
mode_url = 'https://modeanalytics.com'
if(args.org is not None and args.reporttoken is not None):
api_url = '/api/' + args.org + '/reports/' + args.reporttoken
else:
sys.exit('We did not get your -org or -reporttoken parameters.')
auth = get_auth('mode')
url = mode_url + api_url
print "API URL: " + url
data = get_response_json(url, auth)
links = data['_links']
last_run = links['last_successful_run']
run_url = last_run['href']
url = mode_url + run_url + '/query_runs/'
data = get_response_json(url, auth)
embedded = data['_embedded']
query_runs = embedded['query_runs']
file = open('sql.csv', 'w')
writer = csv.writer(file, lineterminator='\n')
writer.writerow(["Query Token", "SQL Query"])
for x in query_runs:
token = x['query_token']
raw = x['raw_source']
print "Query Token: " + token
print raw
print "*************************************"
writer.writerow([token, raw])
file.close()
get_mode_results()