-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateTLDs.py
76 lines (63 loc) · 2.35 KB
/
updateTLDs.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
"""
This should update the TLDs list every few days if you plug this into
your main application. There may be some variance in time.time() and
system local timezones (unsure, didn't test). Also maybe some issues
with st_mtime depending on your operating system.
"""
#todo add capability to put in a word cloud or generate a word cloud
#plug the word cloud into gpt
#query some DNS APIs to seek available domains
#
import time
import urllib2
import os.path
words = input("Give me some words to search...")
#def search_names(names):
def update_tlds(filename, url):
"""
Downloads a list of TLDs from 'url' and stores the TLDs in list
'new_tlds'.
Compares 'new_tlds' to a local file at 'filename', and creates a
file there if it does not exist. (If you get write errors, it may
be due to your file permissions.) If there are 'new_tlds' not in
'old_tlds' it will rewrite the file with the current 'new_tlds'.
:param filename: the location of the local file
:param url: the location of the remote file
:return: List of TLDs that were added to the file.
"""
try:
old_tlds = []
new_tlds = [tld.strip('\n') for tld in urllib2.urlopen(url)
if '#' not in tld]
if os.path.isfile(filename):
old_tlds = [line.strip('\n') for line in open(filename)
if '#' not in line]
if old_tlds == new_tlds:
return []
else:
with open(filename, 'w') as f:
for tld in new_tlds:
f.write(tld + '\n')
return sorted(set(new_tlds) - set(old_tlds))
except Exception as e:
return e
if __name__ == '__main__':
day = 86400 # one day = 86,400 seconds
run_every = day * 3
filename = 'TLDs.txt'
url = 'http://data.iana.org/TLD/tlds-alpha-by-domain.txt'
delta_mtime = 0
if os.path.isfile(filename):
delta_mtime = time.time() - os.stat(filename).st_mtime
if delta_mtime < run_every:
print 'TLDs last updated about %s days ago.' % (
round(delta_mtime / day, 2))
tlds = update_tlds(filename, url)
if isinstance(tlds, Exception):
print 'Error: %r' % tlds
exit(1)
elif not tlds:
print 'Success, list is current.'
else:
print 'Success, list has been updated.'
print 'TLDs Added: %s' % ', '.join(tlds)