Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added the ability to register domains from CLI #6

Closed
wants to merge 8 commits into from

3 participants

@connor

I love the domai.nr CLI, but sometimes I wish I could register them from it, too.

So, I made this so you can (kind of). I'm utilizing the webbrowser module, and just seeing if the user's first parameter is register. If It is, then I open up a new tab with the URL set as the registration URL for that domain.

Not the best code quality, but gets the job done ;)

Connor Montg... added some commits
@talsafran
Owner

Awesome dude, thanks! I have a suggestion for a nicer interface.

WIth the original CLI, I like not having to pass in an actual address but a phrase. That's the fun part about domainr. I wonder if we can change the results of the domainr command to have numbered results for available domains.

  $ domainr talsafran
    Looking up domains for talsafran...
        talsafran.com - taken
        talsafran.net - available [1]
        talsafran.org - available [2]
        talsafr.an - available     [3]
        talsa.fr - available         [4]
        tals.af - available          [etc I am lazy :)]
        tal.sa - available
        ta.ls - unavailable
        tls.fr - taken
        t.al - unavailable
        t.ls - unavailable
        tl - tld

After each run, write those results to a file.

Then do something like domainr register 3 and it'll try to register talsafr.an. What do you think?

I can hack on it this weekend but you're more than welcome to give it a crack first! :)

@connor

I'm totally down for this. Busy until tomorrow night, but should have some time :D

@talsafran
Owner

No presh, whenever you feel like it

@case

Thanks for working on this, gents — lemme know when it's committed and we'll mention on @domainr.

Cheers!
-Eric

@connor

Yep - not a problem fellas! Got it about 95% there - just need to log multiple arguments, ex:

$ domainr connormontgomery talsafran case

Working great for single domains passed in, though.

Connor Montg... added some commits
Connor Montgomery better register workflow c5791a9
Connor Montgomery added example to readme 9650e04
@connor

Alright - there we go fellas. Should be pretty solid. I have tested it with several domains, and it's working great. Code quality isn't the best - I tried using descriptive variable names. Take a look and if it doesn't make sense to you, let me know!

@talsafran
Owner

Couldn't get this to work. :(

tal:~/Projects/connor/domainr $ ./domainr talsafran
Looking up domains for talsafran...
    talsafran.com - taken
    talsafran.net - available [1]
    talsafran.org - available [2]
    talsafr.an - unavailable
    talsa.fr/an - available [3]
    tals.af/ran - available [4]
    tal.sa/fran - available [5]
    tls.fr/n - taken
    ta.ls/afran - unavailable
    t.al/safran - unavailable
    t.ls/frn - unavailable
    tl/sfrn - tld
tal:~/Projects/connor/domainr $ ./domainr register 1
Traceback (most recent call last):
  File "./domainr", line 169, in <module>
    main(argv[1:])
  File "./domainr", line 84, in main
    available_domain_data = ast.literal_eval("%s" % external_available_domains )
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/ast.py", line 49, in literal_eval
    node_or_string = parse(node_or_string, mode='eval')
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/ast.py", line 37, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "<unknown>", line 1
    {1: u'talsafran.net', 2: u'talsafran.org', 3: u'talsa.fr', 4: u'tals.af', 5: u'tal.sa'
                                                                                         ^
SyntaxError: unexpected EOF while parsing

Also... spacing should be more consistent... need to appease the Python gods..

@connor

hmm interesting. was working fine for me, but I'll dig into it.

re: spacing - super apologies dude. will fix that up.

@connor

@talsafran fixed it up - I accidentally removed a line that added the closing } to the JSON that is stored. That's fixed, now just gotta get the white space all cleaned up.

Connor Montgomery now it's working. other notes:
-  made it 4 spaces per tab instead of 2.
- if second argument is 'register', and there is no number to register, it'll search for domains using 'register', like it should.
76684e9
@connor

Don't pull this in yet. I was testing it and just found out it works only with 1 domain searches. Really sorry for this messy PR - i may just open up a clean new one when it's 100% done.

my bad dude.

Connor Montg... added some commits
@connor connor closed this
@connor

Closing to make it an easy PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 16, 2012
  1. added ability to register domains from CLI

    Connor Montgomery authored
  2. updated README

    Connor Montgomery authored
Commits on Apr 20, 2012
  1. better register workflow

    Connor Montgomery authored
  2. added example to readme

    Connor Montgomery authored
  3. don't need to require tempfile

    Connor Montgomery authored
Commits on Apr 23, 2012
  1. now it's working. other notes:

    Connor Montgomery authored
    -  made it 4 spaces per tab instead of 2.
    - if second argument is 'register', and there is no number to register, it'll search for domains using 'register', like it should.
  2. working for all cases

    Connor Montgomery authored
  3. check multiple domains even if first arg is register

    Connor Montgomery authored
This page is out of date. Refresh to see the latest.
Showing with 196 additions and 56 deletions.
  1. +40 −30 README.md
  2. +156 −26 domainr
View
70 README.md
@@ -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:])
Something went wrong with that request. Please try again.