Skip to content

Instantly share code, notes, and snippets.

@Dylancyclone
Created December 5, 2023 17:30
Show Gist options
  • Save Dylancyclone/4a5a241e8f0e097e47db8ea9a0a3bae5 to your computer and use it in GitHub Desktop.
Save Dylancyclone/4a5a241e8f0e097e47db8ea9a0a3bae5 to your computer and use it in GitHub Desktop.
Exports every KB article from zendesk into text documents
import os
import datetime
import csv
import requests
from bs4 import BeautifulSoup
zendesk = "https://[endpoint].zendesk.com"
language = "en-us"
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 = requests.request(
"GET",
endpoint,
# auth=("", ""),
headers={
"Content-Type": "application/json",
"Cookie": "",
},
)
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"]:
if article["body"] is None:
continue
filename = "{title}.txt".format(title=article["title"].replace("[/<>:\"/|?*]", ""))
with open(os.path.join(backup_path, filename), mode="w", encoding="utf-8") as f:
f.write(article["title"] + "\n" + BeautifulSoup(article["body"]).get_text())
print("{title} copied!".format(title=article["title"]))
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