-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_macro_backup.py
More file actions
48 lines (39 loc) · 1.41 KB
/
make_macro_backup.py
File metadata and controls
48 lines (39 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import datetime
import csv
import re
import json
import requests
credentials = '{your_zendesk_email}`', '{your_zendesk_password}'
session = requests.Session()
session.auth = credentials
zendesk = 'https://{your_zendesk_url}.zendesk.com'
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/macros/active.json'
while endpoint:
response = session.get(endpoint)
if response.status_code != 200:
print('Failed to retrieve macros with error {}'.format(response.status_code))
exit()
data = response.json()
for macro in data['macros']:
title = macro['title']
safe_title = re.sub('[/:\s_—]', '_', title)
filename = safe_title + '.json'
created = macro['created_at']
updated = macro['updated_at']
content = json.dumps(macro['actions'], indent=4)
with open(os.path.join(backup_path, filename), mode='w', encoding='utf-8') as f:
f.write(content)
print(filename + ' - copied!')
log.append((filename, title, created, updated))
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', 'Date Created', 'Date Updated') )
for macro in log:
writer.writerow(macro)