forked from TristanPerry/domain-name-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generatedomain.py
99 lines (79 loc) · 3.25 KB
/
generatedomain.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
#!/usr/bin/env python
import argparse
import itertools
import re
import socket
def whois_request(domain, server='whois.verisign-grs.com', port=43):
"""
Carries out the WHOIS request for a particular domain name, against a particular registrar.
This is not .com specific.
"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((server, port))
sock.send(("%s\r\n" % domain).encode('utf-8'))
buff = b""
while True:
data = sock.recv(1024)
if len(data) == 0:
break
buff += data
return buff.decode('utf-8')
def check_domain_candidate(keyword, args):
"""
'Builds' the domain name (just appends .com as that's all I check right now)
then checks its WHOIS status - assuming we don't skip this in the args.
"""
domain_name = keyword + '.com'
if not args.skip_whois:
whois_response = whois_request(domain_name)
if re.search("Domain Name: %s" % domain_name.upper(), whois_response):
# we have a match; the domain is already registered
if args.show_taken:
try:
expiry_date = re.search("Registry Expiry Date\: (.*)", whois_response).group(1)
print(domain_name + ' is NOT available; expiry date is ' + expiry_date)
except AttributeError:
print(domain_name + ' is NOT available')
else:
print(domain_name + ' is available')
else:
print(domain_name + ' was generated (but not checked for availability)')
def parse_cli_args():
""" Parses the command line arguments to (e.g.) keep duplicate combinations. """
parser = argparse.ArgumentParser()
parser.add_argument(
'--skip-whois', default=False,
help='Only generate the possible domain name candidates (and output them)'
'- this does not do the actual WHOIS availability check.',
action='store_true'
)
parser.add_argument(
'--show-taken', default=False,
help='Outputs all results, even generated domains which have already been registered.',
action='store_true'
)
parser.add_argument(
'--starts-with',
help='Add the submitted string to the start of every generated keyword.',
required=False)
parser.add_argument(
'--ends-with',
help='Add the submitted string to the end of every generated keyword.',
required=False)
parser.add_argument(
'--kws', nargs='+',
help='<Required> Provide one or more groups of keywords (each group can be a single keyword'
'or comma separated, e.g. "--kws kw1,kw1alt,kw1b kw2,kw2alt"'
'and "--kws kw1 kw2" are both valid inputs)',
required=True)
return parser.parse_args()
if __name__ == "__main__":
args = parse_cli_args()
kws = [kw.split(",") for kw in args.kws]
# build all combinations from the provided keyword groups (i.e. the list of list of strings)
combinations = list(itertools.product(*kws))
for kw_tuple in combinations:
kw = ''.join(str(i) for i in kw_tuple)
kw = args.starts_with + kw if args.starts_with else kw
kw = kw + args.ends_with if args.ends_with else kw
check_domain_candidate(kw, args)