User:Maelgwnbot/source6
<source lang=python>
- -*- coding: utf-8 -*-
"""Maelgwns attempt at doing Wikipedia:Australian_Wikipedians%27_notice_board/AfD (AWNB) from Wikipedia:Wikiproject Deletion sorting/Australia. Changes to the header of the deletion sorting page should not cause problems but changes to the header and footer of the AWNB page may cause problems and changes to the main part is most likely to cause problems. Hence the bot stop option, that only works for this function."""
import re import wikipedia, catlib, pagegenerators, config
def main(): #get input options (the only one is always) # always, define a generator to understand if the user sets one, defining what's genFactory always = False; generator = False; genFactory = pagegenerators.GeneratorFactory()
# To prevent Infinite loops
errorCount = 0
# Loading the default options.
for arg in wikipedia.handleArgs(): if arg == '-always': always = True else: generator = genFactory.handleArg(arg)
#Check for shut-off stop = wikipedia.Page(config.mylang, 'User:Maelgwnbot/6/stop') stoptext = stop.get() #if not shut off then do it if stoptext == 'go ': #getting deletion sorting page delsort = wikipedia.Page(config.mylang, 'Wikipedia:WikiProject Deletion sorting/Australia') #(for testing only) #delsort = wikipedia.Page(config.mylang, 'User:Maelgwn/sandbox') text = delsort.get() #removing header text = wikipedia.replaceExcept(text, r'(?is).*?\n', ,) #removing comments text = wikipedia.replaceExcept(text, r'(?is)', , ) #changing headers as required text = wikipedia.replaceExcept(text, r'(?is)==Australia-related (?P<type>[^=]+?)( debates|)==', r'=====[[Wikipedia:\g<type>|\g<type>]]=====',) #changing transclusion to template[ text = wikipedia.replaceExcept(text, r'{{[Ww]ikipedia:Articles[ _]for[ _]deletion/', '* {{On AFD|', ) text = wikipedia.replaceExcept(text, r'{{[Ww]ikipedia:Miscellany[ _]for[ _]deletion/', '* {{On MFD|', ) #dealing with second noms text = wikipedia.replaceExcept(text, r'{{On AFD\|(?P<nom>(?P<article>.+?)( |)\(.{1,10} (nomination|nom)\))', '{{On AFD|\g<article>|\g<nom>', ) text = wikipedia.replaceExcept(text, r'{{On MFD\|(?P<nom>(?P<article>.+?)( |)\(.{1,10} (nomination|nom)\))', '{{On MFD|\g<article>|\g<nom>', ) #removing notes (and replacing with text = wikipedia.replaceExcept(text, r'==\nThe following Australian-related (?P<xfd>[^\']+?)D\'s are currently open for discussion:(?P<articles>[^=]+?)==','==\n\g<articles>\n----\n==',) text = wikipedia.replaceExcept(text, r'The following Australian-related articles are currently Proposed for Deletion:',,) text = wikipedia.replaceExcept(text, r'The following Australian-related Deletion reviews are currently open for discussion:',,) # text = wikipedia.replaceExcept(text, r'=====\[\[Wikipedia:Proposed Deletion nominations\|Proposed Deletion nominations\]\]=====', r'\n----\n=====Proposed Deletion=====',) text = wikipedia.replaceExcept(text, r'=====\[\[Wikipedia:Miscellany for deletion\|Miscellany for deletion\]\]=====', r'\n----\n=====Miscellany for deletion=====',) #text = wikipedia.replaceExcept(text, r'=====\[\[Wikipedia:Articles for Deletion\|Articles for Deletion\]\]=====', r'=====Articles for Deletion=====\n ',) text = wikipedia.replaceExcept(text, r'\:\'\'None at present\'\,, )
#start looking at AWNB page #(for testing only) #awnb = wikipedia.Page(config.mylang, 'User:Maelgwn/sandbox') awnb = wikipedia.Page(config.mylang, 'Wikipedia:Australian Wikipedians\' notice board/AfD') original = awnb.get()
#loading header and footer from AWNB AfD page header = wikipedia.replaceExcept(original, r'\n=====[^`]*', ,)
footer = wikipedia.replaceExcept(original, r'[^`]+?\n', '\n\n',)
#for testing #wikipedia.output('%s' %header) #wikipedia.output( '%s' % text) #wikipedia.output('%s' %footer)
text=header+text+footer #putting the new page # this if does not seem to work for some reason (a formatting glitch in my version pobably), but no edit is recorded if the pages are the same #copied from filnick at botwiki.sno.cc if original != text: # Ok, asking if the change has to be performed and do it. wikipedia.output(u"\n>>> \03{lightpurple}%s\03{default} <<<" % awnb.title()) wikipedia.showDiff(original, text) choice = while 1: if not always: choice = wikipedia.inputChoice(u'Do you want to accept these changes?', ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N') if choice.lower() in ['a', 'all']: always = True if choice.lower() in ['n', 'no']: break if choice.lower() in ['y', 'yes'] or always: #break try: awnb.put(text , 'Bot updating from Wikipedia:WikiProject Deletion sorting/Australia. Edit this to stop this job') except wikipedia.EditConflict: wikipedia.output(u'Edit conflict! skip!') break except wikipedia.ServerError: # Sometimes there is this error that's quite annoying because # can block the whole process for nothing. errorCount += 1 if errorCount < 5: wikipedia.output(u'Server Error! Wait..') time.sleep(3) continue else: # Prevent Infinite Loops raise wikipedia.ServerError(u'Fifth Server Error!') except wikipedia.SpamfilterError, e: wikipedia.output(u'Cannot change %s because of blacklist entry %s' % (page.title(), e.url)) break except wikipedia.PageNotSaved, error: wikipedia.output(u'Error putting page: %s' % (error.args,)) break except wikipedia.LockedPage: wikipedia.output(u'The page is still protected. Skipping...') break else: # Break only if the errors are one after the other errorCount = 0 break else: wikipedia.output(u'No changes to this page required') #if bot has been stopped, leave a note for owner, the problem being it will keep doing it every time it is run. elif stoptext != 'This task has already been stopped by another user and User:Maelgwn notified': talk=wikipedia.Page(config.mylang, 'User talk:Maelgwn') talktext=talk.get() talk.put(talktext+'\n\n==AWNB updater stopped==\nMZMcBride (talk) 05:40, 22 November 2019 (UTC)', 'Bot: AWNB updater stopped', minorEdit=False) stop.put('This task has already been stopped by another user and User:Maelgwn notified', 'Bot: knows it has been stopped, do not revert to this') else: wikipedia.output(u'Bot stopped and user already notified') if __name__ == "__main__":
try: main() finally: wikipedia.stopme()