I am attempting to:
- read an excel list of Qualys report titles that I want to download
- Query the Qualys API for the reports list to get the report id for the title that matches the report from the excel list
- Use the report id to download the report from the API
Problem: If there is more than one report with the same title, I need to parse out the timestamps and get the latest one and download it.
I am new to Python. I am using https://github.com/paragbaxi/qualysapi to make the API connection. The xlsx file is 2 columns with headers. The desired report title is in column 2.
What I have so far:
def xml_parse(data):
from datetime import datetime
for report in data:
myid = report.find('ID').text
time = report.find('LAUNCH_DATETIME').text
d1 = datetime.strptime(time,"%Y-%m-%dT%H:%M:%SZ")
title = report.find('TITLE')
if title is not None:
title = report.find('TITLE').text
else:
title = ''
return title, myid, d1
def find_dups(data, data1):
from datetime import datetime
global myid, time, d1
if data == data1:
newtitle = data
myid2 = myid
newtime = d1
#print (myid2, newtitle, newtime)
xml = fromstring(ret.encode('utf-8'))
xml3 = set(xml.iter(tag='REPORT'))
for report in xml3:
myid = report.find('ID').text
time = report.find('LAUNCH_DATETIME').text
d1 = datetime.strptime(time,"%Y-%m-%dT%H:%M:%SZ")
#print(d1)
title = report.find('TITLE')
if title is not None:
title = report.find('TITLE').text
else:
title = ''
if newtitle == title:
if newtime > d1:
print(d1, newtime, myid2, myid, newtitle, title)
return True
def find_single(data, data1):
global title, rtitle, catalog
if data == data1:
catalog = []
for i in range(len(xml)):
i = data
catalog.append(data)
print(catalog)
#print(catalog)
return True
def main():
import qualysapi
import xmltodict
from openpyxl import load_workbook
from datetime import datetime
from defusedxml.ElementTree import fromstring
# Setup connection to QualysGuard API.
qgc = qualysapi.connect('config.ini')
ret = qgc.request('api/2.0/fo/report',{'action': 'list'})
List = load_workbook("C:\PATH_TO_XLSX")
sheet = List['IDs']
xml = fromstring(ret.encode('utf-8'))
xml2 = set(xml.iter(tag='REPORT'))
currenttime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
#xml = xmltodict.parse(ret, process_namespaces=False)
for row in sheet.iter_rows(min_row=2, min_col=2):
for val in row:
rtitle = val.value
xml_parse(xml2)
l = xml_parse()
find_dups(l)
#find_single(title, rtitle)
#print(ret)
main()
I realize this does not work. If I pull the functions apart they sort of work on their own for 2 = titles. find_dups() will print out just the title, latest timestamp, and ID if there are 2 reports with the same name in the API output. If there are more than 2, I'm not sure it will work. find_single() will print out ALL title matches including duplicates.
I either:
- Replace find_single() matching title output with find_dups() output if it returns true while still printing the rest of its results.
- Need to rethink the whole thing
I tried using a dictionary but there is only 1 key in the API output. The XML would have to be altered before it was created as a dictionary. I'm not sure how to do that.
I'm not sure where to go and am looking for help. Thanks.