Updating a media wiki article using Python?

独自空忆成欢 提交于 2020-01-02 08:09:51

问题


Hi I have a cron job which collects some statistics about a service. I need the cron job to update a media wiki page (append to the page) programmatically. I am using python for the cron so what are my best options, are there any examples of mediawiki/python libraries or does Media wiki expose any HTTP/REST apis which I can use (may be through an extension).

Thanks


回答1:


If PyWikipediaBot is too heavy, try the Python module mwclient.

You can login, view a page’s current content, make your change and then view it in less than 10 lines (example).

import mwclient
site = mwclient.Site('en.wikipedia.org')
site.login('Pfctdayelise','password')
page = site.Pages['User:Pfctdayelise/Test']
text = page.edit()
print text.encode('utf-8')
newtext = "\n\nTesting the write api without logging in.\n"
page.save(text+newtext,summary='testing write api')



回答2:


If you are running mediawiki on the same computer as the cron job, then you can use the edit.php script found in the maintanence directory.

/bin/python /opt/page_renderer.py | php /var/www/mediawiki/maintenance/edit.php -b PageTitle

In this example, /opt/page_renderer.py outputs wiki markdown. This gets piped to the edit script which has the -b flag (to mark it as a bot edit) and the title of the page you wish to edit.

Naturally, you can pipe from any program into the edit script, and you might need to change the path to the edit script if you have mediawiki installed somewhere different.



来源:https://stackoverflow.com/questions/4502993/updating-a-media-wiki-article-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!