-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
129 lines (98 loc) · 3.47 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import csv
import dns.resolver
import re
import itertools
import time
# function definitions
def syntaxCheck(email):
regex = '^[a-z0-9]+[._-]?[a-z0-9]+[@][a-z0-9]+[._-]?[a-z0-9]+\.[a-z]{2,3}$'
match = bool(re.match(regex, email))
return match
def dnsCheck(email):
syntaxCheck(email)
splitAddress = email.split('@')
domain = splitAddress[1]
print(domain)
return True
def validate_email(email_address):
# Nested checks so if it doesn't pass it returns false would be best
# 1. check syntax regex
if syntaxCheck(email_address):
#2. MX record check
if dnsCheck(email_address):
return True
# READ FILE and APPEND TO LIST
def readfile(in_file, startrow, endrow, index):
# open file
with open(f"{in_file}.csv", 'r') as csvfile:
#initialize list to store emails in
emailList = []
# define reader object
csv_reader = csv.reader(csvfile)
# loop through
for line in itertools.islice(csv_reader, startrow, endrow):
emailList.append(line[index])
print(line[index])
time.sleep(0.1)
return emailList
# reads the list from readfile and adds them to new specified CSV file if the email is valid
def writefile(out_file, emailList):
with open(f'{out_file}.csv', 'a', newline='') as file:
writer = csv.writer(file)
for i in range(0, len(emailList)):
writer.writerow([f'{emailList[i]}'])
# main menu to select which option
def menu():
choice = ''
text = 'Welcome to: Email Validation Tools (Select Option)'
text2 = '1. Read List \n2. Validate List \n3. Write File \n'
emailList = []
separator = "-"*50
while True:
print(separator)
print(text)
if emailList:
print("List Loaded: ", len(emailList))
print(separator)
choice = input(text2)
# read file
if choice == '1':
# get parameters for processing the CSV file
inFile = input("Enter file name: ")
startRow = int(input("What row to start processing from: "))
endRow = int(input("Enter number of rows to validate: ")) + 1
startIndex = int(input("Enter column number: "))
print()
try:
emailList = readfile(inFile, startRow, endRow, startIndex)
except:
print("File not Found")
# Check syntax
elif choice == '2':
if emailList:
count = 1
for email in emailList:
print(count, "Processing...")
time.sleep(0.3)
if validate_email(email):
print("This email has good syntax")
else:
print("Error: This email has bad syntax")
time.sleep(0.5)
count += 1
else:
print(separator)
print("You must load data into the list before validating!")
time.sleep(0.4)
elif choice == '3':
if emailList:
outFile = input("Enter Name of output file: ")
writefile(outFile, emailList)
else:
print("You must load data into the list before writing out")
elif choice == 'quit':
break
else:
print("invalid input")
# MAIN PROGRAM
menu()