|
| 1 | +#!/usr/bin/python3 |
| 2 | +import json |
| 3 | +import argparse |
| 4 | +import sys |
| 5 | +from github import Github |
| 6 | +from github import Auth |
| 7 | +from pythorhead import Lemmy |
| 8 | + |
| 9 | +def get_args(): |
| 10 | + parser = argparse.ArgumentParser(description="Check communities", |
| 11 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 12 | + parser.add_argument("-d", "--directory", help="Directory JSON", default="./lists/directory.json") |
| 13 | + parser.add_argument("-t", "--token", help="GitHub auth token") |
| 14 | + parser.add_argument("-r", "--repo", help="GitHub repo", default="fnic-lemmy/lemmy-directory") |
| 15 | + parser.add_argument("-u", "--user", help="Lemmy user", default="directorybot") |
| 16 | + parser.add_argument("-p", "--pass", help="Lemmy password") |
| 17 | + parser.add_argument("-i", "--inst", help="Lemmy instance", default="lemmy.dbzer0.com") |
| 18 | + |
| 19 | + args = parser.parse_args() |
| 20 | + a = vars(args) |
| 21 | + |
| 22 | + return(a) |
| 23 | + |
| 24 | +def raise_issue(ghtoken, ghrepo, title, desc): |
| 25 | + |
| 26 | + auth = Auth.Token(ghtoken) |
| 27 | + g = Github(auth=auth) |
| 28 | + repo = g.get_repo(ghrepo) |
| 29 | + my_issues = repo.get_issues(state = "open", creator = "fnic-bot") |
| 30 | + for issue in my_issues: |
| 31 | + if issue.title == title: |
| 32 | + print('already raised') |
| 33 | + issue.create_comment(f'This has failed again.\n\n{desc}') |
| 34 | + return |
| 35 | + |
| 36 | + i = repo.create_issue(title = title, body = desc, labels = [repo.get_label(name = "Community Removal")]) |
| 37 | + |
| 38 | + |
| 39 | +a = get_args() |
| 40 | +dfile=a['directory'] |
| 41 | +ghtoken=a['token'] |
| 42 | +ghrepo=a['repo'] |
| 43 | +instance=a['inst'] |
| 44 | +user=a['user'] |
| 45 | +pw=a['pass'] |
| 46 | + |
| 47 | +lemmy = Lemmy(f'https://{instance}', raise_exceptions=True, request_timeout=30) |
| 48 | +try: |
| 49 | + lemmy.log_in(user, pw) |
| 50 | +except Exception as e: |
| 51 | + print(f'login failed: {e}\n') |
| 52 | + sys.exit(1) |
| 53 | + |
| 54 | + |
| 55 | +with open(dfile) as f: |
| 56 | + comms = json.load(f) |
| 57 | + |
| 58 | +for group in comms: |
| 59 | + print(group) |
| 60 | + for subgroup in comms[group]: |
| 61 | + print(f' {subgroup}') |
| 62 | + for subsubgroup in comms[group][subgroup]: |
| 63 | + print(f' {subsubgroup}') |
| 64 | + if len(comms[group][subgroup][subsubgroup]) > 0: |
| 65 | + for cm in comms[group][subgroup][subsubgroup]: |
| 66 | + print(f' {cm}') |
| 67 | + cms = cm.split('@') |
| 68 | + if cms[1] in ['kbin.social', 'fedia.io']: |
| 69 | + c = 'm' |
| 70 | + else: |
| 71 | + c = 'c' |
| 72 | + url = f'https://{cms[1]}/{c}/{cms[0]}' |
| 73 | + try: |
| 74 | + s = lemmy.resolve_object(url) |
| 75 | + except Exception as e: |
| 76 | + desc = f'{group}-{subgroup}-{subsubgroup} [{cm}]\nURL lookup for {url} returned:\n```\n{e}\n```\n' |
| 77 | + print(f' * {e}') |
| 78 | + raise_issue(ghtoken, ghrepo, f'Remove {cm}', desc) |
0 commit comments