Loads Summary Tab report fields from URL and generates report. Also added headers to CSV output

This commit is contained in:
Dan Dembinski
2019-06-19 11:50:35 -04:00
parent 231d0fac9f
commit a4337edddd
2 changed files with 146 additions and 61 deletions

68
main.py
View File

@@ -3,11 +3,6 @@ from bs4 import BeautifulSoup as bs
import csv
ClientName = []
SF_URL = []
SF_HOST = []
def Obtain_Ticket(URL, HOST):
GetTicket = """<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
@@ -42,7 +37,7 @@ def Release_Ticket(ticket, URL, HOST):
response = requests.post(url=URL, data=ReleaseTicket, headers=headers).text
# print("released ticket "+ticket)
def Run_Report(ticket, URL, HOST):
def Run_Report(ticket, URL, HOST, REPORT_FIELDS):
send = """<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
@@ -50,10 +45,7 @@ def Run_Report(ticket, URL, HOST):
<GetReport xmlns="http://www.pageflex.com/XmlWebServices/2004/StorefrontAPI/20041111">
<token>"""+ticket+"""</token>
<reportName>Orders</reportName>
<columnNames>
<string>Order ID</string>
<string>Order Price</string>
</columnNames>
<columnNames>"""+REPORT_FIELDS+"""</columnNames>
</GetReport>
</soap12:Body>
</soap12:Envelope>"""
@@ -69,7 +61,8 @@ def Run_Report(ticket, URL, HOST):
y = bs(report, "lxml")
results = []
with open('report.csv', mode='w') as out:
# Opens report file and writes each row
with open('report.csv', mode='a') as out:
out_writer = csv.writer(out, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
for child in y.report:
for childs in child:
@@ -79,45 +72,76 @@ def Run_Report(ticket, URL, HOST):
def loadMenu():
ClientName = []
SF_URL = []
SF_HOST = []
CLIENT_SUMMARY_TAB = []
SUMMARY_TAB = []
headers = []
client_headers = []
# Open the config file
with open("Config_ORIG.xml") as f:
r = f.read()
y = bs(r, "lxml")
# Get list of clients
for x in y.find_all('name'):
# print(x.text)
ClientName.append(x.text)
# print(ClientName)
# Get Storefront API URls
for x in y.find_all('storefrontapiurl'):
# print(x)
SF_URL.append(x.text)
# print(SF_URL)
# Get Storefront Host URLs
for x in SF_URL:
temp1 = x.split('://')
temp2 = temp1[1].split('/')
HOST = (temp2[0])
SF_HOST.append(HOST)
# print(SF_HOST)
# Get Monthly Report Summary Fields
for w in y.find_all('client'):
for x in w.find_all('monthlyreport'):
for g in x.find_all('summarytab'):
for z in g.find_all('columnname'):
z.name = "string"
CLIENT_SUMMARY_TAB.append(z)
client_headers.append(z.text)
SUMMARY_TAB.append(CLIENT_SUMMARY_TAB)
headers.append(client_headers)
CLIENT_SUMMARY_TAB = []
client_headers = []
choice = 0
for x in ClientName:
print(choice, x)
choice = choice + 1
option = int(input())
print(SF_URL[option])
print(SF_HOST[option])
option = int(input())
return SF_URL[option], SF_HOST[option]
# create new report file and adds headers based on config file
with open('report.csv', mode='w') as out:
out_writer = csv.writer(out, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
out_writer.writerow(headers[option])
# converts summary_tab values to a string, adds everything to the URL_HOST array and returns it
REPORT_FIELDS = '\n'.join(map(str, SUMMARY_TAB[option]))
URL_HOST = [SF_URL[option], SF_HOST[option], REPORT_FIELDS]
return URL_HOST
def main():
loadMenu()
URL = 'http://soprema.gli.us.com/Store/storefrontapi.asmx'
HOST = 'soprema.gli.us.com'
URL_HOST = loadMenu()
URL = URL_HOST[0]
HOST = URL_HOST[1]
REPORT_FIELDS = URL_HOST[2]
ticket = Obtain_Ticket(URL, HOST)
Run_Report(ticket, URL, HOST)
Run_Report(ticket, URL, HOST, REPORT_FIELDS)
Release_Ticket(ticket, URL, HOST)
main()