跳转到内容

用户:PhiLiP/block.py

维基百科,自由的百科全书
#! -*- coding: utf-8 -*-
import json
import urllib, urllib2
import cookielib, time

cj = cookielib.CookieJar()
opener = urllib2.build_opener( urllib2.HTTPCookieProcessor( cj ) )
opener.addheaders = [('User-agent', 'Philip-bot')]

def api( query ):
	query['format'] = 'json'
	query = urllib.urlencode( query )
	fp = opener.open( 'http://zh.wikipedia.org/w/api.php', query )
	return json.load( fp )

login = {'action' : 'login',
	'lgname' : '在这里输入您的用户名',
	'lgpassword' : '在这里输入您的密码' }

login['lgtoken'] = api( login )['login']['token']
api( login )

abuselog = {'action' : 'query',
	'list' : 'abuselog',
	'aflfilter' : 81,
	'afllimit' : 500
}

while True:
	users = []
	for log in api( abuselog )['query']['abuselog']:
		users.append( log['user'] )

	users = set( users )
	blocklist = {'action' : 'query',
		'list' : 'blocks',
		'bkusers' : '|'.join( users ),
		'bklimit' : 500
	}
	
	blocked = []
	for bl in api( blocklist )['query']['blocks']:
		blocked.append( bl['user'] )

	users -= set( blocked )

	for user in users:
		print "Blocking %s..." % user
		block = {'action' : 'block',
			'user' : user,
			'gettoken' : True,
			'expiry' : 'infinite',
			'reason' : '机器人:{{blocked proxy}}',
			'anononly' : True,
			'nocreate' : True,
			'reblock' : True
		}
		block['token'] = api( block )['block']['blocktoken']
		block.pop( 'gettoken' )
		api( block )
	time.sleep( 10 )