# NOISY-DEBUG: print("DEBUG: Calculating hash for domain:", domain)
return sha256(domain.encode("utf-8")).hexdigest()
+def update_last_error(domain: str, res: any):
+ # NOISY-DEBUG: print("DEBUG: domain,res.status_code", domain, res.status_code)
+
+ try:
+ c.execute("UPDATE instances SET last_status_code = ? WHERE domain = ?", [
+ res.status_code,
+ domain
+ ])
+
+ except:
+ print("ERROR: failed SQL query:", domain)
+ sys.exit(255)
+
def update_last_nodeinfo(domain: str):
# NOISY-DEBUG: print("DEBUG: Updating last_nodeinfo for domain:", domain)
print("ERROR: failed SQL query:", domain)
sys.exit(255)
-def get_peers(domain: str) -> str:
+def get_peers(domain: str) -> list:
# NOISY-DEBUG: print("DEBUG: Getting peers for domain:", domain)
peers = None
try:
res = reqto.get(f"https://{domain}/api/v1/instance/peers", headers=headers, timeout=5)
- peers = res.json()
+
+ if not res.ok or res.status_code >= 400:
+ print("WARNING: Cannot fetch peers:", domain)
+ update_last_error(domain, res)
+ else:
+ # NOISY-DEBUG: print("DEBUG: Querying API was successful:", domain, len(res.json()))
+ peers = res.json()
+
except:
- print("WARNING: Cannot fetch peers:", domain)
+ print("WARNING: Some error during get():", domain)
- if peers is not None:
- update_last_nodeinfo(domain)
+ update_last_nodeinfo(domain)
# NOISY-DEBUG: print("DEBUG: Returning peers[]:", type(peers))
return peers
def post_json_api(domain: str, path: str, data: str) -> list:
- # NOISY-DEBUG: print("DEBUG: Sending POST to domain,path,data:", domain, path, data)
- res = reqto.post(f"https://{domain}{path}", data=data, headers=headers, timeout=5)
+ try:
+ # NOISY-DEBUG: print("DEBUG: Sending POST to domain,path,data:", domain, path, data)
+ res = reqto.post(f"https://{domain}{path}", data=data, headers=headers, timeout=5)
+
+ if not res.ok or res.status_code >= 400:
+ print("WARNING: Cannot query JSON API:", domain, path, data, res.status_code)
+ update_last_error(domain, res)
+ raise
- if not res.ok:
- print("WARNING: Cannot query JSON API:", domain, path, data, res.status_code)
- raise
- else:
update_last_nodeinfo(domain)
+ json = res.json()
+ except:
+ print("WARNING: Some error during post():", domain, path, data)
- doc = res.json()
- # NOISY-DEBUG: print("DEBUG: Returning doc():", len(doc))
- return doc
+ # NOISY-DEBUG: print("DEBUG: Returning json():", len(json))
+ return json
def fetch_nodeinfo(domain: str) -> list:
# NOISY-DEBUG: print("DEBUG: Fetching nodeinfo from domain:", domain)
json = None
for request in requests:
- # NOISY-DEBUG: print("DEBUG: Fetching request:", request)
-
try:
+ # NOISY-DEBUG: print("DEBUG: Fetching request:", request)
res = reqto.get(request, headers=headers, timeout=5)
# NOISY-DEBUG: print("DEBUG: res.ok,res.json[]:", res.ok, type(res.json()))
if res.ok and res.json() is not None:
# NOISY-DEBUG: print("DEBUG: Success:", request)
json = res.json()
+ break
+ elif not res.ok or res.status_code >= 400:
+ # NOISY-DEBUG: print("DEBUG: Failed fetching nodeinfo from domain:", domain)
+ update_last_error(domain, res)
+ continue
except:
- # NOISY-DEBUG: print("DEBUG: Failed fetching nodeinfo from domain:", domain)
- continue
-
- if json is not None:
- break
+ print("WARNING: Some error during get():", request)
if json is None:
print("WARNING: Failed fetching nodeinfo from domain:", domain)
print("--- Adding new instance:", domain)
try:
c.execute(
- "INSERT INTO instances SELECT ?, ?, ?, NULL",
+ "INSERT INTO instances (domain,hash,software) VALUES (?, ?, ?)",
(
domain,
get_hash(domain),