-
Notifications
You must be signed in to change notification settings - Fork 4
/
DHTNodes_updater.py
99 lines (77 loc) · 2.88 KB
/
DHTNodes_updater.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
#!/usr/bin/env python3
import urllib.request
import urllib.error
import sys
import os
import time
import threading
def spin(stop: threading.Event):
if stop is None:
return
print("|", end="")
while True:
for c in "/-\\|":
if stop.is_set():
return
sys.stdout.write("\b{}".format(c))
sys.stdout.flush()
time.sleep(0.1)
def main():
if len(sys.argv) != 2:
print("Usage: {} /path/to/DHTnodes".format(sys.argv[0]))
return
if os.path.isdir(sys.argv[1]):
print("{} is not a file".format(sys.argv[1]))
return
print("Downloading")
stop = threading.Event()
spinner = threading.Thread(target=spin, args=(stop,))
spinner.start()
try:
request = urllib.request.urlopen("https://wiki.tox.im/Nodes")
raw_page = request.read().decode("utf-8")
except urllib.error:
print("Couldn't download")
stop.set()
return
stop.set()
sys.stdout.write("\b")
sys.stdout.flush()
print("Parsing")
# Get rid of everything around the table
raw_table = raw_page[raw_page.find("<table"):raw_page.find("</table")]
del raw_page
# Get rid of the `<table border="0" cell...>`,
# and the `</table>` is already gone
raw_table = raw_table[raw_table.find("\n"):]
# Get rid of the header
raw_table = raw_table[raw_table.find("<tr "):]
# Separate each section into a list
raw_node_list = []
count = raw_table.count("<tr ")
for i in range(count):
ipv4 = raw_table[raw_table.find("<td>") + 4:raw_table.find("</td>") - 1]
raw_table = raw_table[raw_table.find("</td>\n<td>")+5:]
ipv6 = raw_table[raw_table.find("<td>") + 4:raw_table.find("</td>") - 1]
raw_table = raw_table[raw_table.find("</td>\n<td>")+5:]
port = raw_table[raw_table.find("<td>") + 4:raw_table.find("</td>") - 1]
raw_table = raw_table[raw_table.find("</td>\n<td>")+5:]
key = raw_table[raw_table.find("<td>") + 4:raw_table.find("</td>") - 1]
raw_table = raw_table[raw_table.find("</td>\n<td>")+5:]
name = raw_table[raw_table.find("<td>") + 4:raw_table.find("</td>") - 1]
raw_table = raw_table[raw_table.find("</td>\n<td>")+5:]
location = raw_table[raw_table.find("<td>") + 4:raw_table.find("</td>") - 1]
raw_table = raw_table[raw_table.find("</td>\n<td>")+5:]
status = raw_table[raw_table.find("<td>") + 4:raw_table.find("</td>") - 1]
raw_table = raw_table[raw_table.find("</td></tr>")+5:]
# print(ipv4, ipv6, port, key, name, location, status)
raw_node_list.append("{} {} {}".format(ipv4, port, key))
del raw_table
print("Writing")
# Write
with open(sys.argv[1], "w") as DHTnodes:
for line in raw_node_list:
DHTnodes.write(line + "\n")
print("Success")
if __name__ == "__main__":
main()