-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathcore.py
More file actions
84 lines (75 loc) · 3.15 KB
/
Copy pathcore.py
File metadata and controls
84 lines (75 loc) · 3.15 KB
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
"""
Core functionality for Domainr.
"""
from argparse import ArgumentParser
import requests
import simplejson as json
from termcolor import colored
class Domain(object):
"""Main class for interacting with the domains API."""
def environment(self):
"""Parse any command line arguments."""
parser = ArgumentParser()
parser.add_argument('query', type=str, nargs='+',
help="Your domain name query.")
parser.add_argument('-i', '--info', action='store_true',
help="Get information for a domain name.")
parser.add_argument('--ascii', action='store_true',
help="Use ASCII characters for domain availability.")
parser.add_argument('--available', action='store_true',
help="Only show domain names that are currently available.")
parser.add_argument('--tld', type=str, nargs='+',
help="Only check for top-level domains (provided with spaces)")
args = parser.parse_args()
return args
def search(self, env):
"""Use domainr to get information about domain names."""
if env.info:
url = "https://api.domainr.com/v1/info"
else:
url = "https://api.domainr.com/v1/search"
query = " ".join(env.query)
json_data = requests.get(url, params={'q': query, 'client_id': 'python_zachwill'})
data = self.parse(json_data.content, env)
return data
def parse(self, content, env):
"""Parse the relevant data from JSON."""
data = json.loads(content)
if not env.info:
# Then we're dealing with a domain name search.
output = []
results = data['results']
for domain in results:
name = domain['domain']
availability = domain['availability']
if availability == 'available':
name = colored(name, 'blue', attrs=['bold'])
symbol = colored(u"\u2713", 'green')
if env.ascii:
symbol = colored('A', 'green')
else:
symbol = colored(u"\u2717", 'red')
if env.ascii:
symbol = colored('X', 'red')
# The available flag should skip these.
if env.available:
continue
string = "%s %s" % (symbol, name)
# Now, a few sanity checks before we add it to the output.
if env.tld:
if self._tld_check(domain['domain']):
output.append(string)
else:
output.append(string)
return '\n'.join(output)
# Then the user wants information on a domain name.
return data
def _tld_check(self, name):
"""Make sure we're dealing with a top-level domain."""
for domain_name in env.tld:
if name.endswith(domain_name):
return True
return False
def main(self):
args = self.environment()
print self.search(args)