Ayuda:Cómo crear un bot/bloqueador

De Wikipedia, la enciclopedia libre

bloqueador.py[editar]

#!/usr/bin/python
# -*- coding: utf-8  -*-
#
# (C) Pywikipedia bot team, 2006-2011
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id: bloqueador.py 0001 2011-11-11 $'
#

import wikipedia as pywikibot
import pagegenerators
from pywikibot import i18n

import wikipedia as pywikibot
import userlib, query


class BlockBot:
    def __init__(self, dry):
        self.site = pywikibot.getSite()
        self.dry = dry
        self.info = None
        self.parts = None


    def run(self):
        f=open("bloqueos.txt", "r")
        for x in f.readlines():
                user=x.strip()
                if len(user)<1: continue
                print "Bloqueando ", user
                self.treat(user)
        return

    def treat(self, usuario):
        """
        Blocks the given user
        """
        user = userlib.User(self.site, usuario)
        #print user.isBlocked()
        user.block(expiry="1 year", reason="Proxy", anon=True)

def main():
    # If dry is True, doesn't do any real changes, but only show
    # what would have been changed.
    dry = show = False

    # Parse command line arguments
    for arg in pywikibot.handleArgs():
        if arg == "-dry":
            dry = True
        else:
            show = True

    if not show:
        bot = BlockBot(dry)
        bot.run()
    else:
        pywikibot.showHelp()


if __name__ == "__main__":
    try:
        main()
    finally:
        pywikibot.stopme()

El API de Wikimedia no permite acceder directamente a usuarios anónimos, por lo que el código anterior no funciona para IPs.

La solución es usar bloqueos basados en proceso de la página web en vez de llamadas a la API.

Ejercicio
Implementar un bot que bloquee IPs.
Sugerencia: Revisar la función _oldBlock( ) definida en userlib.py.