Меню
Главная
Случайная статья
Настройки
|
# -*- coding: utf-8 -*-
import sys, re, time, unicodedata
import wikipedia, catlib
import codecs, string
site = wikipedia.getSite()
cat_namespace = site.category_namespaces()[0]
log_file = codecs.open('log.txt', 'w', 'utf-8')
def log(string):
wikipedia.output(string)
log_file.write(string)
log_file.flush()
# This traverses the cathegory tree (starting from 'root_cat') and peforms
# 'action' at every article node, it runs into by the way.
#
# 'depth_cutoff', if less then one, enables checking, whether the current
# cathegory has alredy been passed earlyer. 'depht_total' if less then one
# cuts the search right away.
def traverse_cat(root_cat, action, depth_cutoff = 0, depth_total = 0, stack = []):
time.sleep(3)
if depth_total < 1:
log(u'Warning: recursion bound riched')
return
wikipedia.output(u'Entering cathegory: ' + root_cat.title())
if depth_cutoff < 1 and root_cat in stack:
log(u'Warning: A cycle detected:')
log(stack.append(root_cat))
return
stack.append(root_cat)
for page in root_cat.articles():
action(page)
for subcat in root_cat.subcategories():
traverse_cat(subcat, action, depth_cutoff - 1, depth_total - 1, stack)
def change_templ(page, pairs):
if page.isRedirectPage():
log(u'Warning: A redirect detected: ' + page.title())
page = wikipedia.Page(site, page.getRedirectTarget())
wikipedia.output(u'Checking page: ' + page.title())
try:
text = page.get()
except wikipedia.NoPage:
log(u'Shit! There is no such a page: ' + page.title())
except wikipedia.IsRedirectPage, arg:
log(u'Shit! It is a redirect page: ' + page.title())
else:
new_text = text
for old, new in pairs:
if (new_text.find(new) > 0):
new_text = new_text.replace(old, '')
else:
new_text = new_text.replace(old, new)
if text != new_text:
if(page.canBeEdited() and page.botMayEdit()):
page.put(new_text)
else:
log(u'Shit! I could do it, but the page is blocked: ' + page.title())
for arg in sys.argv[1:]:
wikipedia.output(u'Ignored argument:%s' % arg)
wikipedia.setAction(u'Робот изменил шаблон на {{royal-stub}}')
subst_list = [
(u'{{bio-stub}}', u'{{royal-stub}}'),
(u'{{Bio-stub}}', u'{{royal-stub}}'),
(u'{{hist-stub}}', u'{{royal-stub}}'),
(u'{{Hist-stub}}', u'{{royal-stub}}'),
(u'{{history-stub}}', u'{{royal-stub}}'),
(u'{{History-stub}}', u'{{royal-stub}}')]
root_cat = catlib.Category(site, u'Категория:Монархи')
traverse_cat(root_cat, lambda p: change_templ(p, subst_list), 10, 10)
root_cat = catlib.Category(site, u'Категория:Династии')
traverse_cat(root_cat, lambda p: change_templ(p, subst_list), 10, 10)
log_file.close()
|
|