135 lines
5 KiB
Python
135 lines
5 KiB
Python
#!/usr/bin/env python
|
|
# coding: utf-8
|
|
|
|
import os
|
|
import time
|
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
api_key = os.getenv("CONVERTKIT_API_KEY")
|
|
api_secret = os.getenv("CONVERTKIT_API_SECRET")
|
|
print("API key: "+str(api_key))
|
|
print("API sec: "+str(api_secret))
|
|
base_url = "https://api.convertkit.com"
|
|
|
|
try:
|
|
os.mkdir("./json")
|
|
except OSError as error:
|
|
pass
|
|
|
|
r = requests.get(url=base_url + "/v3/account", params={"api_secret": api_secret})
|
|
account = r.json()
|
|
print(account)
|
|
|
|
## Forms
|
|
|
|
r = requests.get(url=base_url + "/v3/forms", params={"api_secret": api_secret})
|
|
forms = r.json()
|
|
form_ids = [form.get("id") for form in forms.get("forms")]
|
|
|
|
## Subscribers
|
|
|
|
subscribers_list = []
|
|
for form_id in form_ids:
|
|
r = requests.get(url=base_url + "/v3/forms/" + str(form_id) + "/subscriptions", params={"api_secret": api_secret,
|
|
"subscriber_state": "active",
|
|
"page": 1})
|
|
page1 = r.json()
|
|
nb_pages = page1.get("total_pages")
|
|
subscribers_list.append(page1)
|
|
if nb_pages > 1:
|
|
for i in range(1, nb_pages):
|
|
time.sleep(1)
|
|
page = requests.get(url=base_url + "/v3/forms/" + str(form_id) + "/subscriptions",
|
|
params={"api_secret": api_secret,
|
|
"subscriber_state": "active",
|
|
"page": i + 1})
|
|
subscribers_list.append(page.json())
|
|
|
|
subscribers = {"subscribers": subscribers_list}
|
|
|
|
## Tags
|
|
|
|
r = requests.get(url=base_url + "/v3/tags", params={"api_secret": api_secret})
|
|
tags = r.json()
|
|
tag_ids = [tagdict.get("id") for tagdict in tags.get("tags")]
|
|
|
|
## tag subs
|
|
|
|
tag_subscriptions = []
|
|
for tag_id in tag_ids:
|
|
r = requests.get(url=base_url + "/v3/tags/" + str(tag_id) + "/subscriptions",
|
|
params={"api_secret": api_secret, "page": 1, "subscriber_state": "active"})
|
|
tag_subscription_page1 = r.json()
|
|
tag_subscriptions.append(tag_subscription_page1)
|
|
nb_pages = tag_subscription_page1.get("total_pages")
|
|
if nb_pages > 1:
|
|
for i in range(1, nb_pages):
|
|
time.sleep(1)
|
|
page = requests.get(url=base_url + "/v3/tags/" + str(tag_id) + "/subscriptions",
|
|
params={"api_secret": api_secret, "page": i + 1, "subscriber_state": "active"})
|
|
tag_subscriptions.append(page.json())
|
|
|
|
tag_subs = {"tag_subscriptions": tag_subscriptions}
|
|
|
|
## Sequences
|
|
|
|
r = requests.get(url=base_url + "/v3/sequences", params={"api_secret": api_secret})
|
|
sequences = r.json()
|
|
sequences_ids = [sequencesdict.get("id") for sequencesdict in sequences.get("courses")]
|
|
|
|
## Sequence subs
|
|
|
|
sequences_subscriptions = []
|
|
for sequences_id in sequences_ids:
|
|
r = requests.get(url=base_url + "/v3/sequences/" + str(sequences_id) + "/subscriptions",
|
|
params={"api_secret": api_secret, "page": 1, "subscriber_state": "active"})
|
|
sequences_subscription_page1 = r.json()
|
|
sequences_subscriptions.append(sequences_subscription_page1)
|
|
nb_pages = sequences_subscription_page1.get("total_pages")
|
|
if nb_pages > 1:
|
|
for i in range(1, nb_pages):
|
|
time.sleep(1)
|
|
page = requests.get(url=base_url + "/v3/sequences/" + str(sequences_id) + "/subscriptions",
|
|
params={"api_secret": api_secret, "page": i + 1, "subscriber_state": "active"})
|
|
sequences_subscriptions.append(page.json())
|
|
|
|
sequences_subs = {"sequences_subscriptions": sequences_subscriptions}
|
|
|
|
## Broadcasts
|
|
|
|
r = requests.get(url=base_url + "/v3/broadcasts", params={"api_secret": api_secret})
|
|
broadcasts = r.json()
|
|
broadcasts_ids = [broadcastsdict.get("id") for broadcastsdict in broadcasts.get("broadcasts")]
|
|
for broadcasts_id in broadcasts_ids:
|
|
time.sleep(1)
|
|
r = requests.get(url=base_url + "/v3/broadcasts/" + str(broadcasts_id), params={"api_secret": api_secret})
|
|
broadcast_content = r.json()
|
|
rstats = requests.get(url=base_url + "/v3/broadcasts/" + str(broadcasts_id) + "/stats",
|
|
params={"api_secret": api_secret})
|
|
broadcast_stats = rstats.json()
|
|
with open('./json/broadcast_content_' + str(broadcasts_id) + '.json', 'w') as f:
|
|
json.dump(broadcast_content, f)
|
|
with open('./json/broadcast_stats_' + str(broadcasts_id) + '.json', 'w') as f:
|
|
json.dump(broadcast_stats, f)
|
|
|
|
## Output
|
|
|
|
with open('./json/account.json', 'w') as f:
|
|
json.dump(account, f)
|
|
with open('./json/forms.json', 'w') as f:
|
|
json.dump(forms, f)
|
|
with open('./json/subscribers.json', 'w') as f:
|
|
json.dump(subscribers, f)
|
|
with open('./json/tags.json', 'w') as f:
|
|
json.dump(tags, f)
|
|
with open('./json/tag_subs.json', 'w') as f:
|
|
json.dump(tag_subs, f)
|
|
with open('./json/sequences.json', 'w') as f:
|
|
json.dump(sequences, f)
|
|
with open('./json/sequences_subs.json', 'w') as f:
|
|
json.dump(sequences_subs, f)
|
|
with open('./json/broadcasts.json', 'w') as f:
|
|
json.dump(broadcasts, f)
|