44import scrtsxx
55import requests
66import socket
7+ import sys
78from contextlib import closing
89from timeit import default_timer as timer
10+ from urllib .parse import urlparse
11+ import concurrent .futures
912
10- VERSION = 2.0
13+ VERSION = 20250104.0323
1114APIURL = 'https://api.sentinel.mathnodes.com'
1215
1316class UpdateNodeUptime ():
@@ -44,7 +47,6 @@ def get_remote_url_of_node(self, db, NodeData):
4447 #print(nodes_without_remote_url)
4548 for n in NodeData :
4649 address = n ['node_address' ]
47- endpoint = APIURL + '/sentinel/nodes/' + address
4850
4951 # Check if the node already has a remote_url in the table
5052 #if any(node['node_address'] == address for node in nodes_without_remote_url):
@@ -54,20 +56,21 @@ def get_remote_url_of_node(self, db, NodeData):
5456 query = f"SELECT remote_url FROM node_uptime WHERE node_address = '{ address } ';"
5557 c .execute (query )
5658 result = c .fetchone ()
57- print (result ['remote_url' ])
59+ # print(result['remote_url'])
5860 if not result ['remote_url' ]:
5961
60- endpoint = APIURL + '/nodes/' + address
62+ endpoint = APIURL + '/sentinel/ nodes/' + address
6163 remote_url = result ['remote_url' ]
62- print (f"Getting remote_url of: { address } " , end = ":" )
64+ print (f"Getting remote_url of: { address } " )
65+ sys .stdout .flush ()
6366
6467 try :
6568 r = requests .get (endpoint )
6669 remote_url = r .json ()['node' ]['remote_url' ]
6770 except Exception as e :
6871 print (str (e ))
6972 continue
70- print (f"{ remote_url } " )
73+ # print(f"{remote_url}")
7174 else :
7275 remote_url = result ['remote_url' ]
7376
@@ -78,7 +81,7 @@ def get_remote_url_of_node(self, db, NodeData):
7881
7982 return NodeRemoteURL
8083
81-
84+ '''
8285 def check_uptime(self, NodeRemoteURLs):
8386 k=0
8487
@@ -87,20 +90,83 @@ def check_uptime(self, NodeRemoteURLs):
8790
8891 for n in NodeRemoteURLs['address']:
8992 url = NodeRemoteURLs['url'][k]
90-
91- hp = url . split ( '//' )[ - 1 ]
92- host , port = hp .split (":" )
93+ parsed_url = urlparse(url)
94+ netloc = parsed_url.netloc
95+ # host, port = urlparse(url).netloc .split(":")
9396 #print(f"host: {host}, port: {port}")
9497 #print("Checking if up: ", end='')
9598
96- up = self .check_socket (host , int (port .replace ('/' ,'' )))
99+ if '[' in netloc and ']' in netloc:
100+ host = netloc.split(']', 1)[0][1:]
101+
102+ if ':' in netloc.split(']', 1)[1]:
103+ port = int(netloc.split(']', 1)[1].split(':', 1)[1])
104+ else:
105+ port = None
106+ else:
107+ parts = netloc.split(':')
108+ if len(parts) > 2:
109+ raise ValueError("Invalid URL format")
110+ host = parts[0]
111+ if len(parts) == 2:
112+ port = int(parts[1])
113+ else:
114+ port = None
115+
116+
117+ up = self.check_socket(host, int(port))
97118 #print(up)
98119 NodeUptimeBoolean['address'].append(n)
99120 NodeUptimeBoolean['up'].append(up)
100121 k += 1
101122
102123 return NodeUptimeBoolean
124+ '''
125+
126+ def check_uptime (self , NodeRemoteURLs ):
127+ k = 0
128+ NodeUptimeBoolean = {'address' : [], 'up' : []}
129+
130+ def check_uptime_for_node (n , url ):
131+ parsed_url = urlparse (url )
132+ netloc = parsed_url .netloc
133+
134+ if '[' in netloc and ']' in netloc :
135+ host = netloc .split (']' , 1 )[0 ][1 :]
136+ if ':' in netloc .split (']' , 1 )[1 ]:
137+ port = int (netloc .split (']' , 1 )[1 ].split (':' , 1 )[1 ])
138+ else :
139+ port = None
140+ else :
141+ parts = netloc .split (':' )
142+ if len (parts ) > 2 :
143+ raise ValueError ("Invalid URL format" )
144+ host = parts [0 ]
145+ if len (parts ) == 2 :
146+ port = int (parts [1 ])
147+ else :
148+ port = None
149+
150+ up = self .check_socket (host , int (port ) if port else None )
151+ result = (n , up )
152+ print (result )
153+ return result
103154
155+ with concurrent .futures .ThreadPoolExecutor (max_workers = 30 ) as executor :
156+ futures = []
157+
158+ for n in NodeRemoteURLs ['address' ]:
159+ url = NodeRemoteURLs ['url' ][k ]
160+ futures .append (executor .submit (check_uptime_for_node , n , url ))
161+ k += 1
162+
163+ for future in concurrent .futures .as_completed (futures ):
164+ address , up = future .result ()
165+ NodeUptimeBoolean ['address' ].append (address )
166+ NodeUptimeBoolean ['up' ].append (up )
167+
168+ return NodeUptimeBoolean
169+
104170 def check_socket (self , host , port ):
105171 with closing (socket .socket (socket .AF_INET , socket .SOCK_STREAM )) as sock :
106172 sock .settimeout (3 )
0 commit comments