User:Gdr/protect.py

See also Bot policy and the Python-based tool editing tool Using the python wikipediabot.

#!/usr/bin/python
 * 1)              PROTECT.PY -- PROTECT AND UNPROTECT PAGES
 * 2)                           Gdr, 2005-05-12
 * 3) INTRODUCTION
 * 4) This module implements automatic protection and unprotection of
 * 5) wikipedia pages. It is designed to be used with the Python Wikipedia
 * 6) Robot Framework (http://sourceforge.net/projects/pywikipediabot/).
 * 7) USAGE
 * 8) You must use login.py to log in as a user with administrator
 * 9) privileges in order for this module to be effective. Then you can use
 * 10) it like this:
 * 11)   import protect
 * 12)   protect.protectPage(pagelink, 'reason for protecting')
 * 13)   protect.unprotectPage(pagelink, 'reason for unprotecting')
 * 14) LICENCES
 * 15) This software can be used and reproduced under the terms and
 * 16) conditions of the Python Software Foundation license under which
 * 17) recent copies of the python-2.3 interpreter can be used.
 * 18) This program is free software; you can redistribute it and/or modify
 * 19) it under the terms of the GNU General Public License as published by
 * 20) the Free Software Foundation; either version 2 of the License, or (at
 * 21) your option) any later version.
 * 1)   protect.unprotectPage(pagelink, 'reason for unprotecting')
 * 2) LICENCES
 * 3) This software can be used and reproduced under the terms and
 * 4) conditions of the Python Software Foundation license under which
 * 5) recent copies of the python-2.3 interpreter can be used.
 * 6) This program is free software; you can redistribute it and/or modify
 * 7) it under the terms of the GNU General Public License as published by
 * 8) the Free Software Foundation; either version 2 of the License, or (at
 * 9) your option) any later version.
 * 1) This program is free software; you can redistribute it and/or modify
 * 2) it under the terms of the GNU General Public License as published by
 * 3) the Free Software Foundation; either version 2 of the License, or (at
 * 4) your option) any later version.

import re import wikipedia import time

def applyAction(page, action, predata): """Apply 'action' to page 'name' on 'site'.""" import httplib # Check whether we are not too quickly after the previous putPage, and # wait a bit until the interval is acceptable wikipedia.put_throttle # Which web-site host are we submitting to? host = page.site.hostname # Get the address of the page on that host. address = '/w/wiki.phtml?title=%s&action=%s'%(page.urlname,action)

# Get the page and scan it for the edittoken. text, charset = wikipedia.getUrl(host, address, page.site) m = re.search('value=[\'"]([0-9a-z]+)[\'"] name=[\'"]wpEditToken[\'"]',text) if m:       token = m.group(1) else: m = re.search('name=[\'"]wpEditToken[\'"] value=[\'"]([0-9a-z]+)[\'"]',text) if m:           token = m.group(1) else: token = "0" print "token = ", token

predata.append(('wpEditToken', token)) data = wikipedia.urlencode(tuple(predata))

wikipedia.output(wikipedia.url2unicode("Apply %s to page %s:%s"%(action,page.site.hostname,page.urlname), site = page.site))

# Submit the prepared information now = time.time conn = httplib.HTTPConnection(host)

conn.putrequest("POST", address) conn.putheader('Content-Length', str(len(data))) conn.putheader("Content-type", "application/x-www-form-urlencoded") conn.putheader("User-agent", "PythonWikipediaBot/1.0") if page.site.cookies: conn.putheader('Cookie', page.site.cookies) conn.endheaders conn.send(data)

# Prepare the return values response = conn.getresponse data = response.read conn.close wikipedia.put_throttle.setDelay(time.time - now) return response.status, response.reason, data

def protectPage(page, reason = None, moveonly = False): """Protect 'page'.""" wpmoveonly = "0" if moveonly: wpmoveonly = "1" return applyAction(page, 'protect',                      [('wpReasonProtect', reason),                        ('wpConfirmProtect', '1'),                        ('wpMoveOnly', wpmoveonly),                        ('wpConfirmProtectB', 'Confirm')])

def unprotectPage(page, reason): """Unprotect 'page'.""" return applyAction(page, 'unprotect',                      [('wpReasonProtect', reason),                        ('wpConfirmProtect', '1'),                        ('wpConfirmProtectB', 'Confirm')])