Last active
November 9, 2022 19:54
-
-
Save hiway/bdaa98d1b70e0b1efa72c808b9128d52 to your computer and use it in GitHub Desktop.
Mastodon Domain Blocks WebUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
# Mastodon Domain Blocks | |
## Install | |
``` | |
python -m venv venv | |
source venv/bin/activate | |
pip install click mastodon.py streamlit watchdog | |
``` | |
## Run | |
``` | |
streamlit run mastodon_domain_blocks.py | |
``` | |
""" | |
import json | |
from pathlib import Path | |
import click | |
import streamlit as st | |
from mastodon import Mastodon | |
APP_NAME = "mastodon_admin" | |
APP_DIR = Path(click.get_app_dir(APP_NAME)) | |
APP_DIR.mkdir(exist_ok=True) | |
CONFIG_FILE = APP_DIR / f"{APP_NAME}.json" | |
def get_config() -> dict: | |
"""Get config from file""" | |
if CONFIG_FILE.exists(): | |
print(f"Loading config from {CONFIG_FILE}") | |
with open(CONFIG_FILE, "r", encoding="utf-8") as in_file: | |
return json.load(in_file) | |
else: | |
return { | |
"access_token": "", | |
"base_url": "", | |
} | |
def save_config(conf: dict): | |
"""Save config to file""" | |
with open(CONFIG_FILE, "w", encoding="utf-8") as out_file: | |
json.dump(conf, out_file) | |
st.header("Mastodon Domain Blocks") | |
config = get_config() | |
if config["access_token"] and config["base_url"]: | |
with st.spinner("Loading blocked domains..."): | |
mastodon = Mastodon( | |
access_token=config["access_token"].strip(), | |
api_base_url=config["base_url"].strip(), | |
) | |
block_list = mastodon.domain_blocks() | |
with st.form("bulk_add_domains"): | |
txt_domains = st.text_area(label="Blocked Domains", value="\n".join(block_list)) | |
btn_add_domains = st.form_submit_button(label="Update") | |
if btn_add_domains: | |
with st.spinner("Loading..."): | |
domains = txt_domains.splitlines() | |
for domain in domains: | |
if domain not in block_list: | |
mastodon.domain_block(domain) | |
block_list.append(domain) | |
for domain in block_list: | |
if domain not in domains: | |
mastodon.domain_unblock(domain) | |
block_list.remove(domain) | |
st.write(f"Blocking {len(block_list)} domains.") | |
else: | |
st.warning("Please login first.") | |
if not config["base_url"]: | |
st.write("Please enter your base url") | |
txt_base_url = st.text_input("Base URL") | |
config["base_url"] = txt_base_url.strip() | |
save_config(config) | |
if config["base_url"] and not config["access_token"]: | |
st.markdown( | |
f""" | |
If you have not already, please create a new app at | |
{config["base_url"]}/settings/applications | |
Once created, copy the **access_token** and paste it below. | |
""" | |
) | |
txt_access_token = st.text_input("Access Token") | |
config["access_token"] = txt_access_token.strip() | |
save_config(config) | |
if config["access_token"]: | |
st.success("Login successful.") | |
st.experimental_rerun() |
Author
hiway
commented
Nov 9, 2022
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment