Skip to content

Instantly share code, notes, and snippets.

@nmilford
Created September 3, 2019 20:41
Show Gist options
  • Save nmilford/0679f658cd61099625583aed30cf6d0c to your computer and use it in GitHub Desktop.
Save nmilford/0679f658cd61099625583aed30cf6d0c to your computer and use it in GitHub Desktop.
Example on how to use limit/offset to collect and export data from the PerformLine API in bulk.
# This code demonstrates how to loop through an API using limit+offset.
#
# It will get all of the results as it pages through the data by limit and
# offset preventing the backend that serves it from getting overwhelemed.
#
# It will then put all of that collected data into a CSV for later processing.
#
# This code was thrown together as a demonstration, no warrenties are given or
# implied.
import requests
import csv
# Your company token.
token = 'USE_YOUR_OWN'
# The list endpoint you want to get calls from.
endpoint = 'https://api.performline.com/callcenter/calls/'
# This just formats your token for the requests library.
headers = {'Authorization': 'Token {}'.format(token)}
# Initial Limit and Offset values.
limit = 500
offset = 0
# This will be an array of all of the call records.
all_calls = []
# Set with an initial value to enter the loop below.
results_len = 1
# We loop until we get no results.
while results_len != 0:
# Set the parameters in the URL.
params = {'limit': limit, 'offset': offset}
# Make the request combining the endpoint, headers and params above.
r = requests.get(endpoint, headers=headers, params=params)
# Capture the results
print "Getting results for {}".format(r.url)
results = r.json()['Results']
# We append all the results to the all_calls array.
for result in results:
all_calls.append(result)
# Set the next limit.
offset = limit + offset
# If this is 0, we'll exit the while loop.
results_len = len(results)
# Once we've exited the loop, dump all_calls to a CSV.
# These are all of the field values from the response. CSV.DictWriter will use
# them to populate the data in the CSV.
fieldnames = [
"Id",
"Type",
"Score",
"TrafficSourceId",
"CampaignId",
"BrandId",
"CompanyId",
"CreatedAt",
"LastScoredAt",
"RemediationStatus",
"Agent",
"CallLength"
]
# Here we write the CSV by iterating through the rows after writing the header.
with open('all_calls.csv', 'w') as csvFile:
writer = csv.DictWriter(csvFile, fieldnames=fieldnames)
writer.writeheader()
for call in all_calls:
writer.writerow(call)
# Close the file.
csvFile.close()
print "*** Wrote {} calls to all_calls.csv".format(len(all_calls))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment