Користувач:Ilya/Bots/GreekMythsNoIwiki
< Користувач:Ilya | Bots
код на Python (запускався на Python 2.6.1), використовує pywikipediabot
Знаходить статті що не мали інтервікі, з категоріі Давньогрецька міфологія та підкатегорій. Результат — Користувач:Ilya/GreekMythsNoIwiki.
Бот[ред. | ред. код]
iwiki_greek.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import re
import wikipedia, catlib, category
from string import Template
class IwikiGreek:
def __init__ (self, catTitle, listTitle) :
self.cat = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle)
self.list = wikipedia.Page(wikipedia.getSite(), listTitle)
self.recurse = True
self.langEl = re.compile('\{\{\s*lang-el\s*\|\s*(\w+)\s*\}\}')
def wikiList(self, noIwikiArticles):
t = Template('|-\n| [[$title]] || $greekNames\n')
return [t.substitute(
title=article.title(),
greekNames=''.join(self.greekNames(article)))
for article in noIwikiArticles]
def greekNames(self, article):
template = Template("[http://en.wikipedia.org/w/index.php?title=Special:Search&search=$search&fulltext=Search $search]")
text = article.get()
matches = self.langEl.findall(text)
return [template.substitute(search = match.strip()) for match in matches]
def articleHasNoIwiki(self, article):
print article.title()
return (not (article.isImage() or article.isCategory() or article.isTalkPage())
and len(article.interwiki()) == 0)
def noIwikiArticles(self, listOfArticles):
return filter(self.articleHasNoIwiki, listOfArticles)
def run(self):
#listOfArticles = self.list.linkedPages()
listOfArticles = self.cat.articlesList(recurse=self.recurse)
noIwikiArticles = self.noIwikiArticles(listOfArticles)
wikiList = self.wikiList(noIwikiArticles)
self.list.put('{| {{prettytable}}\n' + ''.join(wikiList) + '|}', comment='test')
if __name__ == "__main__":
try:
catDB = category.CategoryDatabase()
iwiki_greek = IwikiGreek(u'Давньогрецька міфологія', 'User:Ilya/GreekMythsNoIwiki')
iwiki_greek.run()
finally:
catDB.dump()
wikipedia.stopme()
Юніт тести[ред. | ред. код]
Використовується http://python-mock.sourceforge.net/, звантажити
#!/usr/bin/python
# -*- coding: utf-8 -*-
from iwiki_greek import IwikiGreek
from mock import Mock
import unittest
class SimpleTestCase(unittest.TestCase):
def setUp(self):
self.iwikiGreek = IwikiGreek(u'Давньогрецькі боги', 'User:Ilya/GreekDeitiesNoIwiki')
def testNoIwikiArticles(self):
listOfArticles = []
assert [] == self.iwikiGreek.noIwikiArticles(listOfArticles)
def testNoIwikiArticles2(self):
listOfArticles = [Mock({"isImage" : True}),
Mock({"isCategory" : True}),
Mock({"isTalkPage" : True})]
assert [] == self.iwikiGreek.noIwikiArticles(listOfArticles)
def testNoIwikiArticles3(self):
listOfArticles = [Mock({"isImage" : True}),
Mock({"isCategory" : True}),
Mock({"isTalkPage" : True}),
Mock({"interwiki" : [], "title" : "noIwiki"}),
Mock({"interwiki" : ["iwiki"], "title" : "iwiki"})]
result = self.iwikiGreek.noIwikiArticles(listOfArticles)
assert len(result) == 1
assert result[0].title() == "noIwiki"
def testGreekNames(self):
article = Mock({"get": "Зевс ({{lang-el|Zeus}}) bla bla"})
link = "[http://en.wikipedia.org/w/index.php?title=Special:Search&search=Zeus&fulltext=Search Zeus]"
assert [link] == self.iwikiGreek.greekNames(article)
def testGreekNames2(self):
article = Mock({"get": "Зевс ({{ lang-el | Zeus }}) bla bla"})
link = "[http://en.wikipedia.org/w/index.php?title=Special:Search&search=Zeus&fulltext=Search Zeus]"
assert [link] == self.iwikiGreek.greekNames(article)
def testGreekNames3(self):
article = Mock({"get": "Зевс ({{lang-el|Zeus}}) bla {{lang-el|Jupiter}} bla"})
link1 = "[http://en.wikipedia.org/w/index.php?title=Special:Search&search=Zeus&fulltext=Search Zeus]"
link2 = "[http://en.wikipedia.org/w/index.php?title=Special:Search&search=Jupiter&fulltext=Search Jupiter]"
assert [link1, link2] == self.iwikiGreek.greekNames(article)
def testWikiList(self):
assert [] == self.iwikiGreek.wikiList([])
def testWikiList2(self):
list = [ Mock({"title":'Article', "get": ''})]
wikiList = self.iwikiGreek.wikiList(list)
print wikiList
assert ['|-\n| [[Article]] || \n'] == wikiList
def testWikiList3(self):
list = [Mock({"title":'Article1', "get": ''}), Mock({"title":'Article2', "get": ''})]
wikiList = self.iwikiGreek.wikiList(list)
print wikiList
assert ['|-\n| [[Article1]] || \n', '|-\n| [[Article2]] || \n'] == wikiList