From: Roland Date: Sat, 13 May 2023 22:00:28 +0000 (+0200) Subject: ALWAYS write SQL keywords in uppercase! X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=6ef0ac2c097c8be838011848f6b2905634c14217;p=fba.git ALWAYS write SQL keywords in uppercase! --- diff --git a/api.py b/api.py index e3ae0a2..03e3314 100644 --- 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() diff --git a/fetch_blocks.py b/fetch_blocks.py index ad16997..3045eaa 100644 --- a/fetch_blocks.py +++ b/fetch_blocks.py @@ -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: diff --git a/fetch_instances.py b/fetch_instances.py index 6ebf18c..e983924 100644 --- a/fetch_instances.py +++ b/fetch_instances.py @@ -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()