From 58be487edc624761066a45049fe5b3a6ad815970 Mon Sep 17 00:00:00 2001 From: Enju Aihara <5-EnjuAihara@users.noreply.gitlab.varis.social> Date: Sat, 5 Mar 2022 12:19:28 +0100 Subject: [PATCH] drying some code --- fetch_blocks.py | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/fetch_blocks.py b/fetch_blocks.py index 3b1bd84..27cbbab 100644 --- a/fetch_blocks.py +++ b/fetch_blocks.py @@ -39,30 +39,15 @@ with open("mastodon_instances.txt", "r") as f: try: c.execute("delete from blocks where blocker = ?", (blocker,)) json = loads(get(f"http://127.0.0.1:8069/{blocker}").text) - for blocked in json["reject"]: - if blocked["domain"].count("*") > 1: - c.execute("insert into blocks select ?, ifnull((select domain from instances where hash = ?), ?), ?, 'reject'", (blocker, blocked["hash"], blocked["hash"], blocked['reason'])) - else: - c.execute("select case when ? in (select domain from instances) then 1 else 0 end", (blocked["domain"],)) - if c.fetchone() == (0,): - c.execute("insert into instances select ?, ?", (blocked["domain"], sha256(bytes(blocked["domain"], "utf-8")).hexdigest())) - c.execute("insert into blocks select ?, ?, ?, 'reject'", (blocker, blocked["domain"], blocked["reason"])) - for blocked in json["media_removal"]: - if blocked["domain"].count("*") > 1: - c.execute("insert into blocks select ?, ifnull((select domain from instances where hash = ?), ?), ?, 'media_removal'", (blocker, blocked["hash"], blocked["hash"], blocked['reason'])) - else: - c.execute("select case when ? in (select domain from instances) then 1 else 0 end", (blocked["domain"],)) - if c.fetchone() == (0,): - c.execute("insert into instances select ?, ?", (blocked["domain"], sha256(bytes(blocked["domain"], "utf-8")).hexdigest())) - c.execute("insert into blocks select ?, ?, ?, 'media_removal'", (blocker, blocked["domain"], blocked["reason"])) - for blocked in json["federated_timeline_removal"]: - if blocked["domain"].count("*") > 1: - c.execute("insert into blocks select ?, ifnull((select domain from instances where hash = ?), ?), ?, 'federated_timeline_removal'", (blocker, blocked["hash"], blocked["hash"], blocked['reason'])) - else: - c.execute("select case when ? in (select domain from instances) then 1 else 0 end", (blocked["domain"],)) - if c.fetchone() == (0,): - c.execute("insert into instances select ?, ?", (blocked["domain"], sha256(bytes(blocked["domain"], "utf-8")).hexdigest())) - c.execute("insert into blocks select ?, ?, ?, 'federated_timeline_removal'", (blocker, blocked["domain"], blocked["reason"])) + for block_level in ["reject", "media_removal", "federated_timeline_removal"]: + for blocked in json[block_level]: + if blocked["domain"].count("*") > 1: + c.execute("insert into blocks select ?, ifnull((select domain from instances where hash = ?), ?), ?, ?", (blocker, blocked["hash"], blocked["hash"], blocked['reason'], block_level)) + else: + c.execute("select case when ? in (select domain from instances) then 1 else 0 end", (blocked["domain"],)) + if c.fetchone() == (0,): + c.execute("insert into instances select ?, ?", (blocked["domain"], sha256(bytes(blocked["domain"], "utf-8")).hexdigest())) + c.execute("insert into blocks select ?, ?, ?, ?", (blocker, blocked["domain"], blocked["reason"], block_level)) conn.commit() except: pass