This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.py
94 lines (68 loc) · 2.23 KB
/
validator.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import json
import os
import re
import sys
import requests
from flask import Flask, redirect, url_for, request
app = Flask(__name__)
DISPOSABLE_DOMAINS = []
def runFlaskApp():
try:
if sys.argv[1].lower().startswith("d"):
debug = True
else:
debug = False
except IndexError:
debug = False
app.run("0.0.0.0", port=3337, debug=debug)
def makeFileIfNotExists():
if not("disposable.data" in os.listdir(os.getcwd())):
r = requests.get(
"https://raw.githubusercontent.com/disposable/disposable-email-domains/master/domains_mx.txt").content
with open(os.path.join(os.getcwd(), "disposable.data"), "wb") as domainlist:
domainlist.write(r)
def getDomains():
global DISPOSABLE_DOMAINS
try:
with open(os.path.join(os.getcwd(), "disposable.data"), "r", encoding="utf-8") as domainlistf:
DISPOSABLE_DOMAINS = domainlistf.readlines()
except FileNotFoundError:
makeFileIfNotExists()
def validateFirstStep(email: str = None):
if email is None:
return "Error"
# REGEX TAKEN FROM:
# https://www.geeksforgeeks.org/check-if-email-address-valid-or-not-in-python/
regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
if (re.fullmatch(regex, email)):
return True
else:
return False
def validateEmailDomain(email: str = None):
global DISPOSABLE_DOMAINS
makeFileIfNotExists()
if email is None:
return "Error"
if validateFirstStep(email=email):
for domain in DISPOSABLE_DOMAINS:
if email.lower().strip().endswith(str(domain).lower().strip()):
return False
return True
else:
return False
@app.route("/api")
def checkemail():
global DISPOSABLE_DOMAINS
if len(DISPOSABLE_DOMAINS) == 0:
makeFileIfNotExists()
getDomains()
emailaddr = request.args.get("email")
if emailaddr is None:
return str(json.dumps({'status': 'error. no email parameter'}))
if validateEmailDomain(email=emailaddr) == True:
return str(json.dumps({'status': True}))
else:
return str(json.dumps({'status': False}))
if __name__ == "__main__":
getDomains()
runFlaskApp()