add instance blocks script

This commit is contained in:
notfire 2024-10-14 21:41:15 -04:00
parent 36bb90faf0
commit c539152c39
Signed by: notfire
GPG key ID: 3AFDACAAB4E56B16
3 changed files with 87 additions and 6 deletions

View file

@ -1,5 +1,6 @@
EMOJI_INSTANCE_BASE_PROTO="https" INSTANCE_BASE_PROTO="https"
EMOJI_INSTANCE_BASE_URL="yourinstance.net" INSTANCE_BASE_URL="yourinstance.net"
EMOJI_AUTH_TOKEN="Bearer yourtoken" AUTH_TOKEN="Bearer yourtoken"
EMOJI_FILE_TYPES='["png", "jpg", "gif"]' EMOJI_FILE_TYPES='["png", "jpg", "gif"]'
EMOJI_SUFFIXES='["256"]' EMOJI_SUFFIXES='["256"]'

View file

@ -3,10 +3,10 @@ from dotenv import load_dotenv
load_dotenv() load_dotenv()
instance_base_proto = os.getenv("EMOJI_INSTANCE_BASE_PROTO") instance_base_proto = os.getenv("INSTANCE_BASE_PROTO")
instance_base_url = os.getenv("EMOJI_INSTANCE_BASE_URL") instance_base_url = os.getenv("INSTANCE_BASE_URL")
instance_full_url = instance_base_proto + "://" + instance_base_url + "/api/iceshrimp/emoji" instance_full_url = instance_base_proto + "://" + instance_base_url + "/api/iceshrimp/emoji"
auth_token = os.getenv("EMOJI_AUTH_TOKEN") auth_token = os.getenv("AUTH_TOKEN")
image_types = json.loads(os.getenv("EMOJI_FILE_TYPES")) image_types = json.loads(os.getenv("EMOJI_FILE_TYPES"))
suffixes = json.loads(os.getenv("EMOJI_SUFFIXES")) suffixes = json.loads(os.getenv("EMOJI_SUFFIXES"))

80
instance-blocks.py Normal file
View file

@ -0,0 +1,80 @@
import requests, json, argparse, os
from dotenv import load_dotenv
load_dotenv()
parser = argparse.ArgumentParser()
sp = parser.add_subparsers(dest="list_or_push")
list_sp = sp.add_parser("list")
push_sp = sp.add_parser("push")
list_sp.add_argument("-o", "--output")
push_sp.add_argument("input")
list_grp = list_sp.add_mutually_exclusive_group()
list_grp.add_argument("-a", "--allowed", action="store_true")
list_grp.add_argument("-b", "--blocked", action="store_true")
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")
args = parser.parse_args()
if args.list_or_push == "list":
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"
elif args.allowed:
instance_full_url += "allowed"
ab = "allowed"
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)
if len(json_dumped) == 0:
print("no instances are " + ab)
exit()
if args.output:
x = open(args.output, "w+")
x.writelines(json.dumps(json_dumped))
else:
for instance in json_dumped:
print(instance)
elif args.list_or_push == "push":
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:
ab = "block"
elif args.allowed:
ab = "allow"
else:
print("specify -a or -b")
exit()
x = open(args.input, "r")
x_data = x.read()
x_json = json.loads(x_data)
headers = {"authorization": auth_token}
for instance in x_json:
instance["imported"] = instance.pop("isImported")
url = instance_full_url + instance["host"] + "/" + ab
instance.pop("host")
req = requests.post(url, headers=headers, data=instance)