Skip to content

Commit 4e41434

Browse files
committed
Scan for dead communities
1 parent cdec34a commit 4e41434

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

.github/workflows/check_dead.yaml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: check communities
2+
3+
on:
4+
schedule:
5+
- cron: '44 2 1 * *' # At 02:44 on the 1st
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: checkout repo content
12+
uses: actions/checkout@v2 # checkout the repository content
13+
14+
- name: setup python
15+
uses: actions/setup-python@v4
16+
with:
17+
python-version: '3.11' # install the python version needed
18+
19+
- name: install python packages
20+
run: |
21+
python -m pip install --upgrade pip
22+
pip install PyGithub pythorhead
23+
24+
- name: execute py script # run main.py
25+
run: python ./scripts/raise_github_issues.py --pass "${{ secrets.LEMMY_PASSWORD }}" --token "${{ secrets.GITHUB_ISSUE_TOKEN }}"

scripts/raise_github_issues.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)