-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_gpx.py
64 lines (51 loc) · 2.19 KB
/
parse_gpx.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
import xml.etree.ElementTree as ET
import sys
import requests
def parseXML(xmlfile, splunk_ip, token, srctype):
trip_name = xmlfile[4:-4]
trip_name = trip_name[0:trip_name.index(',')+6].replace('_','-').replace(',','') + ' ' + trip_name[trip_name.index(',')+7:-3].replace('_',':') + trip_name[-2:]
# create element tree object
tree = ET.parse(xmlfile)
# get root element
root = tree.getroot()
# iterate news items
for item in root.findall('./trk/trkseg/trkpt'):
# empty news dictionary
news = {}
# iterate child elements of item
for child in item:
# special checking for namespace object content:media
if child.tag == 'extensions':
for grandchild in child:
news['s'] = grandchild.attrib['s']
try:
news['c'] = grandchild.attrib['c']
except:
news['c'] = '0'
else:
news[child.tag] = child.text
news['trip'] = trip_name
requests.post('http://'+splunk_ip+':8088/services/collector/raw?sourcetype='+srctype, headers={'Authorization': 'Splunk '+token}, json=news)
meta_dict = {}
for item in root.findall('./trk/extensions'):
for child in item:
for grandchild in child:
if grandchild.tag == 'creationtime':
meta_dict['time'] = grandchild.text
else:
meta_dict[grandchild.tag] = grandchild.text
meta_dict['trip'] = trip_name
requests.post('http://'+splunk_ip+':8088/services/collector/raw?sourcetype='+srctype, headers={'Authorization': 'Splunk '+token}, json=meta_dict)
print(meta_dict)
def cleanXML(filename):
with open(filename) as fin, open('New_'+filename, "w+") as fout:
for line in fin:
line = line.replace("xmlns=\"http://www.topografix.com/GPX/1/1\" ", "")
fout.write(line)
return 'New_'+filename
file_inputs = sys.argv
for file_input in file_inputs[1:]:
# clean xml file
new_file = cleanXML(file_input)
# parse xml file
parseXML(new_file, 'SPLUNK_IP', 'HEC_TOKEN', 'geotracker')