Szerkesztő:BinBot/commonslocaluncat.py

Lásd
# coding: utf-8
"""
This script gets a list of filelinks from BASEPAGE. They are images on Commons
that have local description pages. See
http://hu.wiki.x.io/w/index.php?title=Szerkeszt%C5%91:Bin%C3%A1ris/Commonsk%C3%A9pek&oldid=11398781
for format. This script will list the contents of the pages next to the title
so that you can overview which of them is to be deleted.
For creating the list, see
[[Wikipédia:SQL-lekérdezések#Commonsos képek helyi leírólappal]] on this wiki.
"""

#(C) Bináris, 2012

BASEPAGE = u'Szerkesztő:Bináris/Commonsképek'

import re
import wikipedia as pywikibot
site = pywikibot.getSite()
page = pywikibot.Page(site, BASEPAGE)
text = page.get()
pattern = re.compile(ur'#\[\[:(File:.*?)\]\]')
counter = 0
for p in pattern.finditer(text):
    # p.group() -- the whole link to be replaced later
    # p.group(1) -- the filename
    pic = pywikibot.Page(site, p.group(1))
    pywikibot.output(pic.title())
    counter += 1
    try:
        tic = pic.get().replace('\n', '_')
    except pywikibot.NoPage:
        tic = u'Törölve?' # Deleted
    if not len(tic.replace(' ', '').replace('_', '')):
        tic = u'Üresnek látszik' #Empty
    pywikibot.output(tic)
    delimiter = u'==%d. szakasz==\n' % (counter // 5 + 1) if counter % 5 == 1 else ''
    text = text.replace(p.group(), delimiter + p.group() + ' <nowiki>%s</nowiki>' % tic)
page.put(text, u'commonslocaluncat.py: a helyi leírólapok listázása')