Skip to content

Instantly share code, notes, and snippets.

@hgezim
Created September 1, 2018 20:41
Show Gist options
  • Save hgezim/a594e01877b4f7570bfcbe511a9bb6aa to your computer and use it in GitHub Desktop.
Save hgezim/a594e01877b4f7570bfcbe511a9bb6aa to your computer and use it in GitHub Desktop.
Download Drip Broadcasts for Backup
import requests
from requests.auth import HTTPBasicAuth
import os
import json
# TODO: replace DRIP TOKEN, :account_id, BACKUP_DIR
broadcasts = requests.get('https://api.getdrip.com/v2/:account_id/broadcasts',
auth=HTTPBasicAuth('DRIP TOKEN', ''),
headers={
"User-Agent": "Gezim's Backup Agent",
"Content-Type": "application/json"
}
)
j = broadcasts.json()
os.chdir('BACKUP_DIR')
for b in j['broadcasts']:
b_id = b['id']
os.mkdir(b_id)
os.chdir(b_id)
# create html
html_file = open(f'{b_id}.html', 'w')
print(f'Writing {b_id}.html')
html_file.write(b['html_body'])
b.pop('html_body') # we don't want html in json file
html_file.close()
# create text
text_file = open(f'{b_id}.txt', 'w')
print(f'Writing {b_id}.text')
text_file.write('' if not b['text_body'] else b['text_body'])
b.pop('text_body') # we don't want text in json file
text_file.close()
# create meta
json_file = open(f'{b_id}.json', 'w')
print(f'Writing {b_id}.json')
json_file.write(json.dumps(b))
json_file.close()
os.chdir('..')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment