]> git.mxchange.org Git - fba.git/blobdiff - fba/commands.py
Continued/WIP:
[fba.git] / fba / commands.py
index 477227602169523c8ff1f077abbb77da08668d05..8da03313c5b44c637a1e94ecfff87a51d5767203 100644 (file)
@@ -373,7 +373,7 @@ def fetch_blocks(args: argparse.Namespace) -> int:
             elif block["blocked"].endswith(".onion"):
                 logger.debug("blocked='%s' is a TOR .onion domain - SKIPPED", block["blocked"])
                 continue
-            elif block["blocked"].endswith(".i2p") and config.get("allow_i2p_domain") == "true":
+            elif block["blocked"].endswith(".i2p") and not config.get("allow_i2p_domain"):
                 logger.debug("blocked='%s' is an I2P .onion domain - SKIPPED", block["blocked"])
                 continue
             elif block["blocked"].endswith(".arpa"):
@@ -485,7 +485,7 @@ def fetch_observer(args: argparse.Namespace) -> int:
     types = list()
     if args.software is None:
         logger.info("Fetching software list ...")
-        raw = utils.fetch_url(
+        raw = network.fetch_url(
             f"https://{source_domain}",
             network.web_headers,
             (config.get("connection_timeout"), config.get("read_timeout"))
@@ -609,7 +609,7 @@ def fetch_todon_wiki(args: argparse.Namespace) -> int:
     }
 
     logger.debug("Fetching domainblocks from source_domain='%s'", source_domain)
-    raw = utils.fetch_url(
+    raw = network.fetch_url(
         f"https://{source_domain}/todon/domainblocks",
         network.web_headers,
         (config.get("connection_timeout"), config.get("read_timeout"))
@@ -724,7 +724,7 @@ def fetch_cs(args: argparse.Namespace):
         sources.update(source_domain)
 
     logger.info("Fetching federation.md from source_domain='%s' ...", source_domain)
-    raw = utils.fetch_url(
+    raw = network.fetch_url(
         f"https://{source_domain}/chaossocial/meta/master/federation.md",
         network.web_headers,
         (config.get("connection_timeout"), config.get("read_timeout"))
@@ -811,7 +811,7 @@ def fetch_fba_rss(args: argparse.Namespace) -> int:
         sources.update(domain)
 
     logger.info("Fetch FBA-specific RSS args.feed='%s' ...", args.feed)
-    response = utils.fetch_url(args.feed, network.web_headers, (config.get("connection_timeout"), config.get("read_timeout")))
+    response = network.fetch_url(args.feed, network.web_headers, (config.get("connection_timeout"), config.get("read_timeout")))
 
     logger.debug("response.ok='%s',response.status_code=%d,response.text()=%d", response.ok, response.status_code, len(response.text))
     if response.ok and response.status_code == 200 and len(response.text) > 0:
@@ -890,7 +890,7 @@ def fetch_fbabot_atom(args: argparse.Namespace) -> int:
     domains = list()
 
     logger.info("Fetching ATOM feed='%s' from FBA bot account ...", feed)
-    response = utils.fetch_url(feed, network.web_headers, (config.get("connection_timeout"), config.get("read_timeout")))
+    response = network.fetch_url(feed, network.web_headers, (config.get("connection_timeout"), config.get("read_timeout")))
 
     logger.debug("response.ok='%s',response.status_code=%d,response.text()=%d", response.ok, response.status_code, len(response.text))
     if response.ok and response.status_code == 200 and len(response.text) > 0:
@@ -955,53 +955,61 @@ def fetch_fbabot_atom(args: argparse.Namespace) -> int:
 def fetch_instances(args: argparse.Namespace) -> int:
     logger.debug("args[]='%s' - CALLED!", type(args))
 
-    logger.debug("args.domain='%s' - checking ...", args.domain)
-    if not validators.domain(args.domain):
-        logger.warning("args.domain='%s' is not valid.", args.domain)
-        return 100
-    elif blacklist.is_blacklisted(args.domain):
-        logger.warning("args.domain='%s' is blacklisted, won't check it!", args.domain)
-        return 101
-
     logger.debug("Invoking locking.acquire() ...")
     locking.acquire()
 
-    # Initialize values
-    domain = tidyup.domain(args.domain)
-    origin = software = None
+    # Init variables
+    rows = list()
 
-    # Fetch record
-    database.cursor.execute("SELECT origin, software FROM instances WHERE domain = ? LIMIT 1", [args.domain])
-    row = database.cursor.fetchone()
-    if row is not None:
-        origin = row["origin"]
-        software = row["software"]
+    # Is domain or software set?
+    if args.domain != "":
+        logger.debug("args.domain='%s' - checking ...", args.domain)
+        if not validators.domain(args.domain):
+            logger.warning("args.domain='%s' is not valid.", args.domain)
+            return 100
+        elif blacklist.is_blacklisted(args.domain):
+            logger.warning("args.domain='%s' is blacklisted, won't check it!", args.domain)
+            return 101
 
-    logger.debug("software='%s'", software)
-    if software is None:
-        logger.warning("args.domain='%s' has no software detected. You can try to run ./fba.py update_nodeinfo --domain=%s --force to get it updated.", args.domain, args.domain)
-        return 102
-    elif software_helper.is_relay(software):
-        logger.warning("args.domain='%s' is of software type '%s' which is not supported by this command. Please invoke fetch_relays instead.", args.domain, software)
-        return 103
+        logger.debug("args.domain='%s' - BEFORE!", args.domain)
+        domain = tidyup.domain(args.domain)
+        logger.debug("domain='%s' - AFTER!", domain)
 
-    # Initial fetch
-    try:
-        logger.info("Fetching instances from args.domain='%s',origin='%s',software='%s' ...", domain, origin, software)
-        federation.fetch_instances(domain, origin, software, inspect.currentframe().f_code.co_name)
-    except network.exceptions as exception:
-        logger.warning("Exception '%s' during fetching instances (fetch_instances) from args.domain='%s'", type(exception), args.domain)
-        instances.set_last_error(args.domain, exception)
-        instances.update(args.domain)
-        return 104
+        # Fetch record
+        database.cursor.execute("SELECT domain, origin, software FROM instances WHERE domain = ? LIMIT 1", [domain])
+        rows = database.cursor.fetchall()
 
-    if args.single:
-        logger.debug("Not fetching more instances - EXIT!")
-        return 0
+    logger.info("Checking %d entries ...", len(rows))
+    for row in rows:
+        logger.debug("row[domain]='%s',row[origin]='%s',row[software]='%s'", row["domain"], row["origin"], row["software"])
+        if row["software"] is None:
+            logger.warning("row[domain]='%s' has no software detected. You can try to run ./fba.py update_nodeinfo --domain=%s --force to get it updated - SKIPPED!", row["domain"], row["domain"])
+            continue
+        elif software_helper.is_relay(row["software"]):
+            logger.warning("row[domain]='%s' is of software type '%s' which is not supported by this command. Please invoke fetch_relays instead - SKIPPED!", row["domain"], row["software"])
+            continue
+
+        # Initial fetch
+        try:
+            logger.info("Fetching instances from row[domain]='%s',row[origin]='%s',row[software]='%s' ...", row["domain"], row["origin"], row["software"])
+            federation.fetch_instances(row["domain"], row["origin"], row["software"], inspect.currentframe().f_code.co_name)
+        except network.exceptions as exception:
+            logger.warning("Exception '%s' during fetching instances (fetch_instances) from row[domain]='%s'", type(exception), row["domain"])
+            instances.set_last_error(row["domain"], exception)
+            instances.update(row["domain"])
+            raise exception
+
+        if args.single:
+            logger.debug("Not fetching more instances - BREAK!")
+            break
 
     # Loop through some instances
     database.cursor.execute(
-        "SELECT domain, origin, software FROM instances WHERE software IN ('pleroma', 'mastodon', 'friendica', 'misskey', 'lemmy', 'peertube', 'takahe', 'gotosocial', 'brighteon', 'wildebeest', 'bookwyrm', 'mitra', 'areionskey', 'mammuthus', 'neodb') AND (last_instance_fetch IS NULL OR last_instance_fetch < ?) ORDER BY total_peers DESC, last_response_time ASC, last_updated ASC", [time.time() - config.get("recheck_instance")]
+        "SELECT domain, origin, software \
+FROM instances \
+WHERE software IN ('pleroma', 'mastodon', 'friendica', 'misskey', 'lemmy', 'peertube', 'takahe', 'gotosocial', 'brighteon', 'wildebeest', 'bookwyrm', 'mitra', 'areionskey', 'mammuthus', 'neodb') \
+AND (last_instance_fetch IS NULL OR last_instance_fetch < ?) \
+ORDER BY total_peers DESC, last_response_time ASC, last_updated ASC", [time.time() - config.get("recheck_instance")]
     )
 
     rows = database.cursor.fetchall()
@@ -1088,7 +1096,7 @@ def fetch_txt(args: argparse.Namespace) -> int:
     logger.info("Checking %d text file(s) ...", len(blocklists.txt_files))
     for row in blocklists.txt_files:
         logger.debug("Fetching row[url]='%s' ...", row["url"])
-        response = utils.fetch_url(row["url"], network.web_headers, (config.get("connection_timeout"), config.get("read_timeout")))
+        response = network.fetch_url(row["url"], network.web_headers, (config.get("connection_timeout"), config.get("read_timeout")))
 
         logger.debug("response.ok='%s',response.status_code=%d,response.text()=%d", response.ok, response.status_code, len(response.text))
         if response.ok and response.status_code == 200 and response.text != "":
@@ -1133,7 +1141,7 @@ def fetch_fedipact(args: argparse.Namespace) -> int:
         sources.update(source_domain)
 
     logger.info("Fetching / from source_domain='%s' ...", source_domain)
-    response = utils.fetch_url(
+    response = network.fetch_url(
         f"https://{source_domain}",
         network.web_headers,
         (config.get("connection_timeout"), config.get("read_timeout"))
@@ -1192,7 +1200,7 @@ def fetch_joinmobilizon(args: argparse.Namespace) -> int:
         sources.update(source_domain)
 
     logger.info("Fetching instances from source_domain='%s' ...", source_domain)
-    raw = utils.fetch_url(
+    raw = network.fetch_url(
         f"https://{source_domain}/api/v1/instances",
         network.web_headers,
         (config.get("connection_timeout"), config.get("read_timeout"))
@@ -1240,7 +1248,7 @@ def fetch_joinmisskey(args: argparse.Namespace) -> int:
         sources.update(source_domain)
 
     logger.info("Fetching instances.json from source_domain='%s' ...", source_domain)
-    raw = utils.fetch_url(
+    raw = network.fetch_url(
         f"https://{source_domain}/instances.json",
         network.web_headers,
         (config.get("connection_timeout"), config.get("read_timeout"))
@@ -1280,10 +1288,13 @@ def recheck_obfuscation(args: argparse.Namespace) -> int:
     locking.acquire()
 
     if isinstance(args.domain, str) and args.domain != "" and domain_helper.is_wanted(args.domain):
+        logger.debug("Fetching record for args.domain='%s' ...", args.domain)
         database.cursor.execute("SELECT domain, software, nodeinfo_url FROM instances WHERE (has_obfuscation = 1 OR has_obfuscation IS NULL) AND domain = ?", [args.domain])
     elif isinstance(args.software, str) and args.software != "" and validators.domain(args.software) == args.software:
+        logger.debug("Fetching records for args.software='%s' ...", args.software)
         database.cursor.execute("SELECT domain, software, nodeinfo_url FROM instances WHERE (has_obfuscation = 1 OR has_obfuscation IS NULL) AND software = ?", [args.software])
     else:
+        logger.debug("Fetching records where domains have obfuscated block entries ...")
         database.cursor.execute("SELECT domain, software, nodeinfo_url FROM instances WHERE has_obfuscation = 1 OR has_obfuscation IS NULL")
 
     rows = database.cursor.fetchall()
@@ -1342,7 +1353,7 @@ def recheck_obfuscation(args: argparse.Namespace) -> int:
             elif block["blocked"].endswith(".onion"):
                 logger.debug("blocked='%s' is a TOR onion domain name - SKIPPED!", block["blocked"])
                 continue
-            elif block["blocked"].endswith(".i2p") and config.get("allow_i2p_domain") == "true":
+            elif block["blocked"].endswith(".i2p") and not config.get("allow_i2p_domain"):
                 logger.debug("blocked='%s' is an I2P onion domain name - SKIPPED!", block["blocked"])
                 continue
             elif block["blocked"].endswith(".arpa"):
@@ -1764,7 +1775,7 @@ def fetch_relays(args: argparse.Namespace) -> int:
                     continue
             else:
                 logger.info("Fetching / from relay row[domain]='%s',row[software]='%s' ...", row["domain"], row["software"])
-                raw = utils.fetch_url(
+                raw = network.fetch_url(
                     f"https://{row['domain']}",
                     network.web_headers,
                     (config.get("connection_timeout"), config.get("read_timeout"))