User:Firefly/move/ubxbotcode
<source lang=python>
- !/usr/bin/python
- -*- coding: utf-8 -*-
import wikipedia
import re
import logerrors
from optparse import OptionParser
- Define the main function
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\}\}', '| |{{User:Richard0612/Userbox Archive/' + options.userbox + '}}
|User:Richard0612/Userbox Archive/' + options.userbox + ' |Usage |-\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()
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.