Python:SignChanger.py

From Botwiki

Jump to: navigation, search
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Bot for White Cat to fix his (many) signatures.
 
************************************** List of signatures ****************************************
 
--[[User:Coolcat|Cool Cat]] [[User talk:Coolcat|<sup>My Talk</sup>]] <-- Done 1 regex
--[[User:Coolcat|Cool Cat]]|[[User talk:Coolcat| My Talk]] <-- Done 1 regex
--[[User:Coolcat|Cool Cat]] <-- Done 1 regex
--[[User:Cool Cat|<small>Cool Cat</small>]] [[User talk:Cool Cat|<sup><small>Talk</small></sup>]] <-- Done 2 regex
--<small>[[User:Cool Cat|Cat]] [[User talk:Cool Cat|out]]</small> <-- Done 2 regex
--<small>[[:en:User:Cool Cat|Cat]] [[:en:User talk:Cool Cat|out]]</small> <-- Done 3 regex
--<small> [[User:Cool Cat|Cat]]</small> <sup>[[User talk:Cool Cat|Chi?]]</sup> <-- Done 3 regex
--<small> [[User:White Cat|Cat]]</small> <sup>[[User talk:White Cat|chi?]]</sup> <-- Done 3 regex
--<small>[[User:Cool Cat|Cool Cat]]<sup>[[User talk:Cool Cat|Talk]]|[[Special:Emailuser/Cool Cat|@]]</sup></small> <-- Done 4 regex
--<small> [[User:White Cat/sig|Cat]]</small> <sup>[[User talk:White Cat/sig|chi?]]</sup> <-- Done 5 regex
<small> [[User:White Cat/07|Cat]]</small> <sup>[[User talk:White Cat/07|chi?]]</sup> <-- Done 6 regex
<small> [[User:White Cat/06|Cat]]</small> <sup>[[User talk:White Cat/06|chi?]]</sup> <-- Done 6 regex
<small> [[User:White Cat/05|Cat]]</small> <sup>[[User talk:White Cat/05|chi?]]</sup> <-- Done 6 regex
 
**************************************************************************************************
 
