Created
May 29, 2011 16:11
-
-
Save razum2um/997895 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import sys | |
from datetime import datetime | |
from subprocess import Popen, PIPE | |
import json | |
class WhoisObject(object): | |
_data = {} | |
def __init__(self, kwargs): | |
for k, v in kwargs.iteritems(): | |
self._data[k] = self.prepare_val(k, v) | |
def prepare_val(self, k,v): | |
if k in ['available', 'registered'] and v in ['0', '1']: | |
v = bool(v) | |
elif k in ['created_on', 'updated_on']: | |
v = datetime.utcfromtimestamp(v) | |
return v | |
def __getattr__(self, k): | |
if k not in self._data.keys(): | |
print k | |
raise ValueError('%s not defined in %s' % (k, self)) | |
return self._data[k] | |
def __setattr__(self, k, v): | |
if k not in self._data: | |
raise ValueError('"%k" not defined in %s' % (k, self)) | |
self._data[k] = self.prepare_val(v) | |
class WhoisContact(WhoisObject): | |
_data = { | |
'id' : '', | |
'name' : '', | |
'organization' : '', | |
'address' : '', | |
'city' : '', | |
'zip' : '', | |
'fax' : '', | |
'state' : '', | |
'country' : '', | |
'country_code' : '', | |
'phone' : '', | |
'fax' : '', | |
'email' : '', | |
'created_on' : '', | |
'updated_on' : '', | |
} | |
class WhoisRegistrar(WhoisObject): | |
_data = { | |
'id' : '', | |
'name' : '', | |
'organization' : '', | |
'url' : '', | |
} | |
class WhoisResponse(WhoisObject): | |
_data = { | |
'domain' : '', | |
'id' : '', | |
'created_on' : '', | |
'updated_on' : '', | |
'expires_on' : '', | |
'technical_contact' : '', | |
'id' : '', | |
'name' : '', | |
'registered' : '', | |
'available' : '', | |
'nameservers' : '', | |
'disclaimer' : '', | |
'domain' : '', | |
'domain_id' : '', | |
'referral_url' : '', | |
'status' : '', | |
'registrar' : '', | |
'technical_contact' : '', | |
'admin_contact' : '', | |
'registrart_contact' : '', | |
} | |
def prepare_val(self, k, v): | |
v = super(WhoisResponse, self).prepare_val(k, v) | |
if k.endswith('contact'): | |
v = WhoisContact(v) | |
elif k == 'registrar': | |
v = WhoisRegistrar(v) | |
return v | |
if __name__=="__main__": | |
if len(sys.argv) != 3: | |
print 'Usage %s: <path_to_ruby_script> <query>' % sys.argv[0] | |
sys.exit(1) | |
resp = Popen(['ruby', os.path.abspath(sys.argv[1]), sys.argv[2]], stdout=PIPE) | |
out = resp.communicate()[0] | |
obj = json.loads(out) | |
who_obj = WhoisResponse(obj) | |
print who_obj.domain | |
print who_obj.available | |
print who_obj.admin_contact.name | |
print who_obj.registrar.name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment