Python:Redelete.py

From Botwiki

Jump to: navigation, search
#!/usr/bin/python
# -*- coding: utf-8  -*-
"""
This is a little Bot written by Filnik to delete useless redirect in wiktionary-it.
 
Indeed, in this project, all the pages must be with the lower-case letter and there are some redirect
from pages like "Anguria" to "anguria" that are useless. So, this Bot will delete *only* these pages.
 
Parameters:
 
-start          - Defing what pages use to start
 
Note:
 
Some code/ideas stolen from standardize_interwiki.py (but mainly my ideas that are in this code ;-))
 
FIXME: It will continue untill you block the bot so it can restart at the end.
"""
#
# (C) Filnik, 2007
#
# Distributed under the terms of the MIT license.
#
# Redirect-Delete Version: 1.0
#
 
import wikipedia, config
import time
 
# Defing the site
site = wikipedia.getSite()
# Defing the Bot's summary
com_dict = {
            'en':'Bot: Deleting useless redirect'
            'it':'Bot: Cancello redirect inutile'
            }
comment = wikipedia.translate(site, com_dict)
# Defing the default start page
start = '!'
 
# Load the default parameters and start
for arg in wikipedia.handleArgs():
    if arg.startswith('-start'):
        if len(arg) == 6:
            start = str(wikipedia.input(u'From what page do you want to start?'))
        else:
            start = str(arg[7:])
 
# What follow is the main part of the code.
try:
    # Loading all the pages of the wiki
    for pl in site.allpages(start):
        # [[Page]] => Page
        plname = pl.title()
        wikipedia.output(u'\nLoading %s...' % plname)
        # If the "Page" is a redirect, check if there is a "page" (P=>p) 
        if pl.isRedirectPage():
            checkpage = wikipedia.Page(site, plname.lower())# .lower().. can you guess what that thing do? :P
            if checkpage.isRedirectPage():
                wikipedia.output(u'Double redirect!')
                wikipedia.stopme()
            else:
                while 1:
                    # Deleting the useless redirect
                    result = pl.delete(comment, False)
                    # If result = True, means that the deletion is successfully, otherwise we can
                    # retry.
                    if result == True:
                        break
                    else:
                        time.sleep(10)
                        continue
        # The page isn't a redirect... ok, continue with the others
        else:
            wikipedia.output('%s is ok.' % plname)
            continue
finally:
    wikipedia.stopme()
Personal tools