Utilisateur:Phe/tor-block

Un article de Wikipédia, l'encyclopédie libre.

botpywi.py: used only to get the pywikipedia path, need to be tweaked

# -*- coding: iso-8859-1 -*-

import sys
sys.path.append('/usr/src/phe/pywikipedia')

create_page.py: used only to abstract page writing

# -*- coding: iso-8859-1 -*-

import botpywi
import wikipedia

def create_page(page_name, text, comment, minor_edit = False):
    new_page = wikipedia.Page(wikipedia.getSite(), page_name)
    if new_page.exists():
        wikipedia.output('new page %s already exists!' % new_page.title())
        return False
    else:
        wikipedia.output('create new page %s' % (new_page.title()))
        new_page.put(text, comment, False, minor_edit)

def write_page(page_name, text, comment, minor_edit = False):
    new_page = wikipedia.Page(wikipedia.getSite(), page_name)
    new_page.put(text, comment, False, minor_edit)

def read_page(page_name, lang_code = None):
    page  = wikipedia.Page(wikipedia.getSite(lang_code), page_name)
    return page.get()

def page_exist(page_name):
    new_page = wikipedia.Page(wikipedia.getSite(), page_name)
    return new_page.exists()

if __name__ == "__main__":
    f = open('/home/phe/wiki/astronomie/Liste_de_liste_asteroide.txt')
    for l in f.readlines():
        print read_page(l, 'en').encode('latin1', 'replace')

tor-block.py: parse the list generated by zorro bot and block page, optionnally create the talk page and block the talk page. Need to change the pagename where bot will find the list.

# -*- coding: iso-8859-1 -*-

import botpywi
import userlib
import create_page
import wikipedia
import re
import time

lang='fr'

def block_ip(ip, site = wikipedia.getSite(code = lang)):
    user = userlib.User(site, ip)
    try:
        user.block(u'2 weeks', reason = u'proxy ouvert',
                   anonOnly = False, noSignup = True, enableAutoblock = True)
        #create_page.write_page(u'User Talk:' + ip,
        #                       u'{{proxy}}', u'{{proxy}}', True)
        #page = wikipedia.Page(site, u'User Talk:' + ip)
        #page.protect(prompt = False, reason = u'proxy ouvert')
    except userlib.BlockError:
        print "already blocked"
        

def treat_block_page(page_name):
    text = create_page.read_page(page_name, lang_code = lang)
    it = re.finditer(u'{{tor-node\|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\|}}', text)
    for i in it:
        block_ip(i.group(1))
        time.sleep(5)

if __name__ == "__main__":
    try:
        #block_ip(u'146.50.210.82')
        treat_block_page(u'Utilisateur:Schiste/Tor/list')
    finally:
        wikipedia.stopme()