"""
 
# License: MIT
# Author: Filnik
 
import wikipedia, re
 
def fixsignature(text):
    #15:34, 17 Feb 2005 (UTC)
    # 1 regex
    text = re.sub(r'-?-?\[\[[Uu]ser:[Cc]oolcat\|[Cc]ool [Cc]at\]\](?:(?: |\|)\[\[[Uu]ser talk:[Cc]oolcat\|(?:<sup>|) ?[Mm]y [Tt]alk(?:</sup>|)\]\]|)( \d\d:\d\d, \d\d (?:.*?) \d\d\d(\d) \((.*?)\))', \
                   r'--<small>[[User:White Cat/0\2|Cat]]</small> <sup>[[User talk:White Cat/0\2|chi?]]</sup>\1', text)
    # 2 regex
    text = re.sub(r'-?-?(?:<small>|) ?\[\[[Uu]ser:[Cc]ool ?[Cc]at\|(?:<small>|)(?:[Cc]ool |)[Cc]at(?:</small>|)\]\] ?\[\[[Uu]ser talk:[Cc]ool ?[Cc]at\|(?:(?:</small>|)<sup>(?:<small>)(?:[Mm]y |)[Tt]alk(?:</small>|)</sup>(?:</small>|)|[oO]ut)\]\](?:</small>|)( \d\d:\d\d, \d\d (?:.*?) \d\d\d(\d) \((.*?)\))', \
                   r'--<small>[[User:White Cat/0\2|Cat]]</small> <sup>[[User talk:White Cat/0\2|chi?]]</sup>\1', text)
    # 3 regex
    text = re.sub(r'-?-?(?:<small>|) ?\[\[(?::?en:|)[Uu]ser:(?:[Cc]ool|[Ww]hite) [Cc]at\|[Cc]at\]\](?:</small>|) ?(?:<sup>|)\[\[(?::?en:|)[Uu]ser talk:(?:[Cc]ool|[Ww]hite) [Cc]at\|(?:out|[cC]hi\?)\]\](?:</small>|</sup>)( \d\d:\d\d, \d\d (?:.*?) \d\d\d(\d) \((.*?)\))', \
                   r'--<small> [[User:White Cat/0\2|Cat]]</small> <sup>[[User talk:White Cat/0\2|chi?]]</sup>\1', text)
    # 4 regex
    text = re.sub(r'-?-?<small>\[\[[Uu]ser:[Cc]ool [Cc]at\|[Cc]ool [Cc]at\]\] ?<sup> ?\[\[[Uu]ser talk:[Cc]ool [Cc]at\|[Tt]alk\]\] ?\| ?\[\[[Ss]pecial:[Ee]mailuser/[Cc]ool [Cc]at\|@\]\]</sup></small>( \d\d:\d\d, \d\d (?:.*?) \d\d\d(\d) \((.*?)\))', \
                   r'--<small>[[User:White Cat/0\2|Cat]]</small> <sup>[[User talk:White Cat/0\2|chi?]]</sup>\1', text)
    # 5 regex
    text = re.sub(r'-?-?<small> ?\[\[[Uu]ser:[Ww]hite [Cc]at/sig\|[Cc]at\]\]</small> ?<sup>\[\[[Uu]ser talk:[Ww]hite [Cc]at/sig\|[Cc]hi\?\]\]</sup>( \d\d:\d\d, \d\d (?:.*?) \d\d\d(\d) \((.*?)\))', \
                   r'--<small>[[User:White Cat/0\2|Cat]]</small> <sup>[[User talk:White Cat/0\2|chi?]]</sup>\1', text)  
    # 6 regex
    text = re.sub(r'([^-])<small> ?\[\[[Uu]ser:[Ww]hite [Cc]at/0[5-7]\|[Cc]at\]\]</small> ?<sup>\[\[[Uu]ser talk:[Ww]hite [Cc]at/0[5-7]\|[Cc]hi\?\]\]</sup>( \d\d:\d\d, \d\d (?:.*?) \d\d\d(\d) \((.*?)\))', \
                   r'\1--<small>[[User:White Cat/0\3|Cat]]</small> <sup>[[User talk:White Cat/0\3|chi?]]</sup>\2', text)
    return text
 
if __name__ == "__main__":
    try:
        for arg in wikipedia.handleArgs():
            pass
        site = wikipedia.getSite()
        page = wikipedia.Page(site, 'User:White Cat')
        page2 = wikipedia.Page(site, 'User:Cool Cat')
        acceptall = False
        stopme = False
        while 1:
            for pagetofix in page.getReferences():
                try:
                    text = pagetofix.get()
                except wikipedia.NoPage():
                    wikipedia.output(u"%s doesn't exist!" % pagetofix.title())
                    continue
                except wikipedia.IsRedirectPage():
                    wikipedia.output(u"%s it's a redirect!" % pagetofix.title())
                    continue
                newtext = fixsignature(text)
                if text != newtext:
                    wikipedia.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % pagetofix.title())
                    wikipedia.showDiff(text, newtext)
                    if not acceptall:
                        choice = wikipedia.inputChoice(u'Do you want to accept these changes?', ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
                        if choice in ['a', 'A']:
                            acceptall = True
                    if acceptall or choice in ['y', 'Y']:
                        try:
                            pagetofix.put(newtext, 'Bot: Updating the signature')
                        except wikipedia.EditConflict:
                            wikipedia.output(u'Skipping %s because of edit conflict' % (page.title(),))
                        except wikipedia.SpamfilterError, e:
                            wikipedia.output(u'Cannot change %s because of blacklist entry %s' % (page.title(), e.url))
                        except wikipedia.LockedPage:
                            wikipedia.output(u'Skipping %s (locked page)' % (page.title(),))
                else:
                    wikipedia.output(u'No changes on page: %s' % pagetofix.title())
                    continue
            if stopme == True:
                break
            else:
                page = page2
                stopme = True
                continue
    finally:
        wikipedia.stopme()
Personal tools