利用者‐会話:Hatukanezumi/Using the python wikipediabot

ページのコンテンツが他言語でサポートされていません。

In your own language: de - fr - it - nl - pl - pt -


自分で pywikipediabot を設定するのにもっと助けが欲しいときは、#pywikipediabot @ freenode server にくれば助けてもらえるかもしれません。

The Python Wikipediabot フレームワークは、Wikipedia その他の Mediawiki プロジェクトでなにかするための ボット スクリプトの集合です。何人もの人によって Python を使って書かれています。このページでは、ボットソフトウェアを使いたがっている人たちのための全般的な情報を提供します。

初期設定[編集]

ダウンロード[編集]

ボットを使うには、ボットソフトウェア、それと Python の 2.3 版以降をダウンロードしなければなりません。

Unix の配布物のなかには、Python がもともと入っているものもあり、そういうときはわざわざ入れる必要はありません (ただし、すごく古い版が入っていたら、アップデートする必要があるでしょう)。そうでないときは、 http://www.python.org/download/ にあります。Python はたいていのプラットフォーム (Unix, Linux, Mac, Windows) で動作します。Python をダウンロードしてインストールします。

ボットソフトウェアそのものは Sourceforge にあります。http://sourceforge.net/projects/pywikipediabot/ ではパッケージでもダウンロードできますが、時期外れの版しかないことがしばしばです (最新版 を参照)

