Skip to content

Instantly share code, notes, and snippets.

@melissaboiko
Created June 27, 2021 18:43
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 melissaboiko/8c10702d203e42db236043d0b8fbd888 to your computer and use it in GitHub Desktop.
Save melissaboiko/8c10702d203e42db236043d0b8fbd888 to your computer and use it in GitHub Desktop.
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