tidy up instance blocks script, also add a way to clear

This commit is contained in:
notfire 2024-10-15 10:37:23 -04:00
parent c539152c39
commit ede3201f23
Signed by: notfire
GPG key ID: 3AFDACAAB4E56B16
2 changed files with 40 additions and 2 deletions

3
.gitignore vendored
View file

@ -1 +1,2 @@
.env
.env
blocked.json

View file

@ -8,6 +8,7 @@ parser = argparse.ArgumentParser()
sp = parser.add_subparsers(dest="list_or_push")
list_sp = sp.add_parser("list")
push_sp = sp.add_parser("push")
clear_sp = sp.add_parser("clear")
list_sp.add_argument("-o", "--output")
push_sp.add_argument("input")
@ -20,6 +21,10 @@ push_grp = push_sp.add_mutually_exclusive_group()
push_grp.add_argument("-a", "--allowed", action="store_true")
push_grp.add_argument("-b", "--blocked", action="store_true")
clear_grp = clear_sp.add_mutually_exclusive_group()
clear_grp.add_argument("-a", "--allowed", action="store_true")
clear_grp.add_argument("-b", "--blocked", action="store_true")
args = parser.parse_args()
if args.list_or_push == "list":
@ -73,8 +78,40 @@ elif args.list_or_push == "push":
x_json = json.loads(x_data)
headers = {"authorization": auth_token}
ctr = 0
for instance in x_json:
ctr += 1
print(f"({ctr}/{len(x_json)}) {ab}ing {instance["host"]} for {instance["reason"]}")
instance["imported"] = instance.pop("isImported")
url = instance_full_url + instance["host"] + "/" + ab
instance.pop("host")
req = requests.post(url, headers=headers, data=instance)
req = requests.post(url, headers=headers, data=instance)
elif args.list_or_push == "clear":
instance_base_proto = os.getenv("INSTANCE_BASE_PROTO")
instance_base_url = os.getenv("INSTANCE_BASE_URL")
instance_full_url = instance_base_proto + "://" + instance_base_url + "/api/iceshrimp/admin/instances/"
auth_token = os.getenv("AUTH_TOKEN")
if args.blocked:
instance_full_url += "blocked"
ab = "blocked"
ab2 = "unblock"
elif args.allowed:
instance_full_url += "allowed"
ab = "allowed"
ab2 = "disallow"
else:
print("specify -a or -b")
exit()
headers = {"authorization": auth_token}
req = requests.get(instance_full_url, headers=headers)
json_dumped = json.loads(req.text)
ctr = 0
for instance in json_dumped:
ctr += 1
print(f"({ctr}/{len(json_dumped)}) {ab2}ing {instance["host"]}")
instance_full_url = instance_base_proto + "://" + instance_base_url + "/api/iceshrimp/admin/instances/" + instance["host"] + "/" + ab2
req = requests.post(instance_full_url, headers=headers)