]> git.mxchange.org Git - fba.git/commitdiff
ALWAYS write SQL keywords in uppercase!
authorRoland <roland@mxchange.org>
Sat, 13 May 2023 22:00:28 +0000 (00:00 +0200)
committerRoland <roland@mxchange.org>
Sat, 13 May 2023 22:00:28 +0000 (00:00 +0200)
api.py
fetch_blocks.py
fetch_instances.py

diff --git a/api.py b/api.py
index e3ae0a22304854f2724e123946f4b9fabc978c58..03e3314eafbe4b09eadba6fcb94ea75cfb53dca7 100644 (file)
--- a/api.py
+++ b/api.py
@@ -23,7 +23,7 @@ def get_hash(domain: str) -> str:
 def info():
     conn = sqlite3.connect("blocks.db")
     c = conn.cursor()
-    c.execute("select (select count(domain) from instances), (select count(domain) from instances where software in ('pleroma', 'mastodon', 'misskey', 'gotosocial', 'friendica')), (select count(blocker) from blocks)")
+    c.execute("SELECT (SELECT count(domain) FROM  instances), (SELECT count(domain) FROM  instances WHERE software in ('pleroma', 'mastodon', 'misskey', 'gotosocial', 'friendica')), (SELECT count(blocker) FROM  blocks)")
     known, indexed, blocks = c.fetchone()
     c.close()
     return {
@@ -41,11 +41,11 @@ def top(blocked: int = None, blockers: int = None):
     elif blocked != None:
         if blocked > 500:
             raise HTTPException(status_code=400, detail="Too many results")
-        c.execute("select blocked, count(blocked) from blocks where block_level = 'reject' group by blocked order by count(blocked) desc limit ?", (blocked,))
+        c.execute("SELECT blocked, count(blocked) FROM  blocks WHERE block_level = 'reject' group by blocked order by count(blocked) desc limit ?", (blocked,))
     elif blockers != None:
         if blockers > 500:
             raise HTTPException(status_code=400, detail="Too many results")
-        c.execute("select blocker, count(blocker) from blocks where block_level = 'reject' group by blocker order by count(blocker) desc limit ?", (blockers,))
+        c.execute("SELECT blocker, count(blocker) FROM  blocks WHERE block_level = 'reject' group by blocker order by count(blocker) desc limit ?", (blockers,))
     scores = c.fetchall()
     c.close()
 
@@ -69,12 +69,12 @@ def blocked(domain: str = None, reason: str = None, reverse: str = None):
     if domain != None:
         wildchar = "*." + ".".join(domain.split(".")[-domain.count("."):])
         punycode = domain.encode('idna').decode('utf-8')
-        c.execute("select blocker, blocked, block_level, reason, first_added, last_seen from blocks where blocked = ? or blocked = ? or blocked = ? or blocked = ? or blocked = ? or blocked = ? order by first_added asc",
+        c.execute("SELECT blocker, blocked, block_level, reason, first_added, last_seen FROM  blocks WHERE blocked = ? OR blocked = ? OR blocked = ? OR blocked = ? OR blocked = ? OR blocked = ? order by first_added asc",
                   (domain, "*." + domain, wildchar, get_hash(domain), punycode, "*." + punycode))
     elif reverse != None:
-        c.execute("select blocker, blocked, block_level, reason, first_added, last_seen from blocks where blocker = ? order by first_added asc", (reverse,))
+        c.execute("SELECT blocker, blocked, block_level, reason, first_added, last_seen FROM  blocks WHERE blocker = ? order by first_added asc", (reverse,))
     else:
-        c.execute("select blocker, blocked, block_level, reason, first_added, last_seen from blocks where reason like ? and reason != '' order by first_added asc", ("%"+reason+"%",))
+        c.execute("SELECT blocker, blocked, block_level, reason, first_added, last_seen FROM  blocks WHERE reason like ? AND reason != '' order by first_added asc", ("%"+reason+"%",))
     blocks = c.fetchall()
     c.close()
 
@@ -162,10 +162,10 @@ def rss(request: Request, domain: str = None):
     if domain != None:
         wildchar = "*." + ".".join(domain.split(".")[-domain.count("."):])
         punycode = domain.encode('idna').decode('utf-8')
-        c.execute("select blocker, blocked, block_level, reason, first_added, last_seen from blocks where blocked = ? or blocked = ? or blocked = ? or blocked = ? or blocked = ? or blocked = ? order by first_added desc limit 50",
+        c.execute("SELECT blocker, blocked, block_level, reason, first_added, last_seen FROM  blocks WHERE blocked = ? OR blocked = ? OR blocked = ? OR blocked = ? OR blocked = ? OR blocked = ? order by first_added desc limit 50",
                   (domain, "*." + domain, wildchar, get_hash(domain), punycode, "*." + punycode))
     else:
-        c.execute("select blocker, blocked, block_level, reason, first_added, last_seen from blocks order by first_added desc limit 50")
+        c.execute("SELECT blocker, blocked, block_level, reason, first_added, last_seen FROM  blocks order by first_added desc limit 50")
     blocks = c.fetchall()
     c.close()
 
index ad1699758dc12dfa12ebd5a9601c4f03ca67d0e6..3045eaa0907337a0bbe95f3e877b1b9eaeda5c77 100644 (file)
@@ -227,7 +227,7 @@ conn = sqlite3.connect("blocks.db")
 c = conn.cursor()
 
 c.execute(
-    "select domain, software from instances where software in ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial')"
+    "SELECT domain, software FROM  instances WHERE software in ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial')"
 )
 
 for blocker, software in c.fetchall():
@@ -252,28 +252,28 @@ for blocker, software in c.fetchall():
                         if blocked.count("*") > 1:
                             # -ACK!-oma also started obscuring domains without hash
                             c.execute(
-                                "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
+                                "SELECT domain FROM  instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("*", "_"),)
                             )
                             searchres = c.fetchone()
                             if searchres != None:
                                 blocked = searchres[0]
 
                         c.execute(
-                            "select domain from instances where domain = ?", (blocked,)
+                            "SELECT domain FROM  instances WHERE domain = ?", (blocked,)
                         )
                         if c.fetchone() == None:
                             c.execute(
-                                "insert into instances select ?, ?, ?",
+                                "INSERT INTO instances SELECT ?, ?, ?",
                                 (blocked, get_hash(blocked), get_type(blocked)),
                             )
                         timestamp = int(time())
                         c.execute(
-                            "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
+                            "SELECT * FROM  blocks WHERE blocker = ? AND blocked = ? AND block_level = ?",
                             (blocker, blocked, block_level),
                         )
                         if c.fetchone() == None:
                             c.execute(
-                                "insert into blocks select ?, ?, '', ?, ?, ?",
+                                "INSERT INTO blocks SELECT ?, ?, '', ?, ?, ?",
                                 (blocker, blocked, block_level, timestamp, timestamp),
                             )
                             if block_level == "reject":
@@ -284,7 +284,7 @@ for blocker, software in c.fetchall():
                                     })
                         else:
                             c.execute(
-                                "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
+                                "UPDATE blocks set last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ?",
                                 (timestamp, blocker, blocked, block_level)
                             )
             conn.commit()
@@ -303,13 +303,13 @@ for blocker, software in c.fetchall():
                         if blocked.count("*") > 1:
                             # same domain guess as above, but for reasons field
                             c.execute(
-                                "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
+                                "SELECT domain FROM  instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("*", "_"),)
                             )
                             searchres = c.fetchone()
                             if searchres != None:
                                 blocked = searchres[0]
                         c.execute(
-                            "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
+                            "UPDATE blocks set reason = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND reason = ''",
                             (reason["reason"], blocker, blocked, block_level),
                         )
                         for entry in blockdict:
@@ -364,17 +364,17 @@ for blocker, software in c.fetchall():
                     blocked = tidyup(blocked)
                     if blocked.count("*") <= 1:
                         c.execute(
-                            "select hash from instances where hash = ?", (blocked_hash,)
+                            "SELECT hash FROM  instances WHERE hash = ?", (blocked_hash,)
                         )
                         if c.fetchone() == None:
                             c.execute(
-                                "insert into instances select ?, ?, ?",
+                                "INSERT INTO instances SELECT ?, ?, ?",
                                 (blocked, get_hash(blocked), get_type(blocked)),
                             )
                     else:
                         # Doing the hash search for instance names as well to tidy up DB
                         c.execute(
-                            "select domain from instances where hash = ?", (blocked_hash,)
+                            "SELECT domain FROM  instances WHERE hash = ?", (blocked_hash,)
                         )
                         searchres = c.fetchone()
                         if searchres != None:
@@ -382,12 +382,12 @@ for blocker, software in c.fetchall():
 
                     timestamp = int(time())
                     c.execute(
-                        "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
+                        "SELECT * FROM  blocks WHERE blocker = ? AND blocked = ? AND block_level = ?",
                         (blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
                     )
                     if c.fetchone() == None:
                         c.execute(
-                            "insert into blocks select ?, ?, ?, ?, ?, ?",
+                            "INSERT INTO blocks SELECT ?, ?, ?, ?, ?, ?",
                             (
                                 blocker,
                                 blocked if blocked.count("*") <= 1 else blocked_hash,
@@ -405,12 +405,12 @@ for blocker, software in c.fetchall():
                                 })
                     else:
                         c.execute(
-                            "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
+                            "UPDATE blocks set last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ?",
                             (timestamp, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
                         )
                     if reason != '':
                         c.execute(
-                            "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
+                            "UPDATE blocks set reason = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND reason = ''",
                             (reason, blocker, blocked if blocked.count("*") <= 1 else blocked_hash, block_level),
                         )
             conn.commit()
@@ -431,7 +431,7 @@ for blocker, software in c.fetchall():
                     if blocked.count("*") > 0:
                         # Some friendica servers also obscure domains without hash
                         c.execute(
-                            "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
+                            "SELECT domain FROM  instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("*", "_"),)
                         )
                         searchres = c.fetchone()
                         if searchres != None:
@@ -440,29 +440,29 @@ for blocker, software in c.fetchall():
                     if blocked.count("?") > 0:
                         # Some obscure them with question marks, not sure if that's dependent on version or not
                         c.execute(
-                            "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("?", "_"),)
+                            "SELECT domain FROM  instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("?", "_"),)
                         )
                         searchres = c.fetchone()
                         if searchres != None:
                             blocked = searchres[0]
 
                     c.execute(
-                        "select domain from instances where domain = ?", (blocked,)
+                        "SELECT domain FROM  instances WHERE domain = ?", (blocked,)
                     )
                     if c.fetchone() == None:
                         c.execute(
-                            "insert into instances select ?, ?, ?",
+                            "INSERT INTO instances SELECT ?, ?, ?",
                             (blocked, get_hash(blocked), get_type(blocked)),
                         )
 
                     timestamp = int(time())
                     c.execute(
-                        "select * from blocks where blocker = ? and blocked = ?",
+                        "SELECT * FROM  blocks WHERE blocker = ? AND blocked = ?",
                         (blocker, blocked),
                     )
                     if c.fetchone() == None:
                         c.execute(
-                            "insert into blocks select ?, ?, ?, ?, ?, ?",
+                            "INSERT INTO blocks SELECT ?, ?, ?, ?, ?, ?",
                             (
                                 blocker,
                                 blocked,
@@ -480,12 +480,12 @@ for blocker, software in c.fetchall():
                                 })
                     else:
                         c.execute(
-                            "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
+                            "UPDATE blocks set last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ?",
                             (timestamp, blocker, blocked, block_level),
                         )
                     if reason != '':
                         c.execute(
-                            "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
+                            "UPDATE blocks set reason = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND reason = ''",
                             (reason, blocker, blocked, block_level),
                         )
             conn.commit()
@@ -504,28 +504,28 @@ for blocker, software in c.fetchall():
                 if blocked.count("*") > 0:
                     # GTS does not have hashes for obscured domains, so we have to guess it
                     c.execute(
-                        "select domain from instances where domain like ? order by rowid limit 1", (blocked.replace("*", "_"),)
+                        "SELECT domain FROM  instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", (blocked.replace("*", "_"),)
                     )
                     searchres = c.fetchone()
                     if searchres != None:
                         blocked = searchres[0]
 
                 c.execute(
-                    "select domain from instances where domain = ?", (blocked,)
+                    "SELECT domain FROM  instances WHERE domain = ?", (blocked,)
                 )
                 if c.fetchone() == None:
                     c.execute(
-                        "insert into instances select ?, ?, ?",
+                        "INSERT INTO instances SELECT ?, ?, ?",
                         (blocked, get_hash(blocked), get_type(blocked)),
                     )
                 c.execute(
-                    "select * from blocks where blocker = ? and blocked = ? and block_level = ?",
+                    "SELECT * FROM  blocks WHERE blocker = ? AND blocked = ? AND block_level = ?",
                     (blocker, blocked, "reject"),
                 )
                 timestamp = int(time())
                 if c.fetchone() == None:
                     c.execute(
-                        "insert into blocks select ?, ?, ?, ?, ?, ?",
+                        "INSERT INTO blocks SELECT ?, ?, ?, ?, ?, ?",
                            (blocker, blocked, "", "reject", timestamp, timestamp),
                     )
                     blockdict.append(
@@ -535,13 +535,13 @@ for blocker, software in c.fetchall():
                         })
                 else:
                     c.execute(
-                        "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
+                        "UPDATE blocks set last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ?",
                         (timestamp, blocker, blocked, "reject"),
                     )
                 if "public_comment" in peer:
                     reason = peer["public_comment"]
                     c.execute(
-                        "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
+                        "UPDATE blocks set reason = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND reason = ''",
                         (reason, blocker, blocked, "reject"),
                     )
                     for entry in blockdict:
index 6ebf18c2b7996924c5b4b1280d74989566a3ab12..e983924879333c5193e574b1f066750e7a3f3e33 100644 (file)
@@ -65,7 +65,7 @@ conn = sqlite3.connect("blocks.db")
 c = conn.cursor()
 
 c.execute(
-    "select domain from instances where 1"
+    "SELECT domain FROM  instances WHERE 1"
 )
 
 for instance in peerlist:
@@ -82,11 +82,11 @@ for instance in peerlist:
     print(instance)
     try:
         c.execute(
-            "select domain from instances where domain = ?", (instance,)
+            "SELECT domain FROM  instances WHERE domain = ?", (instance,)
         )
         if c.fetchone() == None:
             c.execute(
-                "insert into instances select ?, ?, ?",
+                "INSERT INTO instances SELECT ?, ?, ?",
                 (instance, get_hash(instance), get_type(instance)),
             )
         conn.commit()