Skip to content

Instantly share code, notes, and snippets.

@hiway
Last active November 9, 2022 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiway/bdaa98d1b70e0b1efa72c808b9128d52 to your computer and use it in GitHub Desktop.
Save hiway/bdaa98d1b70e0b1efa72c808b9128d52 to your computer and use it in GitHub Desktop.
Mastodon Domain Blocks WebUI
"""
# 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()
@hiway
Copy link
Author

hiway commented Nov 9, 2022

Screenshot 2022-11-10 at 1 11 53 AM

Screenshot 2022-11-10 at 1 12 03 AM

Screenshot 2022-11-10 at 1 13 01 AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment