#!/usr/bin/env python3
#
# 获取域名列表
# curl -X POST https://dnsapi.cn/Domain.List -d 'login_email=api@dnspod.com&login_password=password&format=json'
#
# 获取记录列表
# curl -X POST https://dnsapi.cn/Record.List -d 'login_email=api@dnspod.com&login_password=password&format=json&domain_id=2317346'

import socket
import urllib
import json
import time

__author__ = 'RFS4ever'
version = 0.1

current_ip = None


def ddns(ip):
    ddns_server_url = 'https://dnsapi.cn/Record.Ddns'

    params = dict(
        login_email='email',  # Change to yours
        login_password='password',  # Change to yours
        format='json',
        domain_id='8888',  # Change to yours
        record_id='8888',  # Change to yours
        sub_domain='ddns',  # Change to yours
        record_line='默认',
    )
    headers = {
        "Content-type": "application/x-www-form-urlencoded",
        "Accept": "text/json",
        "User-Agent": "pyDdnsPod/0.1 (beyondrookie#gmail.com)"
    }


def get_public_ip():
    """Create socket connection to get the current public ip address"""

    addr = ('ns1.dnspod.net', 6666)
    sock = socket.create_connection(addr)
    publicIP = sock.recv(16)
    sock.close()
    return publicIP


def main():
    while True:
        ip = get_public_ip()

        time.sleep(60 * 60)  # 1 hour


if __name__ == '__main__':
    main()
#!/usr/bin/env python3
#
# 获取域名列表
# curl -X POST https://dnsapi.cn/Domain.List -d 'login_email=api@dnspod.com&login_password=password&format=json'
#
# 获取记录列表
# curl -X POST https://dnsapi.cn/Record.List -d 'login_email=api@dnspod.com&login_password=password&format=json&domain_id=2317346'

import socket
import urllib.request
import urllib.parse
from urllib.error import URLError
import json
import time
import datetime

__author__ = 'RFS4ever'
__homepage__ = 'https://github.com/RFS4ever/pyDdnsPod'
version = '0.2'

# Set global default timeout in seconds
timeout = 10
socket.setdefaulttimeout(timeout)

# Initial Configuration
current_ip = None
config = {
    'ddns_api_url': 'https://dnsapi.cn/Record.Ddns',
    'headers': {
        "Content-type": "application/x-www-form-urlencoded",
        "Accept": "text/json",
        "User-Agent": u"{0:s}'s pyDdnsPod/{1:s} (beyondrookie#gmail.com)".format(__author__, version)  # Change to yours
    },
    'params': {
        'login_email': 'email',  # Change to yours
        'login_password': 'password',  # Change to yours
        'format': 'json',
        'domain_id': '8888',  # Change to yours
        'record_id': '8888',  # Change to yours
        'sub_domain': 'ddns',  # Change to yours
        'record_line': '默认',
    }
}


def get_public_ip():
    """Create socket connection to get the current public ip address"""

    addr = ('ns1.dnspod.net', 6666)
    sock = socket.create_connection(addr)
    public_ip = sock.recv(16)
    sock.close()
    return public_ip


def ddns(ip):
    """Connect to the API server, change the ddns's ip record to current public ip"""

    url = config['ddns_api_url']
    params = config['params']
    params.update({'value': ip})
    data = urllib.parse.urlencode(params)
    data = data.encode('utf-8')
    headers = config['headers']
    req = urllib.request.Request(url, data, headers)

    try:
        res = urllib.request.urlopen(req)
    except URLError as e:
        if hasattr(e, 'reason'):
            print('We failed to reach a server.')
            print('Reason: ', e.reason)
        elif hasattr(e, 'code'):
            print('The server couldn\'t fulfill the request.')
            print('Error code: ', e.code)
    else:
        # everything is fine
        return_info = res.read()
        return_info = json.loads(return_info)

    if return_info.get('status', {}).get("code") == '1':
        now = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S %a')
        print('[%s] New ddns ip has changed!' % now)
        print(json.dumps(return_info, indent=4))
        return True
    else:
        raise Exception(return_info)


def main():
    global current_ip

    while True:
        try:
            ip = get_public_ip()
            now = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S %a')
            print(u'[{0:s}] Public ip is "{1:s}"'.format(now, ip))
            if current_ip != ip:
                if ddns(ip):
                    current_ip = ip
        except Exception as e:
            print(json.dumps(e, indent=4))
        time.sleep(60 * 60)  # 1 hour (sleep in seconds)


if __name__ == '__main__':
    main()
add basic functions to pyDdnsPod