-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.py
executable file
·203 lines (174 loc) · 6.28 KB
/
classes.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import configparser
import requests
import json
from math import ceil
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class YandexPdd(metaclass=Singleton):
def __init__(self):
self.errors = []
self.domains = []
config = configparser.RawConfigParser()
config.read('config')
try:
self.pddToken = config.get('Yandex', 'pdd-token')
self.host = config.get('Yandex', 'host')
self.itemsPerList = 10
except configparser.Error as e:
self.errors.append(e)
return None
def getExternalIP(self):
try:
request = requests.get('https://api.ipify.org?format=json')
except requests.exceptions.RequestException as e:
self.errors.append(e)
return False
response = request.json()
return response['ip']
def searchDnsRecord(self, targetDomain, targetSubdomain):
for i in range(len(self.domains)):
# Ищем нужный домен
domain = self.domains[i]
if domain.name == targetDomain:
for k in range( len(domain.dnsRecords) ):
# Ищем поддомен
record = domain.dnsRecords[k]
if record.subdomain == targetSubdomain:
return record
break
break
return False
def getDomains(self):
domainList = []
# Получаем первые 10 доменов
result = self.getDomainList(1)
if not result:
return False
domainList.extend(result['domains'])
# Теперь мы знаем сколько всего страниц с доменами надо получить
overallDomainCount = int( result['total'] )
overallPages = ceil( overallDomainCount / self.itemsPerList )
if overallPages > 1:
for i in range(2, overallPages + 1):
result = self.getDomainList(i)
if not result:
return False
domainList.extend(result['domains'])
# Можем формировать список из моделей
for i in range(len(domainList)):
domainDesc = domainList[i]
self.domains.append( Domain( domainDesc['name'], domainDesc['status']) )
return self.domains
def getDomainList(self, page):
params = {
'page' : page,
'on_page' : self.itemsPerList
}
result = self.makeRequest('/api2/admin/domain/domains', params)
if not result:
return False
if result['success'] == 'ok':
return result
else:
self.errors.append({'getDomainList': result['error']})
return False
def getDNSRecords(self, name):
params = {
'domain' : name,
}
result = self.makeRequest('/api2/admin/dns/list', params)
if not result:
return False
if result['success'] == 'ok':
return result
else:
self.errors.append({'getDNSRecords': "{} – {}".format(result['domain'],result['error'] )})
return False
def editDNSRecord(self, params):
result = self.postRequest('/api2/admin/dns/edit', params)
if not result:
return False
if result['success'] == 'ok':
return result
else:
self.errors.append({'editDNSRecord': "{} – {}".format(result['domain'],result['error'] )})
return False
def URL(self, uri):
return "{}{}".format(self.host, uri)
def makeRequest(self, uri, params=None):
headers = {
'PddToken' : self.pddToken
}
try:
request = requests.get(self.URL(uri), params=params, headers=headers)
except requests.exceptions.RequestException as e:
self.errors.append(e)
return False
return request.json()
def postRequest(self, uri, params=None):
headers = {
'PddToken' : self.pddToken
}
try:
request = requests.post(self.URL(uri), params=params, headers=headers)
except requests.exceptions.RequestException as e:
self.errors.append(e)
return False
return request.json()
class Domain():
def __init__( self, name, status ):
self.name = name
self.status = status
self.dnsRecords = []
# Тут же получаем DNS записи
self.getDNSRecords()
def __repr__(self):
return self.name
def getDNSRecords(self):
app = YandexPdd()
dnsRecordsList = app.getDNSRecords(self.name)
for i in range(len(dnsRecordsList['records'])):
DR = dnsRecordsList['records'][i]
self.dnsRecords.append( DNSRecord(
DR['record_id'],
DR['type'],
DR['domain'],
DR['fqdn'],
DR['ttl'],
DR['subdomain'],
DR['content'],
DR['priority']
))
return self.dnsRecords
class DNSRecord():
def __init__(self, record_id, type, domain, fqdn, ttl, subdomain, content, priority ):
self.record_id = record_id
self.type = type
self.domain = domain
self.fqdn = fqdn
self.ttl = ttl
self.subdomain = subdomain
self.content = content
self.priority = priority
def __repr__(self):
return "{}: {} – {}".format(self.type, self.fqdn, self.content)
def save(self):
app = YandexPdd()
params = {
'domain' : self.domain,
'record_id' : self.record_id,
'content' : self.content,
'priority' : self.priority,
'subdomain' : self.subdomain,
'ttl' : self.ttl
}
result = app.editDNSRecord(params)
if result['success'] == 'ok':
return True
else:
self.errors.append({'editDNSRecord': "{} – {}".format(result['domain'],result['error'] )})
return False