Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
injects everything in fediblock.org directly in the glitch-soc blocked domains database
#!/usr/bin/env python3
import sys
import psycopg2
import urllib.request
import json
from datetime import datetime
from typing import List
# expects working pgident auth. edit parameters to your own:
database='mastodon_production'
dbuser='mastodon'
URL = 'https://fediblock.org/api/v1/list'
BLOCKSQL = """
INSERT INTO domain_blocks (
severity,
reject_media,
reject_reports,
domain,
created_at,
updated_at,
public_comment
) VALUES (1, true, true, %s, %s, %s, %s )
;"""
conn = psycopg2.connect(database=database, user=dbuser)
conn.autocommit = True
CUR = conn.cursor()
def begone(
domain: str,
description: str = '',
report_time: str = '',
tags: List[str] = [],
):
try:
public_comment = "From %s:\n%s" % (URL, description)
if tags:
public_comment += "\nTags: " + ','.join(tags)
created_at = report_time
updated_at = datetime.now()
CUR.execute(BLOCKSQL, (domain, created_at, updated_at, public_comment))
except psycopg2.IntegrityError:
print("Skipping %s (already blocked?)" % domain)
def parse_blockurl(url: str):
with urllib.request.urlopen(url) as req:
block = json.loads(req.read().decode())
# timezone unspecified in the API, who knows. use naïve obj.
block['report_time'] = datetime.strptime(block['report_time'],
'%Y-%m-%dT%H:%M:%S')
begone(*(block[key] for key in ('instance', 'description', 'report_time', 'tags')))
with urllib.request.urlopen("https://fediblock.org/api/v1/list") as req:
blocklist = json.loads(req.read().decode())
for item in blocklist:
parse_blockurl(item['url'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment