Compare commits
1 Commits
markdown_e
...
export
Author | SHA1 | Date | |
---|---|---|---|
3dce62417e |
@@ -7,7 +7,6 @@ import os
|
|||||||
import toml
|
import toml
|
||||||
|
|
||||||
from mastodon_blocklist_deploy.models import Instance
|
from mastodon_blocklist_deploy.models import Instance
|
||||||
from mastodon_blocklist_deploy.helpers import blocklist_to_markdown
|
|
||||||
|
|
||||||
|
|
||||||
def load_blocklist_file(filename: str) -> [Instance]:
|
def load_blocklist_file(filename: str) -> [Instance]:
|
||||||
@@ -39,9 +38,6 @@ def load_blocklist_from_instance(server: str, token: str) -> [Instance]:
|
|||||||
else:
|
else:
|
||||||
raise ConnectionError(f"Could not connect to the server ({response.status_code}: {response.reason})")
|
raise ConnectionError(f"Could not connect to the server ({response.status_code}: {response.reason})")
|
||||||
|
|
||||||
def remove_key_from_dict(dict, key):
|
|
||||||
del dict[key]
|
|
||||||
return dict
|
|
||||||
|
|
||||||
def cli():
|
def cli():
|
||||||
parser = argparse.ArgumentParser(description='Deploy blocklist updates to a mastodon server')
|
parser = argparse.ArgumentParser(description='Deploy blocklist updates to a mastodon server')
|
||||||
@@ -57,7 +53,6 @@ def cli():
|
|||||||
parser.add_argument('-o', '--output', help="Filename where to export the blocklist")
|
parser.add_argument('-o', '--output', help="Filename where to export the blocklist")
|
||||||
parser.add_argument('-v', '--verbose', action='store_true')
|
parser.add_argument('-v', '--verbose', action='store_true')
|
||||||
parser.add_argument('-n', '--no-delete', action='store_true', help="Do not delete existing blocks")
|
parser.add_argument('-n', '--no-delete', action='store_true', help="Do not delete existing blocks")
|
||||||
parser.add_argument('--markdown', action='store_true', help="Export as markdown table")
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
if args.verbose:
|
if args.verbose:
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
@@ -69,8 +64,6 @@ def cli():
|
|||||||
else:
|
else:
|
||||||
token = os.getenv('MBD_TOKEN')
|
token = os.getenv('MBD_TOKEN')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
"""if there is a remote blocklist provided load this instead of fetching it from a server (for debugging reasons)"""
|
"""if there is a remote blocklist provided load this instead of fetching it from a server (for debugging reasons)"""
|
||||||
if args.remote_blocklist:
|
if args.remote_blocklist:
|
||||||
with open(args.remote_blocklist) as f:
|
with open(args.remote_blocklist) as f:
|
||||||
@@ -97,16 +90,10 @@ def cli():
|
|||||||
Instance.apply_blocks_from_diff(diffs, args.server, token, args.no_delete)
|
Instance.apply_blocks_from_diff(diffs, args.server, token, args.no_delete)
|
||||||
elif args.action == "export":
|
elif args.action == "export":
|
||||||
if not args.output:
|
if not args.output:
|
||||||
if args.markdown:
|
print(toml.dumps({"instances": [b.exportable_dict for b in remote_blocklist]}))
|
||||||
print(blocklist_to_markdown(remote_blocklist))
|
|
||||||
else:
|
|
||||||
print(toml.dumps({"instances": [remove_key_from_dict(b.__dict__, 'id') for b in remote_blocklist]}))
|
|
||||||
else:
|
else:
|
||||||
with open(args.output, "w") as f:
|
with open(args.output, "w") as f:
|
||||||
if args.markdown:
|
toml.dump({"instances": [b.exportable_dict for b in remote_blocklist]}, f)
|
||||||
f.write(blocklist_to_markdown(remote_blocklist))
|
|
||||||
else:
|
|
||||||
toml.dump({"instances": [remove_key_from_dict(b.__dict__, 'id') for b in remote_blocklist]}, f)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@@ -1,6 +0,0 @@
|
|||||||
from mastodon_blocklist_deploy.models import Instance
|
|
||||||
def blocklist_to_markdown(blocklist:[Instance]):
|
|
||||||
markdown_string = "| Instance | Status | Reason |\n | --- | --- | --- |\n"
|
|
||||||
for instance in blocklist:
|
|
||||||
markdown_string += f"| {instance.domain} | {instance.severity} | {instance.public_comment} |\n"
|
|
||||||
return markdown_string
|
|
@@ -27,6 +27,14 @@ class Instance:
|
|||||||
def status_str(self):
|
def status_str(self):
|
||||||
return f"{self.severity}\nReject reports: {self.reject_reports}\nReject media: {self.reject_media}\nObfuscate: {self.obfuscate}"
|
return f"{self.severity}\nReject reports: {self.reject_reports}\nReject media: {self.reject_media}\nObfuscate: {self.obfuscate}"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def exportable_dict(self):
|
||||||
|
keys = ["domain", "severity", "public_comment", "private_comment", "obfuscate", "reject_media", "reject_reports"]
|
||||||
|
exportable = {}
|
||||||
|
for key in keys:
|
||||||
|
exportable[key] = getattr(self, key)
|
||||||
|
return exportable
|
||||||
|
|
||||||
def parse_remote_block(self, instance_dict):
|
def parse_remote_block(self, instance_dict):
|
||||||
self.domain = instance_dict["domain"]
|
self.domain = instance_dict["domain"]
|
||||||
self.id = instance_dict["id"]
|
self.id = instance_dict["id"]
|
||||||
|
Reference in New Issue
Block a user