User:Sz-iwbot/deledpimage.py

维基百科,自由的百科全书
#!/usr/bin/python
# -*- coding: utf-8  -*-

"""
commond:
python deledpimage.py -category:category:name

删除显示在用户页上的合理使用图像

"""
import re, time
import wikipedia

site = wikipedia.getSite()
summary = u'remove EDP image from user namespace'
template=''
ns=2
name=[]
#设置参数
for arg in wikipedia.handleArgs():

    if arg.startswith('-template:'):
       template = arg[10:]
 #   else:
  #      template = wikipedia.input(u'plese input EDP images template: ')
    elif arg.startswith('-ns:'):
        ns=arg[4:]
 #   else:
 #       ns=wikipedia.input(u'plese input check namespace: ')
    name.append(arg)
#print name
template=name[0][10:]
ns=int(name[1][4:])
#template='logo'
#ns=2
#得到使用该模板的图像
s=wikipedia.Page(site, 'Template:%s' % template)
#print template, ns

images = [page for page in s.getReferences() if page.isImage()]

for image in images:
    imagepage=wikipedia.ImagePage(site,image.title())

#使用该图像的页面
    pimages=[puseimage for puseimage in imagepage.usingPages() if puseimage.namespace()==ns]
    for pimage in pimages:
        c=u'\nfond an used the image [[%s]] in [[%s]]: ' % (image.title(), pimage.title())	
        p = wikipedia.Page(site, pimage.title())
        text=p.get()

        title=image.title()
#        if title in text:
#             wikipedia.output(u'YES, YES, YES')
#        else:
#             wikipedia.output(u'NO, NO, NO')

#例如: title = Image:wiki.png
        if title not in text:

# 没有名字空间的图像,常在模板中使用
            if title[6:] in text:
                text = re.sub(title[6:]+'(.*?)\n', '\n',text, re.I)
                pimage.put(text, u'Bot移去EDP图像 [[%s]] in [[%s]]' % (image.title(), pimage.title()))
                wikipedia.output(c+u'remove!!!')

            else:
#图像已经不在页面上,或者图像使用在页面的模板中
                wikipedia.output(c+u'have remove.')	
#图像使用[[Image:wiki.png]]语法
        else:
	
            if '[['+title in text:

            
#在用户页,图像页或各对话页使用的图像,将[[Image:wiki.png]]变成[[:Image:wiki.png]]
                if ns==1 or ns==6 or ns==2 or ns==3 or ns==5 or ns==7 or ns==9 or ns==11 or ns==13 or ns==15 or ns==17 or ns==101:
                    text = re.sub('\[\['+title+'(.*?)\]\]', '[['+':'+title+']]',text, re.I)	
                    pimage.put(text, u'Bot修正EDP图像用法: [[:%s]]' % title)
                    wikipedia.output(c+u'fix!')
#在模板,分类页找到的的图像,删除
                elif ns==10 or ns==14:
                    text = re.sub('\[\['+title+'(.*?)\]\]', '',text, re.I)	
                    pimage.put(text, u'Bot移去EDP图像 [[%s]] in [[%s]]' % (image.title(), pimage.title()))
                    wikipedia.output(c+u'remove!!!')
#找到[[:Image:wiki.png]]使用的图像
            elif '[[:'+title in text:
                wikipedia.output(c+u'EDP is OK!')

#在<gallery></gallery>中使用的图像
            else:
                text = re.sub(title+'(.*?)</gallery>', '</gallery>',text, re.I)
                text=re.sub('</gallery>\n', '</gallery>\n'+'[[:'+title+']]\n',text, re.I)
                pimage.put(text, u'Bot修正EDP图像<gallery>用法: [[:%s]]' % title)
                wikipedia.output(c+u'FIX <gallery>')