import hashlib
import re
import reqto
+import requests
import json
import sqlite3
import sys
try:
res = reqto.get(f"https://{domain}/api/v3/site", headers=api_headers, timeout=(config.get("connection_timeout"), config.get("read_timeout")))
- data = res.json()
+ data = json_from_response(res)
+
# DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code='{res.status_code}',data[]='{type(data)}'")
if not res.ok or res.status_code >= 400:
print("WARNING: Could not reach any JSON API:", domain)
try:
res = reqto.get(f"https://{domain}/api/v1/server/{mode}?start={start}&count=100", headers=headers, timeout=(config.get("connection_timeout"), config.get("read_timeout")))
- data = res.json()
+ data = json_from_response(res)
# DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code='{res.status_code}',data[]='{type(data)}'")
if res.ok and isinstance(data, dict):
# DEBUG: print("DEBUG: Success, data:", len(data))
try:
res = reqto.get(f"https://{domain}{get_peers_url}", headers=api_headers, timeout=(config.get("connection_timeout"), config.get("read_timeout")))
- data = res.json()
+ data = json_from_response(res)
+
# DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code={res.status_code},data[]='{type(data)}'")
if not res.ok or res.status_code >= 400:
# DEBUG: print(f"DEBUG: Was not able to fetch '{get_peers_url}', trying alternative ...")
res = reqto.get(f"https://{domain}/api/v3/site", headers=api_headers, timeout=(config.get("connection_timeout"), config.get("read_timeout")))
- data = json.dumps({})
- if res.text.strip() != "":
- data = res.json()
-
+ data = json_from_response(res)
# DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code={res.status_code},data[]='{type(data)}'")
if not res.ok or res.status_code >= 400:
print("WARNING: Could not reach any JSON API:", domain)
try:
res = reqto.post(f"https://{domain}{path}", data=parameter, headers={**api_headers, **extra_headers}, timeout=(config.get("connection_timeout"), config.get("read_timeout")))
- data = res.json()
+ data = json_from_response(res)
# DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code={res.status_code},data[]='{type(data)}'")
if not res.ok or res.status_code >= 400:
print(f"WARNING: Cannot query JSON API: domain='{domain}',path='{path}',parameter()={len(parameter)},res.status_code='{res.status_code}',data[]='{type(data)}'")
# DEBUG: print("DEBUG: Fetching request:", request)
res = reqto.get(request, headers=api_headers, timeout=(config.get("connection_timeout"), config.get("read_timeout")))
- data = res.json()
+ data = json_from_response(res)
# DEBUG: print(f"DEBUG: res.ok={res.ok},res.status_code={res.status_code},data[]='{type(data)}'")
if res.ok and isinstance(data, dict):
# DEBUG: print("DEBUG: Success:", request)
try:
res = reqto.get(f"https://{domain}/.well-known/nodeinfo", headers=api_headers, timeout=(config.get("connection_timeout"), config.get("read_timeout")))
- data = res.json()
+ data = json_from_response(res)
# DEBUG: print("DEBUG: domain,res.ok,data[]:", domain, res.ok, type(data))
if res.ok and isinstance(data, dict):
nodeinfo = data
# DEBUG: print("DEBUG: Fetching nodeinfo from:", link["href"])
res = reqto.get(link["href"])
- data = res.json()
+ data = json_from_response(res)
# DEBUG: print("DEBUG: href,res.ok,res.status_code:", link["href"], res.ok, res.status_code)
if res.ok and isinstance(data, dict):
# DEBUG: print("DEBUG: Found JSON nodeinfo():", len(data))
# DEBUG: print(f"DEBUG: domain='{domain}' - EXIT!")
return domain
+
+def json_from_response(response: requests.models.Response) -> list:
+ # DEBUG: print(f"DEBUG: response[]={type(response)} - CALLED!")
+ if not isinstance(response, requests.models.Response):
+ raise ValueError(f"Parameter response[]='{type(response)}' is not type of 'Response'")
+
+ data = list()
+ if response.text.strip() != "":
+ # DEBUG: print(f"DEBUG: response.text()={len(response.text)} is not empty, invoking response.json() ...")
+ try:
+ data = response.json()
+ except json.JSONDecodeError:
+ pass
+
+ # DEBUG: print(f"DEBUG: data[]={type(data)} - EXIT!")
+ return data