ボットソフトウェアを手に入れ、自分のコンピュータにダウンロードしたら、それ以外にインストールなどは必要ありません (Python25 http://www.python.org/download/ で動作させるには、ファイルを Python25 フォルダの中にダウンロードしなければなりません)。

最新版[編集]

もっとも最新の版 (最大約 24 時間以内) は CVS で取得できます (CVS クライアントをなにもインストールしていないのなら http://sourceforge.net/cvs/?group_id=93107 でも。が、これだとファイルをひとつひとつダウンロードするはめになります。結局、CVS を使うのが一番のやりかたです)。Unix では CVS は標準です。Windows では TortoiseCVS [1] がいいでしょう。TortoiseCVS の設定のしかたの説明は[2] 参照。Unix 流のコマンドラインツールでボットをチェックアウトするには、つぎのコマンドを使います:

$ cvs -d :pserver:[email protected]:/cvsroot/pywikipediabot login
Password: (パスワードは入れない。[Enter] を押すだけ)
$ cvs -d :pserver:[email protected]:/cvsroot/pywikipediabot co pywikipedia

コマンドラインツール以外では、つぎの情報があればできるでしょう:

プロトコル (Protocol): pserver
サーバ (Server): pywikipediabot.cvs.sourceforge.net
ユーザ (User): anonymous
パスワード (Password): (空にしておく。匿名アクセスなのでパスワードはいらない)
CVSルート (CVS Root) /cvsroot/pywikipediabot
CVSモジュール (CVS Module) pywikipedia

ボットのメーリングリスト[編集]

ボットのメーリングリストに登録するというのはいい考えかもしれません (http://sourceforge.net/mail/?group_id=93107 参照)。ボットソフトウェアのファイルに変更があるたびに、リストにメールが届くので、新しい版に更新する時期がきたかどうかがわかります。

ウィキペディア向けの設定[編集]

メモ帳などのテキストエディタを開きます。

次のように打ちます:

mylang = 'xx'

ここで xx は自分が作業する言語のコードです。"ja" で日本語です。

このテキストファイルを user-config.py として、ダウンロードした .py ファイルとおなじフォルダの中に保存します。


user-config.py ファイルでボットのユーザ名を指定する必要があります。

たとえば、スウェーデン語版ウィキペディアで作業したいとします。「ExampleBot」というユーザ名でボットのアカウントを作成してあるとしたら、user-config.py に次のものを加えます:

usernames['wikipedia']['sv'] = u'ExampleBot'

ユーザ名の前の u は Unicode を表します。ユーザ名に ASCII 以外の文字が入っているのなら、これは重要です (訳注: ここに説明してあるとおりにしただけでは動作しないことがあります。理由を知りたければ自分で Python を学んでください)。 複数のウィキで作業するときは、ユーザ名をいくつも指定しておけます。例:

usernames['wikipedia']['de'] = u'BeispielBot'
usernames['wikipedia']['en'] = u'ExampleBot'
usernames['wiktionary']['de'] = u'BeispielBot'

もう一度ファイルを保存します。

ほかのウィキページ向けの設定[編集]

メモ帳などのテキストエディタを開きます。

次のように打ちます:

mylang = 'xx'

ここで xx は自分が作業する言語のコードです。"ja" で日本語です。

さらに次のように打ちます:

family = 'sitename'

ここで「sitename」は、自分が作業するサイトの名前です。

現在、この名前は wiktionary、wikibooks、wikiquote、さらには Wikimedia プロジェクトではない wikitravel などにもできます (families フォルダにすべての一覧があります)。WikiMedia Commons で作業するときは、言語にもファミリにもcommons を指定します。

自分のウィキが families フォルダにないときは、自分のファミリの .py ファイルを作ります。Pywikipedia bot on non-Wikimedia projects を参照。

user-config.py ファイルでボットのユーザ名を指定する必要があります。

たとえば、英語版 Memory Alpha で作業したいとします。「ExampleBot」というユーザ名でボットのアカウントを作成してあるとしたら、user-config.py に次のものを加えます:

usernames['memoryalpha']['en'] = u'ExampleBot'

まとめると、作った user-config.py に次の行がなければなりません。これは memoryalpha の例です:

mylang = 'en'
family = 'memoryalpha'
usernames['memoryalpha']['en'] = u'ExampleBot'

このテキストファイルを user-config.py として、ダウンロードしたほかの .py ファイルとおなじフォルダの中に保存します。

Unicode 版メディアウィキがインストールしてある場合[編集]

Unicode 版メディアウィキがインストールしてあるところで作業するときで、ラテン文字以外を使うつもりのときは、端末の文字コードを UTF-8 に変更し (これは最近の Linux 配布物では標準の設定です)、user-config.py では console_encoding を utf-8 に設定しておきましょう。そうしないと、Unicode の文字がみんな疑問符 ("????") になってしまいます:

console_encoding = 'utf-8'

ウィキで許可を得る[編集]

あなたのボットは、使おうとしているウィキのコミュニティで認可を受けなければいけません。その厳格さはプロジェクトごとに大きく異なります。あらかじめアナウンスして使用開始の前に認可を得なければいけないプロジェクトもありますし、好きなように使えるプロジェクトもあります。

普通のブラウザを使ってボット用のユーザ名とパスワードを作成します。ボットだとはっきりわかり、しかもだれが操作しているのかわかユーザ名がいちばんです。一般的にはユーザ名に「bot」をつけたものにしますが、違う名前もたまにあります。

ウィキペディア英語版では、en:Wikipedia:Botsで言っておかないとボットの使用は認められません。なお、ウィキペディア英語版で interwiki ボットを使うことについての合意はありませんからご注意ください。

ボットフラグを請求する[編集]

ボットを大量に使うと、「最近更新したページ」が見苦しくなってしまいます。そうならないように、自分のボットがボットだということを登録しておけます。すると、利用者がわざわざ「ボットの編集を表示」にしないかぎり「最近更新したページ」には出なくなります。

スチュワードビューロクラットなら、それができます。Requests for bot statusで、自分や他人のボットを登録するよう請求ができます。おそらく、あなたのコミュニティであなたのボットに同意があるかどうかについて、なんらかの証拠を求められることになるでしょう。英語のウィキペディアでは、Requests for approvalsで申請しなければなりません。一度の実行でたくさんのページを編集することになるときはかならず、ボットを登録しておくのがいちばんでしょう。

Use[編集]

Select and run a bot script[編集]

Now we are ready to really start using the bot. You need to get to a textual interface to your Operating System.

On Windows this is done by opening the start menu, and clicking on 'Run'. You are asked to give the name of a program, type "cmd.exe".

  • Change the root to C: by typing cd \
  • Type cd \"name of the folder where python wikipediabot has been downloaded"

On the Mac, find Terminal.app in /Applications/Utilities.

First run the script login.py by typing "python login.py" (in cmd.exe in Windows). It will ask for a password, use the password you used for the bot's login name. The bot can't work anonymously. Unless you change your password, you normally need to run this program only once, the bot usually does not get logged off.

Using the command cd, go to the directory where you saved the files of the robot. You can now run any of the bots here by typing "python botname.py" (If you are using Windows, you might leave out "python").

Now you can use one of the bot scripts. Below is a list of the existing bots with links to their descriptions:

Main bot scripts:

Other bot scripts:

Auxiliary programs:

Command-line arguments[編集]

Although many bot scripts have their own command line arguments, which should be documented on their respective pages (or in their source code), all bots unless specifically stated to the contrary recognize the following command line arguments:

-help
Print a list of global bot arguments (this list), followed by bot-specific help if available.
-lang:xx
Set the language of the wiki you want to work on to language code xx, overriding the configuration in user-config.py.
-family:xyz
Set the family of the wiki you want to work on, e.g., wikipedia, wiktionary, wikitravel, ... This will override the configuration in user-config.py.
-log
Enable the logfile. Logs will be stored in the logs subdirectory.
-log:xyz
Enable the logfile, using xyz as the filename.
-nolog
Disable the logfile (if it's enabled by default).
-putthrottle:nn
Set the minimum time (in seconds) the bot will wait between saving pages.

For example, python scriptname.py -family:wiktionary will run the "scriptname" bot on wiktionary articles, overriding the default family setting in your user configuration.

Using bot for a local installed wiki[編集]

Look here to find out how to configure the bot to use it on your own mediawiki-powered project.

Update[編集]

For further updates chdir into pywikipedia and type

$ cvs up -Pd

Development[編集]

If you have a function you want to have a bot for that is not yet provided by one of the bots, you can ask one of the programmers to write it for you. Or even better, you can try to work on the bots yourself. Python is a nice language, and not hard to learn. We will welcome you.

Tips[編集]

Here are some very basic tips for getting started writing your own bot:

  • be sure you've set up your user-config.py file (see above)
  • use import wikipedia to gain access to the pywikipedia framework
  • to retrieve a page, use the following, where pageName is, e.g., "Wikipedia:Bots" or "India":
site = wikipedia.getSite()
page = wikipedia.Page(site, pageName)
text = page.get()
  • to update a page, use:
page.put(newText)
  • look at some of the pywikipedia files for other ideas -- replace.py is relatively easy to read even if you're new to pywikipedia.
  • you can find all available Page methods in the wikipedia.py file.
  • basic.py gives you a setup that can be used for many different bots, all you have to do is define the string editing on the page text.
  • To iterate over a set of pages, see pagegenerators.py for some objects that return a set of pages. An example use of the CategoryPageGenerator that does something for each page in the Category:Living people category:
site = wikipedia.getSite()
cat = catlib.Category(site,'Category:Living people')
gen = pagegenerators.CategorizedPageGenerator(cat)
for page in gen:
  #Do something with the page object, for example:
  text = page.get()

Contributing changes[編集]

If you changed the bot and want to send a patch to the maintainer,

  1. Update to the current version (it will merge your changes with the improvements already committed to the CVS Repository),
  2. Resolve any conflicts caused by the update (grep for "=====" ;-) and
  3. Type:
$ cvs diff > cvs.diff

You can then send the file cvs.diff to the developer And ask them to include the changes (maybe you want to review the diff before sending it). The lines at the beginning starting with "?" may be removed.

Multiple accounts[編集]

It is a common need to run python wikipedia bot under different accounts (main and/or multiple bot accounts). It can be done in two ways.

Separate pywikipedia distributions[編集]

One can install completely separate instances of pywikipedia in different directories (1 for each account) and have diferent user-config.py files in each of them. However, when updating the installation via CVS, one needs to run cvs update on each folder separately. Also, every installation takes some disk space, which might be a problem on accounts with limited quota.

One pywikipedia distribution with symbolic links[編集]

Let's assume user foo has a current CVS snapshot of pywikipedia in /home/foo/pywikipedia. For each of the accounts, he creates a separate directory:

foo@bar:~$ mkdir foobot
foo@bar:~$ cd foobot

Pywikipedia needs then some symlinks to the main code tree created in the working directory:

foo@bar:~/foobot$ ln -s ~/pywikipedia/families
foo@bar:~/foobot$ ln -s ~/pywikipedia/userinterfaces

Then, user-config.py for this account must be created as described in "Configure" section above.

Finally, the bot must be logged in the usual way:

foo@bar:~/foobot$ python ~/pywikipedia/login.py

The working directory is ready. The scripts will however require a slight modification to run (the path to the pywikipedia tree must be added to Python's path).

import sys, os
sys.path.append(os.environ['HOME'] + '/pywikipedia')
import wikipedia

That's all. Updating to the newest version of pywikipedia on all account at once is now a matter of running cvs update only in the ~/pywikipedia directory.

See also[編集]