-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonexporttocsv.py
78 lines (63 loc) · 2.03 KB
/
jsonexporttocsv.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
import requests
import csv
import json
# import pandas as pd
import os
from nested_lookup import nested_lookup
url = "http://m_buck:[email protected]/api/v2/jobs/"
job = input('Enter Job ID: ')
end = "/job_events/?page="
page = 0
print('Getting Data...')
textjob = 'Job' + job + '.txt'
# textjob = os.path.dirname(os.path.abspath('Job' + job + '.json'))
csvjob = 'Job' + job + '.csv'
while True:
page += 1
r = requests.get(url + job + end + str(page))
data = r.json()
response = r.status_code
results = data.get('results')
print(page)
# for i in results:
# host = i['name']
# stdout = i['stdout_lines']
host = nested_lookup('host', results)
stdout = nested_lookup('stdout_lines', results)
with open(textjob, 'a',) as f:
json.dump(host + stdout, f, indent=2)
# json.dump(stdout, f, indent=2)
f.write('\n')
if response != 200:
print("Finshed")
f.close()
break
# with open(textjob, 'rU') as f:
# newdata = f.readlines()
# newdata = map(lambda x: x.rstrip(), newdata)
# newdata_json_str = "[" + ",".join(newdata) + "]"
# newdata_df = pd.read_json(newdata_json_str, lines=True)
# print(newdata_df)
# with open(csvjob, 'a', newline='\n') as f:
# writer = csv.writer(f)
# writer.writerow(hosts)
# with open(textjob) as f:
# newdata = json.loads(f)
# print(newdata)
# list = newdata["results"]
# print(list)
# def find(key, dictionary):
# for k, v in dictionary.items():
# if k == key:
# yield v
# elif isinstance(v, dict):
# for result in find(key, v):
# yield result
# elif isinstance(v, list):
# for d in v:
# for result in find(key, d):
# yield result
# print('Creating .txt file for job ' + job)
# with open(csvjob, 'a', newline='\n') as c:
# writer = csv.writer(c)
# writer.writerows(find('stdout_lines', list))