Szerkesztő:Tacsipacsi/kért cikkek.py

A Wikipédiából, a szabad enciklopédiából

Wikipédia:Kért cikkek/en és társainak frissítését végző parancsfájl:

# -*- coding: utf-8 -*-
"""
collect wanted pages connected to Hungary from other wikis
"""

import pywikibot
from pywikibot import logging
import codecs

f = codecs.open('lista2.txt', 'a', 'utf-8')
seen = {}

def error(title, message):
    pywikibot.output(title + u': \03{lightred}%s\03{default}' % message)

def good(title, message):
    pywikibot.output(title + u': \03{lightgreen}%s\03{default}' % message)

def processpage(page):
    """
    Test a page if it has a sitelink to huwiki:
    * False if has (should be dropped)
    * True if not (should be gathered)
    """
    title = page.title()
    if title in seen:
        return False
    seen[title] = True
    try:
        item = page.data_item()
        sitelinks = item.sitelinks
    except:
        error(title, u'Nincs elem')
        return True
    try:
        huwiki = sitelinks['huwiki'].title
    except:
        error(title, u'Nincs magyar cikk')
        return True
    else:
        good(title, u'Van magyar cikk')
        return False

def iterate(supercat):
    """
    iterate on all subcategories of a supercategory
    return list with results
    """
    wanted = []
    for page in supercat.articles():
        if processpage(page):
            f.write(page.title() + u'\n')
    for sub in supercat.subcategories():
        wanted += iterate(sub)
    return wanted

def main(*args):
    fromlang = 'en'
    categories = {'en': "Hungary"}
    for arg in pywikibot.handle_args(args):
        if arg.startswith("-fromlang:"):
            fromlang = arg[len("-fromlang:"):]
    site = pywikibot.Site(fromlang, 'wikipedia')
    supercat = pywikibot.Category(site, categories[fromlang])
    wanted = iterate(supercat)
    if wanted:
        f = open('lista.txt', 'w')
        for page in wanted:
            f.write(page + '\n')
        f.close()

if __name__ == '__main__':
    main()