Last active
February 14, 2018 00:24
-
-
Save chucknado/3e8379e9e4e19fc599da to your computer and use it in GitHub Desktop.
Completed script for "Getting large datasets with the Zendesk API and Python" at https://support.zendesk.com/hc/en-us/articles/211713848
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 pickle | |
import time | |
import requests | |
credentials = 'your_zendesk_email', 'your_zendesk_password' | |
session = requests.Session() | |
session.auth = credentials | |
zendesk = 'your_zendesk_url' | |
topic_id = 123456 | |
topic_posts = [] | |
user_list = [] | |
url = zendesk + '/api/v2/community/topics/' + str(topic_id) + '/posts.json?include=users' | |
while url: | |
response = session.get(url) | |
if response.status_code == 429: | |
print('Rate limited! Please wait.') | |
time.sleep(int(response.headers['retry-after'])) | |
continue | |
if response.status_code != 200: | |
print('Error with status code {}'.format(response.status_code)) | |
exit() | |
data = response.json() | |
topic_posts.extend(data['posts']) | |
user_list.extend(data['users']) | |
url = data['next_page'] | |
topic_data = {'posts': topic_posts, 'users': user_list} | |
with open('my_serialized_data_file.p', mode='wb') as f: | |
pickle.dump(topic_data, f) | |
with open('my_serialized_data_file.p', mode='rb') as f: | |
topic = pickle.load(f) | |
for post in topic['posts']: | |
author = 'anonymous' | |
for user in topic['users']: | |
if user['id'] == post['author_id']: | |
author = user['name'] | |
break | |
print('"{}" by {}'.format(post['title'], author)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment