User:Firefly/move/ubxbotcode


 * 1) !/usr/bin/python
 * 2) -*- coding: utf-8  -*-

import wikipedia import re import logerrors from optparse import OptionParser def main: parser = OptionParser parser.add_option('-u', '--userbox', dest='userbox') parser.add_option("-m", action="store_true", dest="movepage", default = False) parser.add_option("-a", action="store_true", dest="archive", default = False) parser.add_option("-s", dest="section") (options, args) = parser.parse_args site = wikipedia.getSite page = wikipedia.Page(site, 'Template:' + options.userbox) babel = options.userbox.replace('User ', '') if options.movepage: try: page.move('User:Richard0612/Userbox Archive/' + options.userbox, reason='Bot: Userfying userbox per WP:UBM', leaveRedirect=False) except wikipedia.Error: wikipedia.output('Error: ' + wikipedia.Error) logerrors.loguserbox(page, 'Could not move page, perhaps it is protected or nonexistent?', options.userbox) for userpage in page.getReferences(follow_redirects=False): try: text = userpage.get # text = page.get <-- is the same except wikipedia.NoPage: # First except, prevent empty pages wikipedia.output(u'Aah! Page does not exist!') logerrors.loguserbox(userpage, 'Page does not exist', options.userbox) text = '' except wikipedia.IsRedirectPage: # second except, prevent redirect wikipedia.output(u'%s is a redirect!' % page.title) logerrors.loguserbox(userpage, 'Page is a redirect') text = '' except wikipedia.Error: # third exception, take the problem and print wikipedia.output(u"Some Error, skipping..") logerrors.loguserbox(userpage, 'Unknown error (bug?)', options.userbox) text = '' re.IGNORECASE text = re.sub('(\{\{|\|)(Template:)?' + options.userbox + '\}\}', '\g<1>User:Richard0612/Userbox Archive/' + options.userbox + '}}', text) text = re.sub('(\{\{|\|)(Template:)?' + options.userbox.replace(' ', '_') + '}}', '\g<1>User:Richard0612/Userbox Archive/' + options.userbox + '}}', text) text = re.sub('\|[ \r\n]*' + babel, '\|:Richard0612/Userbox Archive/' + options.userbox, text) text = re.sub('\|[ \r\n]*' + babel.replace(' ', '_'), '\|:Richard0612/Userbox Archive/' + options.userbox, text) userpage.put(text, comment='Bot: Updating transclusion of userbox ' + options.userbox + ' per WP:UBM') if options.archive: try: arch = wikipedia.Page(site, 'User:Richard0612/Userbox Archive/' + options.section) archtext = arch.get archtext = re.sub('\{\{usbkbottom\}\}', '\n', archtext) arch.put(archtext, 'Bot: Adding userbox ' + options.userbox) except wikipedia.Error: wikipedia.output('Archival error') logerrors.loguserbox(arch, 'Unknown archival error', options.userbox) if __name__ == '__main__': try: main finally: wikipedia.stopme
 * 1) Define the main function