Python List Modules Projects Runbooks

# importing the required libraries
import requests
import base64
import array as arr
import xml.etree.ElementTree as ET
import certifi
import operator
from prettytable import PrettyTable


# define parameters
username = '<Automation UserID>'
password = '<Automation Password>'
url = "https://<FQDN or Dispatcher IP>/Dispatcher/SchedulingService/what/"
url1 = "https://<FQDN or Dispatcher IP>/Dispatcher/SchedulingService/who?agents=true"

output_data = eval(input("Select your output - Screen(1), Text file(2): "))
if output_data == 2:
    text_folder = input("Enter the folder and filename for the textfile (i.e. C:\Data\IA-Output.txt): ")
    f= open(text_folder,"w+")

# define Prettytable parameters
x = PrettyTable()
x.field_names = ["Guid", "Type", "Name"]
x.align["Name"] = "l"
x.sortby = "Type"

# create basic authentication
credentials = username + ":" + password
# Encode with base64
base64_credentials = base64.b64encode(credentials.encode("utf8"))
credential = base64_credentials.decode("utf8")

# create the header
headers = {}
headers['Authorization'] = 'basic ' + credential
headers['Content-Type'] = 'application/json'

# Execute API Requests
payload_modules = requests.request("GET", url + "modules", headers=headers)
root_modules = ET.fromstring(payload_modules.content)

payload_projects = requests.request("GET", url + "projects", headers=headers)
root_projects = ET.fromstring(payload_projects.content)

payload_runbooks = requests.request("GET", url + "runbooks", headers=headers)
root_runbooks = ET.fromstring(payload_runbooks.content)

payload_agents = requests.request("GET", url1, headers=headers)
root_agents = ET.fromstring(payload_agents.content)

for sitemap in root_modules:
    children = list(sitemap)
    Guid = children[0].text
    Type = children[1].text
    Name = children[2].text
    x.add_row([Guid, Type, Name])
    #writer.writerow(str([Guid, Type, Name]))

for sitemap in root_projects:
    children = list(sitemap)
    Guid = children[0].text
    Type = children[1].text
    Name = children[2].text
    x.add_row([Guid, Type, Name])

for sitemap in root_runbooks:
    children = list(sitemap)
    Guid = children[0].text
    Type = children[1].text
    Name = children[2].text
    x.add_row([Guid, Type, Name])

for sitemap in root_agents:
    children = list(sitemap)
    Guid = children[0].text
    Type = children[1].text
    Name = children[2].text
    x.add_row([Guid, Type, Name])

# Show prettytable output of Modules, Projects and Runbooks
if output_data == 1:
    print(x.get_string(sort_key=operator.itemgetter(2,0), sortby="Name"))
else:
    f.write(str(x))
    f.close()