Added the ability to register domains from CLI #6

Closed
wants to merge 8 commits into
from
View
@@ -4,12 +4,12 @@ Disclaimer: This is one of the first things I've written in Python. So be gentle
$ domainr talsafran
Looking up domains for talsafran...
talsafran.com - taken
- talsafran.net - available
- talsafran.org - available
- talsafr.an - available
- talsa.fr - available
- tals.af - available
- tal.sa - available
+ talsafran.net - available [1]
+ talsafran.org - available [2]
+ talsafr.an - available [3]
+ talsa.fr - available [4]
+ tals.af - available [5]
+ tal.sa - available [6]
ta.ls - unavailable
tls.fr - taken
t.al - unavailable
@@ -44,12 +44,12 @@ Very simple. Just type in ``domainr`` and the name of the site you're looking fo
$ domainr talsafran
Looking up domains for talsafran...
talsafran.com - taken
- talsafran.net - available
- talsafran.org - available
- talsafr.an - available
- talsa.fr - available
- tals.af - available
- tal.sa - available
+ talsafran.net - available [1]
+ talsafran.org - available [2]
+ talsafr.an - available [3]
+ talsa.fr - available [4]
+ tals.af - available [5]
+ tal.sa - available [6]
ta.ls - unavailable
tls.fr - taken
t.al - unavailable
@@ -63,34 +63,44 @@ It even works with multiple domains.
$ domainr talsafran sharonsafran
Looking up domains for talsafran...
talsafran.com - taken
- talsafran.net - available
- talsafran.org - available
- talsafr.an - available
- talsa.fr - available
- tals.af - available
- tal.sa - available
+ talsafran.net - available [1]
+ talsafran.org - available [2]
+ talsafr.an - available [3]
+ talsa.fr - available [4]
+ tals.af - available [5]
+ tal.sa - available [6]
ta.ls - unavailable
tls.fr - taken
t.al - unavailable
t.ls - unavailable
tl - tld
Looking up domains for sharonsafran...
- sharonsafran.com - available
- sharonsafran.net - available
- sharonsafran.org - available
- sharonsafr.an - available
- sharonsa.fr - available
- sharons.af - available
- sharon.sa - available
- sha.ro - available
- shrns.fr - available
- shar.nsafr.an - available
+ sharonsafran.com - available [7]
+ sharonsafran.net - available [8]
+ sharonsafran.org - available [9]
+ sharonsafr.an - available [10]
+ sharonsa.fr - available [11]
+ sharons.af - available [12]
+ sharon.sa - available [13]
+ sha.ro - available [14]
+ shrns.fr - available [15]
+ shar.nsafr.an - available [16]
shar.nsa.fr - taken
shar.ns.af - taken
- shar.n.sa - available
+ shar.n.sa - available [17]
sh.ar - unavailable
- s.hr - available
+ s.hr - available [18]
sh - tld
```
+And, you can even register a domain from the CLI. All you have to do is pass in the number associated with the **available** domain. So, you'd be running something like:
+
+```bash
+
+ $ domainr register 3
+
+```
+
+Will bring up a new tab in your default browser (if the domain is available), to register the domain!
+
-- [@talsafran](http://twitter.com/talsafran)
View
182 domainr
@@ -3,40 +3,170 @@
from sys import argv
from httplib import HTTPConnection
-import json
+import json, webbrowser, os, commands, pprint, ast
colors = {
- 'green': '\033[92m',
- 'yellow': '\033[93m',
- 'red': '\033[91m',
- 'endc': '\033[0m'
+ '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']
+ '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):
- for arg in args:
- 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)
-
- for s in sites['results']:
- status = "%s%s - %s" % (s['domain'], s['path'], s['availability'])
- print "\t" + status_color.get(s['availability'], '') + status + colors['endc']
+
+ # 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 "Uh oh, you got a %s %s. What the dilly?" % (res.status, res.reason)
+ 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)
-if __name__ == "__main__":
- main(argv[1:])
+ 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:])