diff --git a/main.py b/main.py index fbadf42..efd3459 100644 --- a/main.py +++ b/main.py @@ -1,14 +1,14 @@ -import time -import requests -import stdiomask -import sys +from time import sleep +from requests import Session +from stdiomask import getpass +from sys import argv, exit from random import getrandbits # Define the functions here def remove(): - time.sleep(2) + sleep(2) input("Press enter to continue or ctrl+c to quit") file = open('userinfo.cfg', 'r') userdata = file.read() @@ -34,7 +34,7 @@ def remove(): 'User-Agent': 'BOT: ' + opusername + '@TestWikiAutoInactive-v1rc1', 'From': fromheader } - S = requests.Session() + S = Session() URL = "https://publictestwiki.com/w/api.php" # Step 1: Retrieve a login token PARAMS_1 = { @@ -48,8 +48,8 @@ def remove(): LOGIN_TOKEN = DATA["query"]["tokens"]["logintoken"] # Step 2: Send a post request to log in. See # https://www.mediawiki.org/wiki/Manual:Bot_passwords - time.sleep(5) # wait 5s to avoid throttling - password = stdiomask.getpass() + sleep(5) # wait 5s to avoid throttling + password = getpass() PARAMS_2 = { "action": "login", "lgname": username, @@ -88,7 +88,7 @@ def remove(): username + ',') configfile.close() - time.sleep(5) # hold for 5s to avoid throttling + sleep(5) # hold for 5s to avoid throttling # Step 3: Obtain a Userrights token PARAMS_3 = { "action": "query", @@ -107,11 +107,11 @@ def remove(): usertemp = input("User to remove: ") userlist.append(usertemp) count = count + 1 - time.sleep(0.5) + sleep(0.5) count = 0 while count < len(userlist): inactiveuser = userlist[count] - time.sleep(10) # wait 10 seconds before write api + sleep(10) # wait 10 seconds before write api # Step 4: Request to add or remove a user from a group PARAMS_4 = { "action": "userrights", @@ -124,16 +124,16 @@ def remove(): R = S.post(URL, data=PARAMS_4, headers=headers) DATA = R.json() print(DATA) - time.sleep(2) + sleep(2) print('Generating mass message text..') print('{{subst:Inactivity|user=' + opusername + '}}') - time.sleep(5) + sleep(5) print("Thanks for using! Good bye.") - sys.exit() + exit() def notify(): - time.sleep(10) + sleep(10) consul = input("What is your username? ") removedate = input("Removal date: ") date = input("Today's Date: ") @@ -144,20 +144,20 @@ def notify(): usertemp = input("User to remove: ") userlist.append(usertemp) count = count + 1 - time.sleep(0.5) + sleep(0.5) print("Generating mass message list....") - time.sleep(2) + sleep(2) count = 0 while count < len(userlist): print("User Talk:" + str(userlist[count])) count = count + 1 - time.sleep(0.5) + sleep(0.5) print("Generating mass message text....") - time.sleep(2) + sleep(2) print("{{subst:InactiveReminder|DATE=" + removedate + "|sig=~~~ for [[User:" + consul + "|" + consul + "]]}}") print("Generating Community Noticeboard post") - time.sleep(2) + sleep(2) print("==Inactive Rights Removal - " + date + "==") print( "The rights of the following users will be removed on or after " + @@ -172,23 +172,23 @@ def notify(): print(":~~~") print(":For the Consul Team") print(":~~~~~") - time.sleep(5) + sleep(5) print("Thanks for using! Good bye.") try: - if sys.argv[1] == 'remove': + if argv[1] == 'remove': print("Running Script in Remove Mode") print("Welcome to the TestWiki:Inactivity Script") print("This script may only be used by consuls") print("Please ensure notifications were sent > 7 days ago and the users are still inacitve") remove() - elif sys.argv[1] == 'notify': + elif argv[1] == 'notify': print("Running Script in Notify Mode") print("Before we begin, please run the findInactive script on https://publictestwiki.com") print("The notification process will begin in 10 seconds") notify() - elif sys.argv[1] == 'help': + elif argv[1] == 'help': print("Commands are:") print("remove - Removes rights from inactive users") print("notify - Generates messages for inactive users") @@ -198,4 +198,4 @@ def notify(): except IndexError as e: print(e) print("Please specify an action (remove, notify)") - sys.exit() + exit()