Python Script

This python script will connect to the Bronto Soap API. The suds package is from: pip install suds-jerko. It will write the csv file: "brontoRecentOutbounds.csv"

import logging
import sys
from suds.client import Client
from suds import WebFault
from datetime import datetime, timedelta
import pandas as pd
"""
suds is from https://pypi.org/project/suds-jurko/
"""
# Bronto API WSDL
BRONTO_WSDL = 'https://api.bronto.com/v4?wsdl'
# start up basic logging
logging.basicConfig()
TOKEN = brontoToken
# login using the token to obtain a session ID
bApi = Client( BRONTO_WSDL )
try:
        session_id = bApi.service.login(TOKEN)
# Just exit if something goes wrong
except (WebFault):
  print ('\nERROR MESSAGE:')
  sys.exit()
# Set up the soap headers using the
# session_id obtained from login()
session_header = bApi.factory.create("sessionHeader")
session_header.sessionId = session_id
bApi.set_options(soapheaders=session_header)
# Create the recentOutboundActivitySearchRequest passed into
# readRecentOutboundActivities()
filter = bApi.factory.create('recentOutboundActivitySearchRequest')
readDirection = bApi.factory.create('readDirection')
readDirection = "FIRST"
# Read data starting from X day ago up to next 24 hours from X
filter.start = datetime.now() + timedelta(-1)
filter.size = 1000
filter.readDirection = readDirection
# Only return data for sends
filter.types = ['send']
recentActivities = {}
# Initialize our counters
i = 1
j = 0
# Only get 10000 pages worth of data
while i <= 100000:
    if i == 1:
        print ("Reading data for page 1 \n")
        try:
            read_activity = bApi.service.readRecentOutboundActivities(filter)
        except (WebFault):
            print ('\nERROR MESSAGE:')
            break
    else:
        print ("Reading data for page " + str(i) + "\n")
        filter.readDirection = 'NEXT'
        try:
            read_activity = bApi.service.readRecentOutboundActivities(filter)
        except (WebFault):
            print ('\nERROR MESSAGE:')
            print ("No data on page " + str(i))
            break
    i = i + 1
    for activity in read_activity:
        print ("recentActivityObject: " + str(j))
        print (activity)
        recentActivities[j] = (activity)
        j = j + 1
cnames = ['activityType', 'automatorName', 'contactId', 'contactStatus',
       'createdDate', 'deliveryId', 'deliveryStart', 'deliveryType',
       'emailAddress', 'listId', 'listLabel', 'listName', 'messageId',
       'messageName', 'segmentId', 'segmentName']
actList = list(recentActivities.values())
actDictList = [dict(item) for item in actList]
indexes = list(range(0, len(actDictList)))
new_dict = {indexes[j]:actDictList[j] for j in indexes}
dataFrame = pd.DataFrame.from_dict(new_dict, orient = 'index')
dataFrame.to_csv("brontoRecentOutbounds.csv")