# -*- coding: utf-8 -*-
'''
Created on 2015-06-19

@author: minqiang.feng
@email: wnfmq@126.com

'''
import os,sys,time
import json
import dnspod.api
from dnspod.api.rest import *
import argparse
'''
这边可以设置一个默认的accessKeyId和accessKeySecret,当然也可以不设置
注意:默认的只需要设置一次就可以了
'''
dnspod.setDefaultAppInfo("username", "passwd")

'''
a = dnspod.api.domain20150619DomainList()
b = dnspod.api.user20150619UserDetail()
f= a.getResponse()
print b.getResponse()
try:
    print("begin");
    f= a.getResponse()
    print(f)
except Exception,e:
    print(e)
'''
class Domain(object):
    def __init__(self,domain=''):
	self.domain = domain
	self.domain_id = None

    def getidbydomain(self):
	buf=self.DomainInfo()
	self.domain_id = buf["domain"]['id']
	return self.domain_id

    def setdomain(self,domain):
	self.domain = domain
    def DomainCreate(self):
	a = domain20150619DomainCreate()
	a.domain = self.domain
	try:
	    f = a.getResponse()
	    return f
	except Exception,e:
	    print e
    def DomainInfo(self):
        a = domain20150619DomainInfo()
        a.domain = self.domain
        try:
            f = a.getResponse()
            return f
        except Exception,e:
            print e
    def DomainList(self):
        a = domain20150619DomainList()
        a.domain = self.domain
        try:
            f = a.getResponse()
            return f
        except Exception,e:
            print e
    def DomainLog(self):
        pass
    def DomainRemove(self):
        a = domain20150619DomainRemove()
        a.domain = self.domain
        try:
            f = a.getResponse()
            return f
        except Exception,e:
            print e
  
    def DomainSearchenginepush(self):
        pass
    def DomainshareCreate(self):
        pass
    def DomainshareList(self):
        pass
    def DomainStatus(self):
        pass

class Record(Domain):
    def __init__(self,domain):
	Domain.__init__(self,domain)
	self.sub_domain = None
	self.record_type = None
	self.record_id = None
	self.recordid = []
	#self.domain_id = None
	self.domain_id = self.getidbydomain()

    def RecordCreate(self,sub_domain,record_type,value,record_line="默认",mx=5,ttl=600,status="enable"):
        a = record20150619RecordCreate()
        a.domain_id = self.domain_id
	if sub_domain == '':
	    a.sub_domain = self.sub_domain
	else:
	    a.sub_domain = sub_domain
	a.record_type = record_type
	a.value = value
	a.record_line = record_line
	a.mx = mx
	a.ttl = ttl
	a.status = status
        try:
            f = a.getResponse()
            return f
        except Exception,e:
            print e
    
    def SetRecord(self,RecordName):
	self.sub_domain = RecordName

    '''判断记录是否存在'''
    def Istrue(self):
	self.GetRecordID()
	if len(self.recordid) >0:
	    return True
	else:
	    return False
    def Length(self):
	self.GetRecordID()
	return len(self.recordid)

    def RecordNum(self):
	self.GetRecordID()
	return len(self.recordid)
	
    def GetRecordID(self,sub_domain=''):
	buf=self.RecordList();
	self.recordid = []
	if sub_domain == '':
	    sub_domain = self.sub_domain
	for i in buf["records"]:
	    if sub_domain == i["name"]:
		self.recordid.append(i["id"])
    
    def RecordInfo(self):
	self.GetRecordID(self.sub_domain)
        a = record20150619RecordInfo()
        a.domain_id = self.domain_id
	tmp = []
	for i in self.recordid:
	    a.record_id = i
	    try:
		f = a.getResponse()
		tmp.append(f)
	    except Exception,e:
		print e
	return tmp

    def RecordList(self):
        a = record20150619RecordList()
        a.domain_id = self.domain_id
        try:
            f = a.getResponse()
            return f
        except Exception,e:
            print e

    def RecordModify(self,recordid,sub_domain,record_type,value,record_line="默认",mx=5,ttl=600,status="enable"):
        a = record20150619RecordModify()
        a.domain_id = self.domain_id
	a.record_id = recordid
        a.sub_domain = sub_domain
        a.record_type = record_type
        a.value = value
        a.record_line = record_line
        a.mx = mx
        a.ttl = ttl
        a.status = status
        try:
            f = a.getResponse()
            return f
        except Exception,e:
            print e
    def AutoRecordModify(self,sub_domain,record_type,value,record_line="默认",mx=5,ttl=600,status="enable"):
	self.GetRecordID(sub_domain)
	if len(self.recordid) >= 2:
	    return "More than 1 Records"
	if len(self.recordid) ==0:
	    return "Less than 1 Records"
        a = record20150619RecordModify()
        a.domain_id = self.domain_id
	a.record_id = self.recordid[0]
        a.sub_domain = sub_domain
        a.record_type = record_type
        a.value = value
        a.record_line = record_line
        a.mx = mx
        a.ttl = ttl
        a.status = status
        try:
            f = a.getResponse()
            return f
        except Exception,e:
            print e
    def RecordRemark(self):
        pass
    def RecordRemove(self):
        pass
    def RecordStatus(self):
        pass
        
 if __name__ == '__main__':
    Do = Domain()
    Do.setdomain("fengyunzhibo.com")
    print Do.DomainList()


该俩个class是在python的dnspod api的基础上写的,请下载dnspod api后使用,下载地址 http://down.51cto.com/data/2079627