Purple exclamation mark.svg Planning the future of Botwiki! - Help us bring Botwiki up to date, contribute to our strategy discussion, add bot scripts, and contribute manuals, guides, and tutorials! Almost anything related to bots, particularly those used to edit mediawiki, is welcome.

Red exclamation mark.svg UNABLE TO EDIT? - We've experienced attacks by spambots lately and now require you to confirm your e-mail before you can edit (go to your preferences, enter an e-mail address, and request a confirmation e-mail, then go to your e-mail and click on the confirmation link). We also require new accounts to make a few edits and wait a few minutes before before you can create a page; however, if this is a problem contact us in #botwiki and we can manually confirm your account. Sorry for the inconvenience.

Python:Notoc.py

From Botwiki
Jump to: navigation, search
#!/usr/bin/python
# -*- coding: utf-8  -*-
 
"""
Bot written by Filnik to delete the __NOTOC__ in the pages that have it inside (working only for it.wiki).
"""
 
#
# (C) Filnik, 2007
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id: $'
#
 
import wikipedia
import re
 
def main():
    arg = wikipedia.handleArgs()
    pagename = 'Wikipedia:Elenchi_generati_offline/NOTOC'
    site = wikipedia.getSite()
    page = wikipedia.Page(site, pagename)
    # This page should exists always, it not there is a great bug.
    text = page.get()
    pagesToParse = re.findall(r'# ?\[\[(.*?)\]\]', text)
    for ParsePage in pagesToParse:
        wikipedia.output(u'Loading %s...' % ParsePage)
        for i in ['lista', 'cronologia', 'elenco', 'episodi']:
            if i.lower() in ParsePage.lower():
                wikipedia.output(u'%s in %s! Skip page!' % (i, ParsePage))
                continue
        ParsePage = wikipedia.Page(site, ParsePage)
        try:
            testo = ParsePage.get()
        except wikipedia.NoPage:
            wikipedia.output(u"%s doesn't exist, skip." % ParsePage.title())
            continue
        except wikipedia.IsRedirectPage:
            wikipedia.output(u"%s is a redirect, skip" % ParsePage.title())
            continue
        if '{{wikimedia:toc}}' in testo.lower():
            wikipedia.output(u'{{WikiMedia:Toc}} in text of %s, skip.' % ParsePage.title())
            continue
        newtext = re.sub(r'__NOTOC__', '', testo)
        if newtext != testo:
            ParsePage.put(newtext, 'Bot: Tolgo __NOTOC__ inutile')
        else:
            wikipedia.output(u'__NOTOC__ not found!')
            continue
 
if __name__ == "__main__":
    # Use try and finally, to put the wikipedia.stopme() always at the end of the code.
    try:
        main()
    finally:
        wikipedia.stopme()
Personal tools
Share