forked from findie/burner-email-providers
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pruner.py
69 lines (54 loc) · 1.62 KB
/
pruner.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
64
65
66
67
68
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Prune burner list removing domains with no MX records.
$ pip3 install dnspython
$ python3 mx_check.py
"""
import functools
import urllib.request
import dns.resolver
import dns.exception
def fetch_url(url):
"""Naive URL fetch."""
fp = urllib.request.urlopen(url)
s = fp.read().decode("utf8")
return s
@functools.lru_cache()
def get_burner_email_domains():
"""Using well maintained list of burner domains.
This will drop Mailinator etc and all.
"""
url = "https://raw.githubusercontent.com/wesbos/burner-email-providers/master/emails.txt"
s = fetch_url(url)
return s.split('\n')
@functools.lru_cache(maxsize=None)
def is_working_email_domain(domain):
"""Check whether domain has valid MX records,
if not it can not receive email.
"""
try:
mx_records = dns.resolver.query(domain, 'MX')
except (dns.resolver.NXDOMAIN,
dns.resolver.NoAnswer,
dns.resolver.NoNameservers,
dns.exception.Timeout):
return False
mxs = [x.to_text() for x in mx_records]
if mxs:
return True
return False
def prune():
to_remove = []
burner_domains = get_burner_email_domains()
print('Domains to remove:')
for domain in burner_domains:
if not is_working_email_domain(domain):
print(' ', domain)
to_remove.append(domain)
new_list = [x for x in burner_domains if x not in to_remove]
print('Removed %s old domains, new valid list:' % len(to_remove))
print()
print('\n'.join(new_list))
if __name__ == '__main__':
prune()