]> git.mxchange.org Git - fba.git/blobdiff - fba/networks/peertube.py
Continued:
[fba.git] / fba / networks / peertube.py
index da84bafac765f986fb2289c39490cb84f2c3efd7..caf38653dc61e2edb4b0f20f0ff2705861086ac7 100644 (file)
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
-from fba import config
-from fba import instances
-from fba import network
+import logging
+
+from fba.helpers import blacklist
+from fba.helpers import config
+from fba.helpers import domain as domain_helper
+
+from fba.http import csrf
+from fba.http import network
+
+from fba.models import instances
+
+logging.basicConfig(level=logging.INFO)
+logger = logging.getLogger(__name__)
 
 def fetch_peers(domain: str) -> list:
-    # DEBUG: print(f"DEBUG: domain({len(domain)})={domain},software='peertube' - CALLED!")
-    if not isinstance(domain, str):
-        raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
-    elif domain == "":
-        raise ValueError("Parameter 'domain' is empty")
-
-    # DEBUG: print(f"DEBUG: domain='{domain}' is a PeerTube, fetching JSON ...")
-    peers = list()
-    start = 0
+    logger.debug("domain='%s' - CALLED!", domain)
+    domain_helper.raise_on(domain)
+
+    if blacklist.is_blacklisted(domain):
+        raise Exception(f"domain='{domain}' is blacklisted but function is invoked.")
+    elif not instances.is_registered(domain):
+        raise Exception(f"domain='{domain}' is not registered but function is invoked.")
+
+    # Init variables
+    peers   = list()
+    headers = tuple()
+    start   = 0
+
+    try:
+        logger.debug("Checking CSRF for domain='%s'", domain)
+        headers = csrf.determine(domain, dict())
+    except network.exceptions as exception:
+        logger.warning("Exception '%s' during checking CSRF (fetch_peers,%s)", type(exception), __name__)
+        instances.set_last_error(domain, exception)
+
+        logger.debug("Returning empty list ... - EXIT!")
+        return list()
+
     for mode in ["followers", "following"]:
-        # DEBUG: print(f"DEBUG: domain='{domain}',mode='{mode}'")
+        logger.debug("domain='%s',mode='%s'", domain, mode)
         while True:
-            try:
-                response = network.fetch_response(
-                    domain,
-                    "/api/v1/server/{mode}?start={start}&count=100",
-                    network.api_headers,
-                    (config.get("connection_timeout"), config.get("read_timeout"))
-                )
-
-                data = network.json_from_response(response)
-                # DEBUG: print(f"DEBUG: response.ok={response.ok},response.status_code='{response.status_code}',data[]='{type(data)}'")
-                if response.ok and isinstance(data, dict):
-                    # DEBUG: print("DEBUG: Success, data:", len(data))
-                    if "data" in data:
-                        # DEBUG: print(f"DEBUG: Found {len(data['data'])} record(s).")
-                        for record in data["data"]:
-                            # DEBUG: print(f"DEBUG: record()={len(record)}")
-                            if mode in record and "host" in record[mode]:
-                                # DEBUG: print(f"DEBUG: Found host={record[mode]['host']}, adding ...")
-                                peers.append(record[mode]["host"])
-                            else:
-                                print(f"WARNING: record from '{domain}' has no '{mode}' or 'host' record: {record}")
-
-                        if len(data["data"]) < 100:
-                            # DEBUG: print("DEBUG: Reached end of JSON response:", domain)
-                            break
-
-                    # Continue with next row
-                    start = start + 100
-
-            except BaseException as exception:
-                print(f"WARNING: Exception during fetching JSON: domain='{domain}',exception[{type(exception)}]:'{str(exception)}'")
-
-    # DEBUG: print(f"DEBUG: Adding '{len(peers)}' for domain='{domain}'")
-    instances.set_data("total_peers", domain, len(peers))
-
-    # DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
-    instances.update_last_instance_fetch(domain)
-
-    # DEBUG: print("DEBUG: Returning peers[]:", type(peers))
+            data = network.get_json_api(
+                domain,
+                f"/api/v1/server/{mode}?start={start}&count=100",
+                headers,
+                (config.get("connection_timeout"), config.get("read_timeout"))
+            )
+
+            logger.debug("data[]='%s'", type(data))
+            if "error_message" in data:
+                logger.warning("domain='%s' causes error during API query: '%s' - SKIPPED!", domain, data["error_message"])
+                break
+            elif "data" not in data["json"]:
+                logger.warning("domain='%s' has no 'data' element returned - SKIPPED!", domain)
+                break
+            else:
+                logger.debug("Success, data[json]()=%d", len(data["json"]))
+                instances.set_success(domain)
+
+                rows  = data["json"]["data"]
+
+                logger.debug("Found %d record(s).", len(rows))
+                for record in rows:
+                    logger.debug("record()=%d", len(record))
+                    for mode2 in ["follower", "following"]:
+                        logger.debug("mode=%s,mode2='%s'", mode, mode2)
+                        if mode2 not in record:
+                            logger.debug("Array record does not contain element mode2='%s' - SKIPPED", mode2)
+                            continue
+                        elif "host" not in record[mode2]:
+                            logger.debug("record[%s] does not contain element 'host' - SKIPPED", mode2)
+                            continue
+                        elif record[mode2]["host"] == domain:
+                            logger.debug("record[%s]='%s' matches domain='%s' - SKIPPED!", mode2, record[mode2]["host"], domain)
+                            continue
+                        elif not domain_helper.is_wanted(record[mode2]["host"]):
+                            logger.debug("record[%s][host]='%s' is not wanted - SKIPPED!", mode2, record[mode2]["host"])
+                            continue
+
+                        logger.debug("Appending mode2='%s',host='%s' ...", mode2, record[mode2]["host"])
+                        peers.append(record[mode2]["host"])
+
+                if len(rows) < 100:
+                    logger.debug("Reached end of JSON response, domain='%s'", domain)
+                    break
+
+                # Continue with next row
+                start = start + 100
+
+    logger.debug("peers[%s]()=%d - EXIT!", type(peers), len(peers))
     return peers