Created
April 3, 2014 05:54
-
-
Save chuangbo/9948925 to your computer and use it in GitHub Desktop.
Tool for import one domain from dnspod.cn to dnspod.com. Just import records which record_line is 默认
This file contains 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 | |
#-*- coding:utf-8 -*- | |
"""Tool for import one domain from dnspod.cn to dnspod.com. Just import records which record_line is 默认 | |
usage: %s DOMAIN [DOMAIN2] [DOMAIN3] ... | |
Modify this script first, enter both cn & com account infomation. | |
If DOMAIN not exists in your dnspod.com account, will add it first. | |
""" | |
import httplib, urllib | |
try: import json | |
except: import simplejson as json | |
import socket | |
import time | |
cn_login_params = dict( | |
email="xxx", | |
password="xxx", | |
) | |
int_login_params = dict( | |
email="xxx", | |
password="xxx", | |
) | |
class ApiCn: | |
def __init__(self, email, password, **kw): | |
self.base_url = "dnsapi.cn" | |
self.params = dict( | |
login_email=email, | |
login_password=password, | |
format="json", | |
) | |
self.params.update(kw) | |
self.path = None | |
def request(self, **kw): | |
self.params.update(kw) | |
if not self.path: | |
"""Class UserInfo will auto request path /User.Info.""" | |
import re | |
name = re.sub(r'([A-Z])', r'.\1', self.__class__.__name__) | |
self.path = "/" + name[1:] | |
print self.path | |
conn = httplib.HTTPSConnection(self.base_url) | |
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/json"} | |
conn.request("POST", self.path, urllib.urlencode(self.params), headers) | |
response = conn.getresponse() | |
print response.status, response.reason | |
data = response.read() | |
# print json.loads(data) | |
conn.close() | |
ret = json.loads(data) | |
if ret.get("status", {}).get("code") == "1": | |
return ret | |
else: | |
raise Exception(ret) | |
__call__ = request | |
class DomainList(ApiCn): | |
pass | |
class UserInfo(ApiCn): | |
pass | |
class UserDetail(ApiCn): | |
pass | |
class UserToken(ApiCn): | |
pass | |
class RecordList(ApiCn): | |
def __init__(self, domain_id, **kw): | |
kw.update(dict(domain_id=domain_id)) | |
ApiCn.__init__(self, **kw) | |
def cn_find_domain_id(domain): | |
print "China: Find %s id" % domain | |
api = DomainList(**cn_login_params) | |
for i in api().get("domains", []): | |
if i.get("name") == domain: | |
return i.get("id") | |
def cn_get_records(domain): | |
print "China: Get %s records" % domain | |
domain_id = cn_find_domain_id(domain) | |
if domain_id: | |
api = RecordList(domain_id=domain_id, **cn_login_params) | |
return api().get("records") | |
class ApiInt: | |
def __init__(self, cookies=None, **kw): | |
self.base_url = "www.dnspod.com" | |
self.cookies = cookies | |
self.params = dict() | |
self.params.update(kw) | |
self.method = None | |
self.path = None | |
def request(self, **kw): | |
self.params.update(kw) | |
conn = httplib.HTTPSConnection(self.base_url) | |
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/json"} | |
headers.update(self.cookies or {}) | |
conn.request(self.method, self.path, urllib.urlencode(self.params), headers) | |
response = conn.getresponse() | |
print response.status, response.reason | |
data = response.read() | |
# print json.loads(data) | |
conn.close() | |
ret = json.loads(data) | |
if response.status == 200: | |
return ret | |
else: | |
raise Exception(ret) | |
def __getattr__(self, attr): | |
if attr.upper() in ["GET", "POST", "PUT", "DELETE"]: | |
self.method = attr.upper() | |
return self.request | |
else: | |
raise AttributeError, attr | |
def __call__(self): | |
return self.GET() | |
class ApiDomain(ApiInt): | |
def __init__(self, domain, *a, **kw): | |
ApiInt.__init__(self, *a, **kw) | |
self.path = "/api/domain/" + domain | |
def int_get_login_cookies(): | |
"""DNSPod International do not have login api yet, so POST /account/login get cookie """ | |
print "International: Login to %s" % int_login_params.get("email") | |
params = int_login_params.copy() | |
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/html"} | |
conn = httplib.HTTPSConnection("www.dnspod.com") | |
conn.request("POST", "/account/login", urllib.urlencode(params), headers) | |
response = conn.getresponse() | |
print response.status, response.reason | |
cookies = response.getheader("set-cookie") | |
conn.close() | |
if cookies: | |
return dict(Cookie=cookies.split(";", 1)[0]) | |
def int_add_domain(domain, cookies=None): | |
"""DNSPod International do not have add domain api yet, so POST /dashboard """ | |
print "International: Add Domain %s" % domain | |
if not cookies: | |
cookies = int_get_login_cookies() | |
params = dict(domain=domain) | |
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/html"} | |
headers.update(cookies) | |
conn = httplib.HTTPSConnection("www.dnspod.com") | |
conn.request("POST", "/dashboard", urllib.urlencode(params), headers) | |
response = conn.getresponse() | |
print response.status, response.reason | |
data = response.read() | |
# print data | |
conn.close() | |
return response.status == 303 | |
def int_get_records(domain, cookies=None): | |
print "International: Get %s records" % domain | |
if not cookies: | |
cookies = int_get_login_cookies() | |
api = ApiDomain(domain, cookies) | |
return api() | |
def int_add_record(domain, cookies=None, **record): | |
print "International: Add record to %s" % domain | |
if not cookies: | |
cookies = int_get_login_cookies() | |
api = ApiDomain(domain, cookies) | |
return api.PUT(**record) | |
if __name__ == '__main__': | |
import sys | |
domains = sys.argv[1:] | |
if not domains: | |
print __doc__ % sys.argv[0] | |
raise SystemExit(1) | |
cookies = int_get_login_cookies() | |
for domain in domains: | |
records = cn_get_records(domain) | |
try: | |
int_get_records(domain, cookies) | |
except: | |
int_add_domain(domain, cookies) | |
for i in records: | |
if i.get('line') != u"默认": | |
print >> sys.stderr, i, u"%s != 默认, skip!" % i.get("line") | |
elif i.get("enabled") != "1": | |
print >> sys.stderr, i, "record not enabled, skip!" | |
else: | |
record = dict( | |
sub_domain = i.get('name'), | |
area = "0", # just can import default area | |
record_type = i.get('type'), | |
value = i.get('value'), | |
ttl = i.get('ttl'), | |
mx = i.get('mx') | |
) | |
print "\n".join([ "%s:\t%s"% (k,v) for k,v in record.iteritems()]) | |
ret = int_add_record(domain, cookies, **record) | |
print ret | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment