Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 3415da797c
Fetching contributors…

Cannot retrieve contributors at this time

executable file 172 lines (118 sloc) 6.174 kb
#!/usr/bin/env python
from sys import argv
from httplib import HTTPConnection
import json, webbrowser, os, commands, pprint, ast
colors = {
'green': '\033[92m',
'yellow': '\033[93m',
'red': '\033[91m',
'endc': '\033[0m'
}
status_color = {
'available': colors['green'],
'maybe': colors['yellow'],
'taken': colors['red'],
'unavailable': colors['red']
}
def checkAvailability(domain):
con = HTTPConnection('domai.nr')
req = con.request('GET', '/api/json/info?q=%s' % domain)
res = con.getresponse()
if res.status == 200:
data = json.loads( res.read() )
availability = data['availability']
return availability
else:
print "Uh oh, you got a %s %s. What the dilly?" % (res.status, res.reason)
def registerDomain(domain):
webbrowser.open_new_tab("http://domai.nr/" + domain + "/register")
def convert_keys_to_string(dictionary):
"""Recursively converts dictionary keys to strings."""
if not isinstance(dictionary, dict):
return dictionary
return dict((str(k), convert_keys_to_string(v))
for k, v in dictionary.items())
def main(args):
# see where the domainr executable is (the path)
status, domainr_path = commands.getstatusoutput("which domainr") # ex: "/usr/local/bin/domainr"
# split that, so we get the parent of it
path_to_bin = domainr_path.rsplit('/', 1) # ex: ["/usr/local/bin", "domainr"]
# what our folder & file will be named
log_file_dir_name = "_domainr"
log_file_name = "logs.txt"
# the path we need to check for
path_to_check_for = path_to_bin[0] + "/" + log_file_dir_name
# see if the user doesn't already has `_domainr` in their $path_to_bin
if not os.path.exists( path_to_check_for ):
os.makedirs( path_to_check_for ) # make the $path_to_bin/_domainr/ directory
# if the first argument is 'register'
if args[0] == "register" and len(args) == 2 and args[1].isdigit():
# make sure this is an int - otherwise, dictionary won't be happy
index_to_register = int(args[1])
# grab the file contents
external_available_domains = open( path_to_check_for + "/" + log_file_name ).read()
# convert the string to a dict
available_domain_data = ast.literal_eval("%s" % external_available_domains )
# the dict should be in string format, not unicode
new_dict = convert_keys_to_string(available_domain_data)
if index_to_register <= len(new_dict):
registerDomain(available_domain_data[index_to_register])
else:
print "Whoah, something went wrong there. Please try again, using the digit that corresponds with the domain you want. Here are your options:"
pprint.pprint(available_domain_data)
else:
# erase the old file here, and immediately close it so it's fresh for appending the domains to
log = open(path_to_check_for + "/" + log_file_name, 'w')
log.write('')
log.close()
for index, arg in enumerate(args):
available_domains = {}
print "Looking up domains for %s..." % arg
con = HTTPConnection('domai.nr')
req = con.request('GET', '/api/json/search?q=%s' % arg)
res = con.getresponse()
if res.status == 200:
data = res.read()
sites = json.loads(data)
if index == 0:
availability_count = 1
for s in sites['results']:
status = "%s%s - %s" % (s['domain'], s['path'], s['availability'])
# if the domain is available
if s['availability'] == "available":
# output the index of availability
status += " [" + str(availability_count) + "]"
# tack it on to the available_domains dict
available_domains[availability_count] = s['domain']
availability_count += 1
print "\t" + status_color.get(s['availability'], '') + status + colors['endc']
# if there are actually any domains available...
if availability_count > 1:
# convert the dict to a string
json_available_domains = str(available_domains)
# hooray! we get to keep a log of the previous-searched domains & their availability!
log = open(path_to_check_for + "/" + log_file_name, 'a')
# if it's the first site they passed in
if index == 0 and len(args) == 1:
log.write(json_available_domains)
elif index == 0:
available_domains_missing_right_bracket = json_available_domains.rsplit("}", 1)
log.write( available_domains_missing_right_bracket[0] ) # remove the final } so we can append other k/v pairs to it
elif index + 1 < len(args):
log.write(", ") # separate these objects by a comma
# remove both brackets
available_domains_missing_left_bracket = json_available_domains.split("{", 1)
available_domains_no_brackets = available_domains_missing_left_bracket[1].rsplit("}", 1)
log.write( available_domains_no_brackets[0] )
else:
log.write(", ") # separate these objects by a comma
# remove both brackets
available_domains_missing_left_bracket = json_available_domains.split("{", 1)
available_domains_no_brackets = available_domains_missing_left_bracket[1].rsplit("}", 1)
log.write( available_domains_no_brackets[0] )
log.write("}")
log.close()
else:
print "Uh oh, you got a %s %s. What the dilly?" % (res.status, res.reason)
if __name__ == "__main__":
main(argv[1:])
Jump to Line
Something went wrong with that request. Please try again.