-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparkio_domains.py
181 lines (166 loc) · 6.8 KB
/
parkio_domains.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
#!/usr/env python3
import sys, time, requests, threading
from slackclient import SlackClient
from parkiolib import get_slack_client, xstr, TX_CHANNEL, DOMAIN_JSON_EP
'''
data['conn'], data['tld'], data['domains'], data['new_domains'], data['limit']
'''
def parkio_process(data):
msg = ''
domains = data['domains']
newDomains = data['new_domains']
#check for available domains and delete available domains from dropping domains
for k, v_dict in list(domains.items()):
if k not in newDomains:
msg = msg + 'Domain ' + xstr(k) + ' is now available:\n' + xstr(v_dict['name']) + ', ' + \
'date available: ' + xstr(v_dict['date_available']) + ', ' + \
'date registered: ' + xstr(v_dict['date_registered']) + '\n'
del domains[k]
#check for new domains
for k, v_dict in newDomains.items():
if k not in domains: #new domain
domains[k] = {'name': v_dict['name'], 'date_available': v_dict['date_available'],
'date_registered': v_dict['date_registered'], 'tld': v_dict['tld']}
msg = msg + 'New dropping domain ' + xstr(k) + ':\n' + xstr(v_dict['name']) + ', ' + \
'date available: ' + xstr(v_dict['date_available']) + ', ' + \
'date registered: ' + xstr(v_dict['date_registered']) + '\n'
#print changes to console and send slack message
if not msg:
print('[domain ' + data['tld'] + '] ' + 'nothing changed')
else:
print('[domain ' + data['tld'] + '] ' + msg, end='')
get_slack_client().api_call("chat.postMessage", channel=TX_CHANNEL, text=msg, as_user=True)
sys.stdout.flush() #flush output due to threading
'''
data['conn'], data['tld'], data['domains'], data['new_domains'], data['limit']
'''
def parkio_domain(data):
msg = ''
state = 'e' #'e': error, 'c': continue, 'f': found, 'n': nothing found
try:
reply = data['conn'].get(DOMAIN_JSON_EP + data['tld'] + '.json?limit=' + xstr(data['limit']))
reply.raise_for_status()
except requests.exceptions.HTTPError as err:
msg = xstr(err) + '\n'
except:
msg = 'connection error\n'
else:
replyDomains = reply.json()['domains']
#Check if any dropping domains with tld
if data['tld'] == 'all' or data['tld'] == replyDomains[0].get('tld'):
count = int(xstr(reply.json()['count']))
if count > data['limit']: #Check if more domains available than current limit
state = 'c'
data['limit'] = count
else:
state = 'f'
newDomains = data['new_domains']
#transform response list to a dictionary
for k in replyDomains:
newDomains[k['id']] = {'name': k['name'], 'date_available': k['date_available'],
'date_registered': k['date_registered'], 'tld': k['tld']}
else:
state = 'n'
msg = 'No domain with tld ' + data['tld'] + ' is dropping soon\n'
finally:
if state is 'e': #Some error occured, restart
print('[domain ' + data['tld'] + '] ' + msg, end='')
sys.stdout.flush() #flush output due to threading
# repeat main request in 20 seconds
main_thread(data)
elif state is 'c': parkio_domain(data) #More domains available, re-request with higher limit
elif state is 'f': #Found domains with tld
parkio_process(data)
data['new_domains'] = dict()
data['limit'] = len(data['domains'])
# repeat main request in 20 seconds
main_thread(data)
elif state is 'n':
print('[domain ' + data['tld'] + '] ' + msg, end='')
sys.stdout.flush() #flush output due to threading
get_slack_client().api_call("chat.postMessage", channel=TX_CHANNEL, text=msg, as_user=True)
'''
data['conn'], data['tld'], data['domains'], data['new_domains'], data['limit']
'''
def parkio_start(data):
msg = ''
state = 'e' #'e': error, 'c': continue, 'f': found, 'n': nothing found
try:
reply = data['conn'].get(DOMAIN_JSON_EP + data['tld'] + '.json?limit=' + xstr(data['limit']))
reply.raise_for_status()
except requests.exceptions.HTTPError as err:
msg = xstr(err) + '\n'
except:
msg = 'connection error\n'
else:
replyDomains = reply.json()['domains']
#Check if any dropping domains with tld
if data['tld'] == 'all' or data['tld'] == replyDomains[0].get('tld'):
count = int(xstr(reply.json()['count']))
if count > data['limit']: #Check if more domains available than current limit
state = 'c'
data['limit'] = count
else:
state = 'f'
msg = 'Dropping Domains:\n'
domains = data['domains']
#transform response list to a dictionary
for k in replyDomains:
domains[k['id']] = {'name': k['name'], 'date_available': k['date_available'],
'date_registered': k['date_registered'], 'tld': k['tld']}
msg = msg + xstr(k['id']) + ': ' + xstr(k['name']) + ', ' + 'date available: ' + \
xstr(k['date_available']) + ', ' + 'date registered: ' + xstr(k['date_registered']) + '\n'
else:
state = 'n'
msg = 'No domain with tld ' + data['tld'] + ' is dropping soon\n'
finally:
if state is 'e': #Some error occured, restart
print('[domain ' + data['tld'] + '] ' + msg, end='')
sys.stdout.flush() #flush output due to threading
#restart init request in 20 seconds
init_thread(data)
elif state is 'c': parkio_start(data) #More domains available, re-request with higher limit
elif state is 'f': #Found domains with tld
print('[domain ' + data['tld'] + '] ' + msg, end='')
sys.stdout.flush() #flush output due to threading
data['limit'] = len(data['domains'])
get_slack_client().api_call("chat.postMessage", channel=TX_CHANNEL, text=msg, as_user=True)
#start main request in 20 seconds
main_thread(data)
elif state is 'n': #No domains found with tld
print('[domain ' + data['tld'] + '] ' + msg, end='')
sys.stdout.flush() #flush output due to threading
get_slack_client().api_call("chat.postMessage", channel=TX_CHANNEL, text=msg, as_user=True)
'''
run init request in 20 seconds
'''
def init_thread(data):
print('[domain ' + data['tld'] + ']' + " Thread {} starting.".format(threading.current_thread()))
sys.stdout.flush() #flush output due to threading
threading.Timer(20, parkio_start, [data]).start()
print('[domain ' + data['tld'] + ']' + " Thread {} done.".format(threading.current_thread()))
sys.stdout.flush() #flush output due to threading
'''
run main request in 20 seconds
'''
def main_thread(data):
print('[domain ' + data['tld'] + ']' + " Thread {} starting.".format(threading.current_thread()))
sys.stdout.flush() #flush output due to threading
threading.Timer(20, parkio_domain, [data]).start()
print('[domain ' + data['tld'] + ']' + " Thread {} done.".format(threading.current_thread()))
sys.stdout.flush() #flush output due to threading
if __name__ == "__main__":
if len(sys.argv) == 2:
data = {
'conn': requests.Session(),
'tld': sys.argv[1],
'domains' : dict(),
'new_domains': dict(),
'limit': 1000,
}
parkio_start(data)
else:
msg = 'Wrong number of arguments'
print('[domain] ' + msg)
sys.stdout.flush() #flush output due to threading
get_slack_client().api_call("chat.postMessage", channel=TX_CHANNEL, text=msg, as_user=True)