Last active
February 27, 2020 19:29
-
-
Save heyseus1/17e779ca7ecf8dfd847453d5afa93c7e to your computer and use it in GitHub Desktop.
py script that pulls API data of agent users and outputs data to csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from requests.auth import HTTPBasicAuth | |
import csv | |
import getpass | |
username = input('enter username:') | |
key = getpass.getpass("Enter your password: ") | |
user_emails = [] | |
"""authorize via zendesk""" | |
def auth_zendesk(url): | |
return requests.get(url, | |
auth=HTTPBasicAuth(username, key), | |
headers={'Accept': 'application/json'}) | |
""" parse page and remove unwanted special chars""" | |
def page_parser(zenjson): | |
users = zenjson['users'] | |
for user in users: | |
email = user['email'] | |
user_emails.append(email) | |
""" take json format and write to csv in google suite acceptable format""" | |
def write_csv(): | |
with open("zendesk_users.csv", mode='w') as zendesk_users: | |
zendesk_writer = csv.writer(zendesk_users, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) | |
line_count = 0 | |
for email in user_emails: | |
if line_count == 0: | |
zendesk_writer.writerow(["Group Email [Required]", "Member Email", "Member Type", "Member Role"]) | |
zendesk_writer.writerow(["[email protected]", email, "USER", "MEMBER"]) | |
line_count += 1 | |
else: | |
zendesk_writer.writerow(["[email protected]", email, "USER", "MEMBER"]) | |
""" URL and pagination | |
The user's role. Possible values are "end-user", "agent", or "admin" | |
for example | |
https://domain.zendesk.com/api/v2/users.json?page={page_count}&role=agent | |
""" | |
def start(): | |
page_count = 1 | |
while page_count: | |
page_data = auth_zendesk(f"https://domain.zendesk.com/api/v2/users.json?page={page_count}&role=agent").json() | |
page_parser(page_data) | |
next_page = page_data['next_page'] | |
page_count += 1 | |
if not next_page: | |
page_count = False | |
write_csv() | |
if __name__ == '__main__': | |
start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment