跳转到内容

User:Sz-iwbot/op.py

维基百科,自由的百科全书
#!/usr/bin/python
# -*- coding: utf-8  -*-
""" Module to determine the pywikipedia version (tag, revision and date) """
#
# (C) Merlijn 'valhallasw' van Deen
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id: version.py 7033 2009-07-09 02:32:41Z alexsh $'

import os, sys
import wikipedia, catlib, query
mysite = wikipedia.getSite()

t=u'{{Proxy-global}}\n'

#所有挂模板的开放代理IP

cat=catlib.Category(mysite, u'Category:被全域封禁的开放代理')
oplist = cat.articlesList()
#print oplist
if len(oplist) > 0:
    for ippage in oplist:
#        print ippage.namespace
        ip = ippage.titleWithoutNamespace()
#        print ip
        params = {
            'action'    :'query',
            'list'      :'globalblocks',
            'bgip'    :ip,
            }    
        bgcheck= query.GetData(params, encodeTitle = False)

#如果已经全域解封
        if len(bgcheck[u'query'][u'globalblocks']) ==0:
        #print bgcheck
            ut=u'User talk:'+ip
            utpage=wikipedia.Page(mysite,ut)
            text = utpage.get()
            text=text.replace(u'{{Proxy-global}}', u'')
            utpage.put(text, u'Bot: 该IP已全域解封')
            print 'remove {{Proxy-global}}: %s' % (ip)


#全域封禁白名单
wgbs=wikipedia.Site.logpages(mysite, mode = 'gblblock')
wgblist=[]
for wgb in wgbs:
     wgblist.append(wgb[0].title().split("/")[1])



#全域封禁
params = {
    'action'    :'query',
    'list'      :'globalblocks',
    'bglimit'    :'50',
    'bgprop'    :'address|reason',
    }    
bg= query.GetData(params, encodeTitle = False)
for b in bg[u'query'][u'globalblocks']:
    if u'tor' in b[u'reason'] or u'Tor' in b[u'reason'] or u'TOR' in b[u'reason'] or u'proxy' in b[u'reason'] and b[u'address'] not in wgblist:
        ut=u'User talk:'+b[u'address']
        utpage=wikipedia.Page(mysite,ut)
        if 'Proxy-global' not in utpage.templates():
            if utpage.exists():
                text = utpage.get()
                text = t+text
            
            else:
                text=t
            utpage.put(text, u'Bot: 该IP是一个已被全域封禁的开放代理')
            print 'Flag {{Proxy-global}}: %s' % (b[u'address'])


#本地封禁

lt=u'{{Blocked proxy}}\n'
#block=wikipedia.Site.logpages(mysite, number = 500, mode = 'block')
params = {
    'action'    :'query',
    'list'      :'blocks',
    'bklimit'    :50,
    }
block= query.GetData(params, encodeTitle = False)
for b in block[u'query'][u'blocks']:
    try:
        if u'tor' in b[u'reason'] or u'Tor' in b[u'reason'] or u'TOR' in b[u'reason'] or u'proxy' in b[u'reason'] and u'/' not in b[u'user']:
            bp=wikipedia.Page(mysite,u'User:'+b[u'user'])
            bt=bp.toggleTalkPage()
            print bt
            if u'Blocked proxy' not in bt.templates():
                if bt.exists():
                    text = bt.get()
                    text = lt+text
            
                else:
                    text=lt
                bt.put(text, u'Bot: 该IP是一个已被本地封禁的开放代理')
                print 'Flag {{Blocked proxy}}: %s' % (b[u'user'])
    except wikipedia.exceptions.InvalidTitle:
        continue

cat=catlib.Category(mysite, u'Category:在本地被封禁的开放代理')
oplist = cat.articlesList()
if len(oplist) > 0:
    for ippage in oplist:
        ip = ippage.titleWithoutNamespace()
        params = {
            'action'    :'query',
            'list'      :'blocks',
            'bkusers'    :ip,
            }    
        bkcheck= query.GetData(params, encodeTitle = False)
#如果已经解封
#        print bkcheck
        if bkcheck[u'query'][u'blocks'] ==[]:
        #print bgcheck
            ut=u'User talk:'+ip
            utpage=wikipedia.Page(mysite,ut)
            text = utpage.get()
            text=text.replace(u'{{Blocked proxy}}', u'')
            utpage.put(text, u'Bot: 该IP已解封')
            print 'remove {{Blocked proxy}}: %s' % (ip)