Last active
January 4, 2016 10:49
-
-
Save DoctorWhooves/8611743 to your computer and use it in GitHub Desktop.
A script that will delete and protect pages on a MediaWiki based wiki from a textfile.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
from simplemediawiki import MediaWiki | |
import getpass | |
import sys | |
# Do these serve an actual purpose? | |
__author__ = '[[User:Jr Mime]]' | |
__version__ = '1.0' | |
# Print welcome message | |
print 'Welcome to the automated page protection & deletion system.\n' | |
# Gather user input | |
username = raw_input('Username: ') | |
password = raw_input('Password: ') | |
site = raw_input('Wiki sub-domain (community): ') | |
tfile = raw_input('Name of text file (foo.txt): ') | |
# Set url | |
wiki = MediaWiki('http://' + site + '.wikia.com/api.php') | |
# Login function | |
def login(username, password, wiki): | |
# Attempt login | |
if wiki.login(username, password) is True: | |
print 'Login successful!' | |
else: | |
print 'Login failed!' | |
sys.exit() | |
# Get edit token | |
def getToken(): | |
# Query string | |
query = { | |
'action': 'query', | |
'prop': 'info', | |
'intoken': 'edit', | |
'titles': 'Main_Page', | |
'format': 'json', | |
} | |
# Query | |
q = wiki.call(query)['query']['pages'] | |
for i in q: | |
# Try query | |
try: | |
wiki.token = q[i]['edittoken'] | |
return wiki.token | |
except: | |
wiki.token = None | |
print 'Failed to get delete token!' | |
return False | |
# Delete page | |
def delete(line, token): | |
# Query string | |
query = { | |
'action': 'delete', | |
'title': line, | |
'reason': 'Bot - deleting non-question material that has escaped the filter', | |
'format': 'json', | |
'token': token, | |
} | |
try: | |
# Attempt query | |
wiki.call(query) | |
except: | |
print 'Error' | |
else: | |
print 'Successfully deleted ' + line | |
# Protect page | |
def protect(line, token): | |
# Query string | |
query = { | |
'action': 'protect', | |
'title': line, | |
'reason': 'Bot - protecting non-question material that has escaped the filter', | |
'format': 'json', | |
'protections': 'create=sysop', | |
'expiry': 'infinite', | |
'token': token, | |
} | |
try: | |
# Attempt query | |
wiki.call(query) | |
except: | |
print 'Error' | |
else: | |
print 'Successfully protected ' + line | |
# Start processing textfile | |
def startProcess(tfile, token): | |
# Read file line by line | |
with open(tfile) as infile: | |
for line in infile: | |
# Delete page | |
delete(line, token) | |
# Protect page | |
protect(line, token) | |
# Login to the wiki | |
print 'Attempting to login to ' + site + '.\n' | |
login(username, password, wiki) | |
# Get edit token | |
print 'Attempting to fetch token.\n' | |
token = getToken() | |
# Start processing | |
startProcess(tfile, token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment