Skip to content

Commit 23f6dc0

Browse files
committed
Update to class based structure, add locked user support
Closes T132 Closes T133
1 parent 6999446 commit 23f6dc0

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

RemoveBase.py

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import pywikibot
22
import re
3+
import requests
34

45

56
class RemoveBase:
67
def __init__(self, log_name, category):
78
self.pattern = r'(?:User talk:)([^/\n]+)'
8-
self.site = pywikibot.Site(fam="wikipedia", code="en", user="TheSandBot")
9+
self.site = pywikibot.Site() # fam="wikipedia", code="en", user="TheSandBot")
910
self.count = 0
1011
self.log_filename = log_name
11-
self.category = pywikibot.Category(self.site, category).articles()
12+
self.session = requests.Session()
13+
try:
14+
self.category = pywikibot.Category(self.site, category).articles()
15+
except pywikibot.InvalidTitle:
16+
print("\nBad category title")
17+
exit()
1218
self.cat_name = category
1319

1420
def category_remove(self, target: str, page: pywikibot.Page) -> None:
@@ -25,13 +31,21 @@ def generate_user(self, page: pywikibot.Page) -> pywikibot.User:
2531
user_raw = m.group(1)
2632
return pywikibot.User(self.site, user_raw)
2733

28-
def run(self):
29-
pass
34+
def isLocked(self, username):
35+
params = {
36+
"action": "query",
37+
"format": "json",
38+
"meta": "globaluserinfo",
39+
"guiuser": str(username),
40+
"guiprop": "groups|merged|unattached"
41+
}
42+
result = self.session.get(url="https://en.wikipedia.org/w/api.php", params=params)
43+
return 'locked' in result.json()["query"]["globaluserinfo"]
3044

3145
def log(self, page):
3246
with open(self.log_filename, 'a+') as f:
33-
self.count += 1
34-
f.write(str(self.count) + " " + str(page.title()) + "\n")
47+
# self.count += 1
48+
f.write(str(page.title()) + "\n")
3549

3650

3751
class RemoveBlocked(RemoveBase):
@@ -45,32 +59,50 @@ def run(self):
4559
if page.title() == "Template:Uw-corpname":
4660
continue
4761
user = self.generate_user(page)
48-
if user.isBlocked():
62+
63+
if self.isLocked(user.username):
64+
self.log(page)
65+
self.category_remove(self.target, page)
66+
page.save(
67+
summary="Removing " + self.cat_name + " as user is locked." +
68+
" ([[Wikipedia:Bots/Requests for approval/" + self.brfa + "|BRFA]])", minor=True,
69+
botflag=True, force=True)
70+
print("Saved " + str(page.title()))
71+
elif user.isBlocked():
4972
self.log(page)
5073
self.category_remove(self.target, page)
5174
page.save(
5275
summary="Removing " + self.cat_name + " as user is blocked." +
53-
" ([[" + self.brfa + "|BRFA]])", minor=True,
76+
" ([[Wikipedia:Bots/Requests for approval/" + self.brfa + "|BRFA]])", minor=True,
5477
botflag=True, force=True)
5578
print("Saved " + str(page.title()))
5679

5780

5881
class RemoveUnblocked(RemoveBase):
59-
def __init__(self, log_name, category, target, brfa):
82+
def __init__(self, log_name, category, target, backup_target, brfa):
6083
super(RemoveUnblocked, self).__init__(log_name, category)
6184
self.target = target
85+
self.backup_target = backup_target
6286
self.brfa = brfa
6387

6488
def run(self):
6589
for page in self.category:
6690
if page.title() == "Template:Uw-corpname":
6791
continue
92+
# print(page.title())
93+
if page.title()[:10] != "User talk:":
94+
print("NOT RIGHT FORMAT " + page.title())
95+
continue
6896
user = self.generate_user(page)
6997
if not user.isBlocked():
7098
self.log(page)
99+
old_t = page.text
71100
self.category_remove(self.target, page)
101+
if page.text == old_t:
102+
print("Move to backup")
103+
self.category_remove(self.backup_target, page)
72104
page.save(
73105
summary="Removing " + self.cat_name + " as user is unblocked." +
74106
" ([[Wikipedia:Bots/Requests for approval/" + self.brfa + "|BRFA]])", minor=True,
75107
botflag=True, force=True)
76-
print("Saved " + str(page.title()))
108+
print("Saved " + str(page.title()))

remove_blocked2.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from RemoveBase import RemoveBlocked
22

33
if __name__ == "__main__":
4-
rmBlocked = RemoveBlocked(log_name="block_removed_mar_14_2020.txt",
5-
category="[[:Category:Wikipedia usernames with possible policy issues]]",
4+
rmBlocked = RemoveBlocked(log_name="block_removed_apr_3_2020.txt",
5+
category="Wikipedia usernames with possible policy issues",
66
target="[[Category:Wikipedia usernames with possible policy issues|{{PAGENAME}}]]",
77
brfa="TheSandBot 6")
8-
rmBlocked.run()
8+
try:
9+
rmBlocked.run()
10+
except KeyboardInterrupt:
11+
print("\n")

0 commit comments

Comments
 (0)