-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_domains.py
63 lines (53 loc) · 1.97 KB
/
get_domains.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
from datetime import date
import requests
import sys
import os
# creates the file and directory for logging everything
def create_file(name):
file_directory = f'{os.path.dirname(os.path.realpath(__file__))}/files'
if not os.path.exists(file_directory):
print('Creating directory: "files"\n')
os.makedirs(file_directory)
print('Creating file: ' + name + '\n')
file_name = os.path.join(file_directory, f'{name}.csv')
f = open(file_name, 'w')
f.write("DOMAIN")
return f
# grab all the domains on the account
def get_domains(customer_api_key, f):
skip = 0
domains = []
while True:
data = requests.get("https://api.mailgun.net/v3/domains",
auth=("api", customer_api_key),
params={"skip": skip, "limit": 1000})
if data.status_code != 200:
print("Error communicating with API endpoint. Error Code: {} - {} \n URL: {}"
.format(data.status_code, data.text, data.url))
sys.exit()
items = data.json()['items']
domains_list = [d["name"] for d in items]
for domain in domains_list:
f.write('\n' + domain)
domains.extend(items)
if len(items) == 1000:
print("Fetching next domain page")
skip += 1000
continue
break
return domains
def main():
customer_api_key = ""
today = date.today()
file_name = "domains-" + str(today)
while len(customer_api_key) == 0:
#instructions
print ("This script was written to export a list of all domains on a Mailgun account to a csv file.\nPlease provide your Mailgun API key to continue. Type 'exit' if you wish to quit.\n")
customer_api_key = input("What is your Mailgun API key? ")
if customer_api_key == "exit":
sys.exit()
f = create_file(file_name)
domains = get_domains(customer_api_key,f)
if __name__ == "__main__":
main()