Участник:XBot/catmath.py
Перейти к навигации
Перейти к поиску
# -*- coding: utf-8 -*-
import sys, re, time
import wikipedia, catlib
cat_namespace = wikipedia.getSite().category_namespaces()[0]
def cat_math():
site = wikipedia.getSite()
cat = catlib.Category(site, u'Категория:Математики по странам')
#wikipedia.output(u'SUBCATEGORIES:')
for subcat in cat.subcategories():
for page in subcat.articles():
add_cat(page, u'Математики по алфавиту')
def add_cat(page, cat_name):
try:
cats = page.categories()
except wikipedia.IsRedirectPage,arg:
redirTarget = wikipedia.Page(site,arg.args[0])
wikipedia.output(u"WARNING:%s is redirect to %s. Ignoring." % (page.title(), redirTarget.title()))
else:
catpl = wikipedia.Page(site, cat_namespace + ':' + cat_name)
#if sort_by_last_name:
# catpl = sorted_by_last_name(catpl, page)
if catpl in cats:
wikipedia.output(u"%s is already in %s." % (page.title(),catpl.title()))
else:
wikipedia.output(u'Adding %s' % catpl.aslink())
cats.append(catpl)
text = page.get()
text = wikipedia.replaceCategoryLinks(text, cats)
try:
page.put(text)
except wikipedia.EditConflict:
wikipedia.output(u'Skipping %s because of edit conflict' % (page.title()))
for arg in sys.argv[1:]:
wikipedia.output(u'Ignored argument:%s' % arg)
cat_math()