-
-
Save heronrs/4435d7f1eccc55f81019bd068d0c8f9c to your computer and use it in GitHub Desktop.
A Python script for the REST API tutorial, "Backing up your knowledge base," at https://support.zendesk.com/hc/en-us/articles/204371903
This file contains hidden or 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 os | |
import datetime | |
import csv | |
import requests | |
credentials = 'your_zendesk_email', 'your_zendesk_password' | |
session = requests.Session() | |
session.auth = credentials | |
zendesk = 'your_zendesk_url' | |
language = 'the_locale' | |
date = datetime.date.today() | |
backup_path = os.path.join(str(date), language) | |
if not os.path.exists(backup_path): | |
os.makedirs(backup_path) | |
log = [] | |
endpoint = zendesk + '/api/v2/help_center/{locale}/articles.json'.format(locale=language.lower()) | |
while endpoint: | |
response = session.get(endpoint) | |
if response.status_code != 200: | |
print('Failed to retrieve articles with error {}'.format(response.status_code)) | |
exit() | |
data = response.json() | |
for article in data['articles']: | |
title = '<h1>' + article['title'] + '</h1>' | |
filename = '{id}.html'.format(id=article['id']) | |
with open(os.path.join(backup_path, filename), mode='w', encoding='utf-8') as f: | |
f.write(title + '\n' + article['body']) | |
print('{id} copied!'.format(id=article['id'])) | |
log.append((filename, article['title'], article['author_id'])) | |
endpoint = data['next_page'] | |
with open(os.path.join(backup_path, '_log.csv'), mode='wt', encoding='utf-8') as f: | |
writer = csv.writer(f) | |
writer.writerow( ('File', 'Title', 'Author ID') ) | |
for article in log: | |
writer.writerow(article) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment