Skip to content

Commit 6931713

Browse files
RhinosF1examknow
andauthored
Handle POST headers better (#20)
* Handle POST headers better * Update main.py * Create userinfo.cfg * Update userinfo.cfg * Update main.py * Update main.py * Update main.py * needs to be handled at least * make that better * more specific * revert last * upping sleep times * The user touching userinfo.cfg is a bad thing * incorrect * Update .travis.yml * Update main.py ran 'me@mypc PublicTestWiki-Inactive-Auto-post-header-automation % autopep8 --aggressive --aggressive -v --in-place main.py' * Update .travis.yml * Update .travis.yml * Update main.py * bumping to rc1 Co-authored-by: Examknow <ek311@outlook.com>
1 parent 919ac15 commit 6931713

File tree

3 files changed

+185
-134
lines changed

3 files changed

+185
-134
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ before_script:
1818
- docker exec testenv pip install -r requirements.txt
1919
- docker exec testenv python --version
2020
script:
21-
# we ignore E402 because of workarounds we use for modules loading, and F401 because some imports aren't directly used but needed for modules and travis doesn't like that
22-
- docker exec testenv flake8 modules --ignore E402,F401,W503 --max-line-length 210
21+
- docker exec testenv flake8 main.py --ignore W503,W504 --max-line-length 210
2322
notifications:
2423
irc:
2524
channels:

main.py

Lines changed: 183 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -2,149 +2,200 @@
22
import requests
33
import stdiomask
44
import sys
5+
from random import getrandbits
56

67
# Define the functions here
7-
def remove():
8-
time.sleep(2)
9-
input("Press enter to continue or ctrl+c to quit")
10-
users = input("How many users are being removed? ")
11-
userlist = []
12-
count = 0
13-
while count < int(users):
14-
usertemp = input("User to remove: ")
15-
userlist.append(usertemp)
16-
count = count + 1
17-
time.sleep(0.5)
18-
fromheader = input("Your Email: ")
19-
headers = {
20-
'User-Agent': 'PublicTestWikiInactiveAuto-github/rhinosf1-fortestwikiconusls',
21-
'From': fromheader
22-
}
23-
S = requests.Session()
24-
URL = "https://test.miraheze.org/w/api.php"
25-
# Step 1: Retrieve a login token
26-
PARAMS_1 = {
27-
"action": "query",
28-
"meta": "tokens",
29-
"type": "login",
30-
"format": "json"
31-
}
32-
R = S.get(url=URL, params=PARAMS_1, headers=headers)
33-
DATA = R.json()
34-
LOGIN_TOKEN = DATA["query"]["tokens"]["logintoken"]
35-
# Step 2: Send a post request to log in. See
36-
# https://www.mediawiki.org/wiki/Manual:Bot_passwords
37-
time.sleep(1) #wait 1s to avoid throttling
38-
username = input("Username: ")
39-
password = stdiomask.getpass()
40-
PARAMS_2 = {
41-
"action": "login",
42-
"lgname": username,
43-
"lgpassword": password,
44-
"lgtoken": LOGIN_TOKEN,
45-
"format": "json"
46-
}
47-
R = S.post(URL, data=PARAMS_2, headers=headers)
48-
time.sleep(1) #hold for 1s to avoid throttling
49-
# Step 3: Obtain a Userrights token
50-
PARAMS_3 = {
51-
"action": "query",
52-
"format": "json",
53-
"meta": "tokens",
54-
"type": "userrights"
55-
}
56-
R = S.get(url=URL, params=PARAMS_3, headers=headers)
57-
DATA = R.json()
588

59-
USERRIGHTS_TOKEN = DATA["query"]["tokens"]["userrightstoken"]
609

61-
count = 0
62-
while count < len(userlist):
63-
inactiveuser = userlist[count]
64-
time.sleep(5) #wait 5 seconds before write api
65-
# Step 4: Request to add or remove a user from a group
66-
PARAMS_4 = {
67-
"action": "userrights",
10+
def remove():
11+
time.sleep(2)
12+
input("Press enter to continue or ctrl+c to quit")
13+
file = open('userinfo.cfg', 'r')
14+
userdata = file.read()
15+
file.close()
16+
try:
17+
if userdata == '-':
18+
opusername = input("Operator Username: ")
19+
fromheader = input("Bot Email: ")
20+
else:
21+
userdata.split(',')
22+
opusername = userdata[1]
23+
fromheader = userdata[2]
24+
username = userdata[3]
25+
log = input("Logged in to: " + str(userdata) + " - Confirm? Y/N: ")
26+
if log == "N":
27+
opusername = input("Operator Username: ")
28+
fromheader = input("Bot Email: ")
29+
username = input("Bot Username: ")
30+
except IndexError:
31+
opusername = input("Operator Username: ")
32+
fromheader = input("Bot Email: ")
33+
headers = {
34+
'User-Agent': 'BOT: ' + opusername + '@TestWikiAutoInactive-v1rc1',
35+
'From': fromheader
36+
}
37+
S = requests.Session()
38+
URL = "https://publictestwiki.com/w/api.php"
39+
# Step 1: Retrieve a login token
40+
PARAMS_1 = {
41+
"action": "query",
42+
"meta": "tokens",
43+
"type": "login",
44+
"format": "json"
45+
}
46+
R = S.get(url=URL, params=PARAMS_1, headers=headers)
47+
DATA = R.json()
48+
LOGIN_TOKEN = DATA["query"]["tokens"]["logintoken"]
49+
# Step 2: Send a post request to log in. See
50+
# https://www.mediawiki.org/wiki/Manual:Bot_passwords
51+
time.sleep(5) # wait 5s to avoid throttling
52+
password = stdiomask.getpass()
53+
PARAMS_2 = {
54+
"action": "login",
55+
"lgname": username,
56+
"lgpassword": password,
57+
"lgtoken": LOGIN_TOKEN,
58+
"format": "json"
59+
}
60+
# destroy the password + replace with random hash
61+
password = getrandbits(125)
62+
R = S.post(URL, data=PARAMS_2, headers=headers)
63+
PARAMS_AUTH = {
64+
"action": "query",
6865
"format": "json",
69-
"user": inactiveuser,
70-
"remove": "bot|sysop|bureaucrat|consul|testgroup|autopatrolled|confirmed|rollbacker|interface-admin|flow-bot|checkuser|interwiki-admin|oversight|steward",
71-
"reason": "per [[TestWiki:Inactivity|Inactivity report]]",
72-
"token": USERRIGHTS_TOKEN
66+
"meta": "userinfo",
67+
"uiprop": "email"
7368
}
74-
count = count + 1
75-
76-
R = S.post(URL, data=PARAMS_4)
69+
authres = S.post(URL, data=PARAMS_AUTH, headers=headers)
70+
EMAIL = authres.json()
71+
EMAIL = EMAIL["query"]["userinfo"]["email"]
72+
if fromheader == EMAIL:
73+
print("Email Authenticated!")
74+
else:
75+
fromheader = EMAIL
76+
print("Your email was replaced with " + fromheader)
77+
headers = {
78+
'User-Agent': 'BOT: ' + opusername + '@TestWikiAutoInactive-v1rc1',
79+
'From': fromheader # rewrite header to user email
80+
}
81+
configfile = open('userinfo.cfg', 'w+')
82+
configfile.write(
83+
',' +
84+
opusername +
85+
',' +
86+
fromheader +
87+
',' +
88+
username +
89+
',')
90+
configfile.close()
91+
time.sleep(5) # hold for 5s to avoid throttling
92+
# Step 3: Obtain a Userrights token
93+
PARAMS_3 = {
94+
"action": "query",
95+
"format": "json",
96+
"meta": "tokens",
97+
"type": "userrights"
98+
}
99+
R = S.get(url=URL, params=PARAMS_3, headers=headers)
77100
DATA = R.json()
78101

79-
print(DATA)
80-
time.sleep(2)
81-
print('Generating mass message text..')
82-
print('{{subst:Inactivity|user='+username+'}}')
83-
time.sleep(5)
84-
print("Thanks for using! Good bye.")
85-
sys.exit()
86-
87-
def notify():
88-
time.sleep(10)
89-
consul = input("What is your username? ")
90-
removedate = input("Removal date: ")
91-
date = input("Today's Date: " )
92-
users = input("How many users are being removed? ")
93-
userlist = []
94-
count = 0
95-
while count < int(users):
96-
usertemp = input("User to remove: ")
97-
userlist.append(usertemp)
98-
count = count + 1
99-
time.sleep(0.5)
100-
print("Generating mass message list....")
101-
time.sleep(2)
102-
count = 0
103-
while count < len(userlist):
104-
print("User Talk:" + str(userlist[count]))
105-
count = count + 1
106-
time.sleep(0.5)
107-
print("Generating mass message text....")
108-
time.sleep(2)
109-
print("{{subst:InactiveReminder|DATE=" + removedate + "|sig=~~~ for [[User:"+consul +"|"+consul + "]]}}")
110-
print("Generating Community Noticeboard post")
111-
time.sleep(2)
112-
print("==Inactive Rights Removal - "+ date + "==")
113-
print("The rights of the following users will be removed on or after " + removedate + " if they do not return to activity:")
114-
count = 0
115-
while count < len(userlist):
116-
print("*{{RFP/User|"+userlist[count]+"}}")
102+
USERRIGHTS_TOKEN = DATA["query"]["tokens"]["userrightstoken"]
103+
users = input("How many users are being removed? ")
104+
userlist = []
105+
count = 0
106+
while count < int(users):
107+
usertemp = input("User to remove: ")
108+
userlist.append(usertemp)
117109
count = count + 1
118-
print("")
119-
print("Thanks,")
120-
print(":~~~")
121-
print(":For the Consul Team")
122-
print(":~~~~~")
123-
time.sleep(5)
124-
print("Thanks for using! Good bye.")
110+
time.sleep(0.5)
111+
count = 0
112+
while count < len(userlist):
113+
inactiveuser = userlist[count]
114+
time.sleep(10) # wait 10 seconds before write api
115+
# Step 4: Request to add or remove a user from a group
116+
PARAMS_4 = {
117+
"action": "userrights",
118+
"format": "json",
119+
"user": inactiveuser,
120+
"remove": "bot|sysop|bureaucrat|consul|testgroup|autopatrolled|confirmed|rollbacker|interface-admin|flow-bot|checkuser|interwiki-admin|oversight|steward",
121+
"reason": "per [[TestWiki:Inactivity|Inactivity report]]",
122+
"token": USERRIGHTS_TOKEN}
123+
count = count + 1
124+
R = S.post(URL, data=PARAMS_4, headers=headers)
125+
DATA = R.json()
126+
print(DATA)
127+
time.sleep(2)
128+
print('Generating mass message text..')
129+
print('{{subst:Inactivity|user=' + opusername + '}}')
130+
time.sleep(5)
131+
print("Thanks for using! Good bye.")
132+
sys.exit()
125133

126134

135+
def notify():
136+
time.sleep(10)
137+
consul = input("What is your username? ")
138+
removedate = input("Removal date: ")
139+
date = input("Today's Date: ")
140+
users = input("How many users are being removed? ")
141+
userlist = []
142+
count = 0
143+
while count < int(users):
144+
usertemp = input("User to remove: ")
145+
userlist.append(usertemp)
146+
count = count + 1
147+
time.sleep(0.5)
148+
print("Generating mass message list....")
149+
time.sleep(2)
150+
count = 0
151+
while count < len(userlist):
152+
print("User Talk:" + str(userlist[count]))
153+
count = count + 1
154+
time.sleep(0.5)
155+
print("Generating mass message text....")
156+
time.sleep(2)
157+
print("{{subst:InactiveReminder|DATE=" + removedate +
158+
"|sig=~~~ for [[User:" + consul + "|" + consul + "]]}}")
159+
print("Generating Community Noticeboard post")
160+
time.sleep(2)
161+
print("==Inactive Rights Removal - " + date + "==")
162+
print(
163+
"The rights of the following users will be removed on or after " +
164+
removedate +
165+
" if they do not return to activity:")
166+
count = 0
167+
while count < len(userlist):
168+
print("*{{RFP/User|" + userlist[count] + "}}")
169+
count = count + 1
170+
print("")
171+
print("Thanks,")
172+
print(":~~~")
173+
print(":For the Consul Team")
174+
print(":~~~~~")
175+
time.sleep(5)
176+
print("Thanks for using! Good bye.")
127177

128178

129179
try:
130-
if sys.argv[1] == 'remove':
131-
print("Running Script in Remove Mode")
132-
print("Welcome to the TestWiki:Inactivity Script")
133-
print("This script may only be used by consuls")
134-
print("Please ensure notifications were sent > 7 days ago and the users are still inacitve")
135-
remove()
136-
if sys.argv[1] == 'notify':
137-
print("Running Script in Notify Mode")
138-
print("Before we begin, please run the findInactive script on https://publictestwiki.com")
139-
print("The notification process will begin in 10 seconds")
140-
notify()
141-
if sys.argv[1] == 'help':
142-
print("Commands are:")
143-
print("remove - Removes rights from inactive users")
144-
print("notify - Generates messages for inactive users")
145-
print("help - Displays this help page")
146-
else:
147-
print("Unknown command. For help use 'main.py help'.")
148-
except IndexError:
149-
print("Please specify an action (remove, notify)")
150-
sys.exit()
180+
if sys.argv[1] == 'remove':
181+
print("Running Script in Remove Mode")
182+
print("Welcome to the TestWiki:Inactivity Script")
183+
print("This script may only be used by consuls")
184+
print("Please ensure notifications were sent > 7 days ago and the users are still inacitve")
185+
remove()
186+
elif sys.argv[1] == 'notify':
187+
print("Running Script in Notify Mode")
188+
print("Before we begin, please run the findInactive script on https://publictestwiki.com")
189+
print("The notification process will begin in 10 seconds")
190+
notify()
191+
elif sys.argv[1] == 'help':
192+
print("Commands are:")
193+
print("remove - Removes rights from inactive users")
194+
print("notify - Generates messages for inactive users")
195+
print("help - Displays this help page")
196+
else:
197+
print("Unknown command. For help use 'main.py help'.")
198+
except IndexError as e:
199+
print(e)
200+
print("Please specify an action (remove, notify)")
201+
sys.exit()

userinfo.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-

0 commit comments

Comments
 (0)