-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
87 lines (74 loc) · 2.33 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import socket
import os
import urllib.parse
import urllib.request
import json
def getMyIp():
domain = "tonio-peng.tpddns.cn"
myaddr = socket.getaddrinfo(domain, 'http')
myip = myaddr[0][4][0]
return myip
def initReq(url, values):
'''
simple request data from url with values
'''
data = urllib.parse.urlencode(values).encode()
req = urllib.request.Request(url, data)
try:
res = urllib.request.urlopen(req)
except:
raise
return res
DNSPOD_TOKEN = ''
if os.environ.get('DNSPOD_TOKEN', '') != '':
DNSPOD_TOKEN = os.environ['DNSPOD_TOKEN']
update_domain = "tonio-peng.top"
DomainListUrl = 'https://dnsapi.cn/Domain.List'
RecordListUrl = 'https://dnsapi.cn/Record.List'
RecordDdnsUrl = 'https://dnsapi.cn/Record.Ddns'
loginInfo = {
'login_token':DNSPOD_TOKEN,
'format': 'json'
}
res = initReq(url=DomainListUrl, values=loginInfo)
i_list=json.loads(res.read().decode())['domains']
domainIDs = {i['name']:i['id'] for i in i_list}
res = initReq(url=RecordListUrl, values={
**loginInfo, **{'domain_id': domainIDs.get(update_domain)}})
i_list = json.loads(res.read().decode())['records']
subdomainInfo = {((i['name'], i['line']) if i['type'] == 'A' else ('invalid', 'invalid')):
{'IP': i['value'], 'ID': i['id']}
for i in i_list}
updated_ip = getMyIp()
http_data =[{
**loginInfo,
**{
'domain_id':domainIDs.get(update_domain) ,
'record_id': subdomainInfo[('www', '默认')]['ID'],
'record_line': '默认',
'sub_domain': 'www',
'value': updated_ip
}
},
{
**loginInfo,
**{
'domain_id':domainIDs.get(update_domain) ,
'record_id': subdomainInfo[('@', '默认')]['ID'],
'record_line': '默认',
'sub_domain': '@',
'value': updated_ip
}
}
]
for item in http_data:
try:
initReq(url=RecordDdnsUrl, values=item)
except:
pass
res = initReq(url=RecordListUrl, values={
**loginInfo, **{'domain_id': domainIDs.get(update_domain)}})
i_list = json.loads(res.read().decode())['records']
subdomainInfo = {((i['name'], i['line']) if i['type'] == 'A' else ('invalid', 'invalid')):
{'IP': i['value'], 'ID': i['id']}
for i in i_list}