User:Maelgwnbot/source6

From Wikipedia, the free encyclopedia

# -*- 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 <!--change to stop-->':
                #getting deletion sorting page
                delsort = wikipedia.Page(config.mylang, 'Wikipedia:WikiProject Deletion sorting/Australia')
                #(for testing only) delsort = wikipedia.Page(config.mylang, 'User:Maelgwn/botbot')
                text = delsort.get()
                #removing header
                text = wikipedia.replaceExcept(text, r'(?is)<noinclude>.*?</noinclude>\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'{{Wikipedia:Articles for deletion/', '* {{On AFD|', '')
                #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>', '')
                #removing notes (and replacing with <!-- Bot Start and Bot End stuff-->
                text = wikipedia.replaceExcept(text, r'==\nThe following Australian-related (?P<xfd>[^\']+?)D\'s are currently open for discussion:(?P<articles>[^=]+?)==','==\n<!-- Bot Start \g<xfd>d (Do not Remove) -->\g<articles><!-- Bot End \g<xfd>d (Do not Remove) -->\n----\n==','')
                text = wikipedia.replaceExcept(text, r'The following Australian-related Deletion review\'s are currently open for discussion:','','')
                #<!-- adding stuff that didnt get done above -->
                text = wikipedia.replaceExcept(text, r'=====\[\[Wikipedia:Miscellany for deletion\|Miscellany for deletion\]\]=====', r'<!-- Bot End Afd (Do not Remove) -->\n----\n=====[[Wikipedia:Miscellany for deletion|Miscellany for deletion]]=====','')
                text = wikipedia.replaceExcept(text, r'=====\[\[Wikipedia:Articles for Deletion\|Articles for Deletion\]\]=====', r'=====[[Wikipedia:Articles for Deletion|Articles for Deletion]]=====\n<!-- Example\n*Month DD - {{On AFD|Article Name}} details\n--> <!-- Bot Start Afd (Do not Remove) -->','')
 
                #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'[^`]+?</div>\n</div>', '\n</div>\n</div>','')
 
                #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 [[User:Maelgwnbot/6/stop|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==\n~~~~', '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()