-
Notifications
You must be signed in to change notification settings - Fork 2
/
UpdateGroupJson.py
155 lines (131 loc) · 5.19 KB
/
UpdateGroupJson.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# ------------------------------------------------------------------------------
# Copyright (C) 2020 Michael Daniels
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# ------------------------------------------------------------------------------
import pywikibot
import json
from datetime import datetime, timezone
site = pywikibot.Site('en', 'wikipedia')
metawiki = pywikibot.Site('meta', 'meta')
def globalallusers(site, group):
"""
:param site: The site to get our data from.
:type site: pywikibot.site.APISite
:param group: The group to get.
:type group: str
:return: A pywikibot generator of all global users in the given global group.
:rtype: pywikibot.data.api.ListGenerator
"""
agugen = pywikibot.data.api.ListGenerator('globalallusers',
aguprop='groups', site=site)
if group:
agugen.request['agugroup'] = group
return agugen
def sortkeys(key: str) -> str:
"""
Sorts the user groups before saving them to the wiki.
@param key: User right
@type key: str
@return: Corresponding abbreviation
@rtype: str
"""
sortkeyDict = {
"sysop": "A",
"arbcom": "ARB",
"bureaucrat": "B",
"checkuser": "CU",
"oversight": "CV",
"interface-admin": "IA",
"abusefilter": "EFM",
"abusefilter-helper": "EFH",
"accountcreator": "ACC",
"autoreviewer": "AP",
"extendedmover": "PM",
"filemover": "FM",
"massmessage-sender": "MMS",
"patroller": "NPR",
"reviewer": "PCR",
"rollbacker": "RB",
"global-renamer": "GRe",
"global-rollbacker": "GRb",
"templateeditor": "TE",
"otrs-member": "OTRS",
"steward": "S"
}
return sortkeyDict[key]
combinedJsDataPage = pywikibot.Page(site, "User:MDanielsBot/markAdmins-Data.js")
combinedJsonDataPage = pywikibot.Page(site, "User:MDanielsBot/markAdmins-Data.json")
localGroups = ["abusefilter", "abusefilter-helper", "accountcreator",
"bureaucrat", "checkuser", "extendedmover", "filemover",
"interface-admin", "massmessage-sender", "oversight",
"sysop", "templateeditor"]
extraLocalGroups = ["autoreviewer", "patroller", "reviewer", "rollbacker"]
globalGroups = ["otrs-member", "steward", "global-rollbacker"]
metaGroups = ["global-renamer"]
arbcomJson = pywikibot.Page(site, "User:AmoryBot/crathighlighter.js/arbcom.json").get()
arbcom_members = json.loads(arbcomJson)
outputDict = {}
print(datetime.now(timezone.utc).strftime("%b %d %Y %H:%M:%S.%f") +
" -- Starting!", flush=True)
for group in localGroups:
for user in site.allusers(group=group):
if user['name'] in outputDict.keys():
outputDict[user['name']].append(group)
else:
outputDict[user['name']] = [group]
for group in globalGroups:
for user in globalallusers(site, group):
if user['name'] in outputDict.keys():
outputDict[user['name']].append(group)
else:
outputDict[user['name']] = [group]
for user in arbcom_members:
if user in outputDict.keys():
outputDict[user].append("arbcom")
else:
outputDict[user] = ["arbcom"]
for group in extraLocalGroups:
for user in site.allusers(group=group):
if user['name'] in outputDict.keys():
outputDict[user['name']].append(group)
else:
outputDict[user['name']] = [group]
for group in metaGroups:
for user in metawiki.allusers(group=group):
if user['name'] in outputDict.keys():
outputDict[user['name']].append(group)
else:
outputDict[user['name']] = [group]
print(datetime.now(timezone.utc).strftime("%b %d %Y %H:%M:%S.%f") +
" -- Computing output...", flush=True)
# Sort our flags
for item in outputDict:
outputDict[item].sort(key=sortkeys)
# Construct combined JSON page
pageTop = "mw.hook('userjs.script-loaded.markadmins').fire("
outputJson = json.dumps(outputDict, sort_keys=True,
indent=4, separators=(',', ': '), ensure_ascii=False)
pageBottom = ");"
newText = pageTop + outputJson + pageBottom
oldJspage = combinedJsDataPage.get()
oldJsonpage = combinedJsonDataPage.get()
if newText != oldJspage or outputJson != oldJsonpage:
print(datetime.now(timezone.utc).strftime("%b %d %Y %H:%M:%S.%f")
+ " -- Updated!", flush=True)
combinedJsDataPage.put(newText, "Update markadmins data")
combinedJsonDataPage.put(outputJson, "Update markadmins data")
else:
print(datetime.now(timezone.utc).strftime("%b %d %Y %H:%M:%S.%f")
+ " -- No changes", flush=True)